【问题标题】:Can't insert range to google sheets in Python无法在 Python 中将范围插入谷歌表格
【发布时间】:2019-07-08 16:43:26
【问题描述】:

我有数据数组 table_data = [["11"],["22"]],[["33"],["44"]] 我想使用 sheet.values().update 将这些数据插入 Google 表格,但使用 GridRange,没有命名范围 我有这个决心:

service = build('sheets', 'v4', credentials=creds())
    sheet = service.spreadsheets()
    body = {'values': values}
    SAMPLE_RANGE_NAME_2 = 'costs!A1:B2'
    value_input_option = 'RAW'
    result = sheet.values().update(
        spreadsheetId=SAMPLE_SPREADSHEET_ID, range=SAMPLE_RANGE_NAME_2, valueInputOption=value_input_option, body=body).execute()

但是在 A1:B2 范围内使用起来很不方便。我想用这样的东西

'startColumnIndex': 0,
'endColumnIndex': 1,
'startRowIndex': 0,
'endRowIndex': 1

我尝试使用批处理更新,但它只能接受字符串参数

spreadsheet_id = SPREADSHEET_ID  # TODO: Update placeholder value.

batch_update_spreadsheet_request_body = {
        "requests": [
            {
                "pasteData": {
                    "data": str(table_data),
                    "type": "PASTE_NORMAL",
                    "delimiter": ",",
                    "coordinate": {
                        "sheetId": 0,
                        "rowIndex": 0,
                        "columnIndex": 0
                    }
                }
            }
        ]
    }
request = service.spreadsheets().batchUpdate(spreadsheetId=spreadsheet_id, body=batch_update_spreadsheet_request_body)
response = request.execute()

【问题讨论】:

    标签: python google-sheets google-api google-sheets-api google-api-python-client


    【解决方案1】:
    • 您想使用 GridRange 放置值。
    • 您想使用table_data = [["11"],["22"]],[["33"],["44"]] 作为值。
      • 我认为如果你想把值放在 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”怎么样?我认为您的情况有几种解决方案。所以请认为这只是其中之一。

    修改脚本:

    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时,可以设置左上角单元格的坐标。所以在这种情况下,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]

    参考:

    如果我误解了您的问题,我深表歉意。

    【讨论】:

    • 请告诉我,它是否适用于大量细胞?因为当我想在循环中将数据从列表传递到单元格时 - 这需要很长时间......
    • @Zzema 感谢您的回复。关于huge qty of cells,我没有注意到您从问题中使用的值的数量。我为我糟糕的英语水平道歉。关于限制,当使用的单元接近500万个时,会发生错误和/或处理时间变长。关于Sheets API的成本,我测了一下,用于写值的Sheets APIbatchUpdatevalues.updatevalues.batchUpdatevalues.append这4个方法的成本几乎是一样的。跨度>
    • @Zzema 所以作为一个方法,首先创建包含值的请求体,然后通过一个API调用发送。如果一个 API 调用无法发送值,则需要将值拆分并通过多个 API 调用发送。如果使用的单元格数量超过 500 万个单元格,则需要将值拆分并放到其他电子表格中。但是,如果我误解了您的问题并且我的回答对您的情况没有用处。我必须道歉。
    • 我正在尝试使用它,但它仅适用于字符串'stringValue':如果某些列有数字,我可以做什么?
    • @Zzema 如果你想放公式和数字,你也可以这样做。您可以在here查看文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多