簡體   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