python gspread - 如何在 google-sheet-API 中 delete/remove 列

python gspread - How to delete/remove column in google-sheet-API

我正在使用 python 中的 gspread 和 values_update() 将数据从 CSV 文件记录到 google sheet。

然后我使用 gspread-formatting 创建背景颜色。由于我还没有找到从 CSV 中格式化颜色的方法,我的脚本读取 C 列中的数据,我在其中存储了我想要使用的颜色。

创建背景色后,我想删除 C 列,包括 header(第 1 行)

删除或移除整列的最佳方法是什么? 或者,如果有一种方法可以直接从 CSV 文件中记录背景颜色,那会更好。

projectSheet.values_update(
worksheet, params={'valueInputOption': 'USER_ENTERED'},
body={'values': list(csv.reader(open(csvName)))}
)

blue = [cell.row for cell in worksheet.findall('Blue')]
for i in blue:
    fmtBlue = cellFormat(
    backgroundColor=color(0.5, 0.5, 1),
    textFormat=textFormat(bold=False, foregroundColor=color(0, 0, 0)),
    horizontalAlignment='CENTER'
    )
    rows = f'A{i}:J{i}'
    format_cell_range(worksheet, rows, fmtBlue)

worksheet.delete_column('C')???
  • 您想使用 gspread 删除列。
  • 您已经能够使用表格输入和获取值 API。
  • 您的问题如下。
    • 删除或移除整列的最佳方法是什么?
    • 如果有一种方法可以直接从 CSV 文件中记录背景颜色,那就更好了。

如果我的理解是正确的,这个示例脚本怎么样?不幸的是,我无法理解 If there is a way to log the background color straight from the CSV file.。所以我无法回答。但我可以回答大约 What is the best way to delete or remove an entire column?。所以在这里,我想提出使用gspread删除列的方法。

gspread里面好像没有delete_column的方法。所以在这种情况下,我想建议使用gspread的batch_update()。请将此视为几个答案之一。

示例脚本:

spreadsheetId = "###"  # Please set Spreadsheet ID.
sheetName = "###"  # Please set sheet name which has the columns you want to delete.

spreadsheet = client.open_by_key(spreadsheetId)
sheetId = spreadsheet.worksheet(sheetName)._properties['sheetId']
body = {
    "requests": [
        {
            "deleteDimension": {
                "range": {
                    "sheetId": sheetId,
                    "dimension": "COLUMNS",
                    "startIndex": 2,
                    "endIndex": 3
                }
            }
        }
    ]
}
res = spreadsheet.batch_update(body)
print(res)
  • 请将范围设置为GridRange。
  • 在上面的脚本中,"C" 列被删除。

参考文献:

如果我误解了你的问题,这不是你想要的结果,我深表歉意。