更新 google 个工作表中的列

Updating Cols in google Sheets

我正在尝试将列从一个 sheet 复制到另一个 sheet。我从来源 sheet 获得响应的列。我需要将它们插入 sheet。由于 insertDimension 和 insertRange 等方法无法做到这一点。我使用了 request = service.spreadsheets().values().update(spreadsheetId=to_spreadsheet_id, range=range_, valueInputOption = "USER_ENTERED", body={"values": response}) 但它给了我这样的错误:- googleapiclient.errors.HttpError: https://sheets.googleapis.com/v4/spreadsheets/1sERXk6YshuNOKi4ggp11a36uf5SGutLg7DAP5vitOoQ/values/Working%20Analysis%21G2?valueInputOption=USER_ENTERED&alt=json returned "Invalid values[1][0]: struct_value { 领域{ 关键:“有效格式” 价值 { struct_value { 领域{ 关键:“背景颜色” 价值 { struct_value{ 领域{ 键:“蓝色” 价值 { number_value:1.0 } } 领域{ 关键:“绿色” 价值 { number_value:1.0 } 好长的文......

如果我使用代码块中注释的其他方式,我会收到以下错误 详细信息:“无效请求 [0].updateCells:尝试写入行:15000,超出最后请求的行:14999”>

def copy_column(service, from_spreadsheet_id, to_spreadsheet_id, from_sheet='Analysis', to_sheet_id, from_column='F', from_column_till='K', to_column='G'):

    request = service.spreadsheets().get(spreadsheetId=from_spreadsheet_id, ranges=[
        from_sheet + "!" + from_column + ":" + from_column_till], includeGridData=True)

    response = request.execute()["sheets"][0]["data"][0]["rowData"]

    range_ = "Working Analysis!G2"

    print(response)

    # value_range_body = {
    #     "requests": {
    #         "insertDimension": {
    #             "range": {
    #                 "sheetId": to_sheet_id,
    #                 "dimension": "COLUMNS",
    #                 "startIndex": 6,
    #                 "endIndex": 11
    #             },
    #             "inheritFromBefore": True
    #         }
    #     }
    # }
    # request_1 = service.spreadsheets().batchUpdate(spreadsheetId=to_spreadsheet_id, body=value_range_body)
    # response_1 = request_1.execute()

    # body = {
    #     "requests": {
    #         "updateCells": {
    #             "rows": response,
    #             "fields": "userEnteredFormat, userEnteredValue",
    #             # "start":{
    #             #     "sheetId": to_sheet_id,
    #             #     "rowIndex": 1,
    #             #     "columnIndex": 6
    #             # },
    #             "range": {
    #                 "sheetId": to_sheet_id,
    #                 "startRowIndex": 1,
    #                 "startColumnIndex": 6,
    #                 "endColumnIndex": 13
    #             },

    #         }
    #     }
    # }

    request = service.spreadsheets().values().update(spreadsheetId=to_spreadsheet_id,
                                                   range=range_, valueInputOption = "USER_ENTERED", body={"values": response})

    response = request.execute()

    return print('Done')

我认为 service.spreadsheets().get() 的响应值不能直接用于 service.spreadsheets().values().update()。从您评论的脚本中,我猜想您可能不仅要复制值,还要复制单元格格式。

这样的话,下面的修改怎么样?

修改后的脚本:

sheetId = "###"  # Please set the sheet ID of the sheet "Working Analysis"

request = service.spreadsheets().get(spreadsheetId=from_spreadsheet_id, ranges=[from_sheet + "!" + from_column + ":" + from_column_till], includeGridData=True)
response = request.execute()["sheets"][0]["data"][0]["rowData"]

requests = {
    "requests": [
        {
            "updateCells": {
                "start": {"sheetId": sheetId, "rowIndex": 1, "columnIndex": 6},
                "rows": response,
                "fields": "*",
            }
        }
    ]
}
request = service.spreadsheets().batchUpdate(spreadsheetId=to_spreadsheet_id, body=requests)
response = request.execute()
print("Done")

例如,如果您只想复制值,则可以使用 service.spreadsheets().values().update(),如下所示。

request = service.spreadsheets().values().get(spreadsheetId=from_spreadsheet_id, range=from_sheet + "!" + from_column + ":" + from_column_till)
response = request.execute()["values"]
range_ = "Working Analysis!G2"
request = service.spreadsheets().values().update(spreadsheetId=to_spreadsheet_id, range=range_, valueInputOption="USER_ENTERED", body={"values": response})
response = request.execute()
return print("Done")

参考文献: