1 回答

TA貢獻(xiàn)1871條經(jīng)驗 獲得超8個贊
您想使用 GridRange 放置值。
您想
table_data = [["11"],["22"]],[["33"],["44"]]
用作值。我認(rèn)為如果你想把值放在 2 行和 2 列,它變成
table_data = [["11", "22"], ["33", "44"]]
. 所以我用了這個。如果要將11、22、33和44分別放入A1、B1、A2和B2,就變成了
table_data = [["11", "22"], ["33", "44"]]
。如果要將11、22、33和44分別放入A1、A2、B1和B2,就變成了
table_data = [["11", "33"], ["22", "44"]]
。
如果我的理解是正確的,那么使用batchUpdate的“updateCells”而不是“pasteData”的方法怎么樣?我認(rèn)為針對您的情況有幾種解決方案。因此,請將此視為其中之一。
修改后的腳本:
spreadsheet_id = SPREADSHEET_ID
sheetId = 0
table_data = [["11", "33"], ["22", "44"]] # or table_data = [["11", "22"], ["33", "44"]]
rows = [{'values': [{'userEnteredValue': {'stringValue': f}} for f in e]} for e in table_data]
rng = {'sheetId': sheetId, 'startRowIndex': 0, 'startColumnIndex': 0}
fields = 'userEnteredValue'
body = {'requests': [{'updateCells': {'rows': rows, 'range': rng, 'fields': fields}}]}
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=body)
response = request.execute()
將值放入 后
A1:B2
,您可以設(shè)置左上角單元格的坐標(biāo)。所以在這種情況下,GridRange 變成{'sheetId': sheetId, 'startRowIndex': 0, 'startColumnIndex': 0}
筆記:
如果要使用
table_data = [["11"], ["22"]], [["33"], ["44"]]
,請修改rows
為rows = [{'values': [{'userEnteredValue': {'stringValue': f[0]}} for f in e]} for e in table_data]
。
添加回答
舉報