繁体   English   中英

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

[英]gspread delete a range of cells (up)

使用 Python 的 GSpread 包,我将如何使用 batch_update 向上删除一系列单元格?

示例代码:

        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

我相信你的目标如下。

  • 您想使用 Sheets API 中“spreadsheets.batchUpdate”方法的deleteRangeRequest删除范围。
  • 您想使用 gspread 和 python 来实现这一点。

改装要点:

  • 在 gspread, batch_update(body)似乎是类gspread.models.Spreadsheet的方法。 在您的脚本中,我认为您可以将它用作类gspread.models.Worksheet的方法。 我认为这是string indices must be integers的错误消息string indices must be integers
    • batch_update(data, **kwargs)类的gspread.models.Worksheet是“spreadsheets.values.batchUpdate”的方法。
  • 在“spreadsheets.batchUpdate”方法中使用deleteRange ,请将其用作deleteRange

当上面的点反映到脚本中时,它变成如下。 不幸的是,从您的脚本中,我无法理解self.worksheet.gs_worksheet的变量。 所以在这次修改中,我使用了其他变量名。

修改后的脚本:

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',
            }
        }
    ]
})

参考:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM