Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python gspread删除一系列单元格(向上)_Python_Gspread - Fatal编程技术网

Python gspread删除一系列单元格(向上)

Python gspread删除一系列单元格(向上),python,gspread,Python,Gspread,使用Python的GSpread包,如何使用批处理更新删除一系列单元格 示例代码: sheet_id = self.worksheet.gs_worksheet._properties['sheetId'] start_index_col = self.cell_data_by_row_col[0][0].col - 1 end_index_col = self.cell_data_by_row_col[0][-1].col - 1

使用Python的GSpread包,如何使用批处理更新删除一系列单元格

示例代码:

        sheet_id = self.worksheet.gs_worksheet._properties['sheetId']
        start_index_col = self.cell_data_by_row_col[0][0].col - 1
        end_index_col = self.cell_data_by_row_col[0][-1].col - 1
        start_index_row = self.cell_data_by_row_col[0][0].row
        end_index_row = self.cell_data_by_row_col[0][0].row

        self.worksheet.gs_worksheet.batch_update({
            'requests': [
                {
                    'deleteRangeRequest': {
                        'range': {
                            'sheetId': sheet_id,
                            'startRowIndex': start_index_row,
                            'endRowIndex': end_index_row,
                            'startColumnIndex': start_index_col,
                            'endColumnIndex': end_index_col,
                        },
                        'shiftDimension': 'ROWS',
                    }
                }
            ]
        })
答复:

  File "C:\Program Files\Python37\lib\site-packages\gspread\utils.py", line 559, in wrapper
    return f(*args, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\gspread\models.py", line 1166, in batch_update
    for vr in data
  File "C:\Program Files\Python37\lib\site-packages\gspread\models.py", line 1166, in <listcomp>
    for vr in data
TypeError: string indices must be integers

包装器中的文件“C:\Program Files\Python37\lib\site packages\gspread\utils.py”,第559行 返回f(*args,**kwargs) 文件“C:\Program Files\Python37\lib\site packages\gspread\models.py”,第1166行,在批更新中 数据中的虚拟现实 文件“C:\Program Files\Python37\lib\site packages\gspread\models.py”,第1166行,在 数据中的虚拟现实 TypeError:字符串索引必须是整数
我相信你的目标如下

  • 您想使用Sheets API中“spreadsheets.batchUpdate”方法的
    deleteRangeRequest
    删除范围
  • 您希望使用gspread和python来实现这一点
修改点:
  • 在gspread,似乎
    batch\u update(body)
    是类
    gspread.models.Spreadsheet
    的方法。在您的脚本中,我认为您可以使用它作为class
    gspread.models.Worksheet
    的方法。我认为这就是
    字符串索引必须是整数的错误消息的原因。
    
    • batch_update(数据,**kwargs)
      类的
      gspread.models.workeep
      是“电子表格.值.批更新”的方法
  • 在“电子表格.批处理更新”方法中使用DeleteRangeRequest时,请将其用作
    deleteRange
当上述点反映到脚本中时,它将变成如下所示。不幸的是,从您的脚本中,我无法理解
self.sheet.gs\u sheet
的变量。所以在这个修改中,我使用了另一个变量名

修改脚本: 参考资料:

好的,我修正了请求的语法,它通过了,但什么也没做。命令必须是“deleteRange”。谢谢,回答得很好。批处理请求已通过,但没有更新,没有错误,只是响应中没有更新。@Carl Von感谢您的回复。给您带来不便,我深表歉意。在这种情况下,当选中
res=spreadsheet.batch\u update({,,})
res
时,您将获得什么值?和/或,例如,当
start\u index\u col、end\u index\u col、start\u index\u row、end\u index\u row
分别为
0、1、0、1
时,仅删除单元格“A1”?
spreadsheetId = "###"  # Please set the Spreadsheet Id.
sheetName = "Sheet1"  # Please set the sheet name.

client = gspread.authorize(credentials)
spreadsheet = client.open_by_key(spreadsheetId)
sheet_id = spreadsheet.worksheet(sheetName)._properties['sheetId']

start_index_col = self.cell_data_by_row_col[0][0].col - 1
end_index_col = self.cell_data_by_row_col[0][-1].col - 1
start_index_row = self.cell_data_by_row_col[0][0].row
end_index_row = self.cell_data_by_row_col[0][0].row

spreadsheet.batch_update({
    'requests': [
        {
            'deleteRange': {
                'range': {
                    'sheetId': sheet_id,
                    'startRowIndex': start_index_row,
                    'endRowIndex': end_index_row,
                    'startColumnIndex': start_index_col,
                    'endColumnIndex': end_index_col,
                },
                'shiftDimension': 'ROWS',
            }
        }
    ]
})