【问题标题】:python gspread - How to delete/remove column in google-sheet-APIpython gspread - 如何删除/删除 google-sheet-API 中的列
【发布时间】:2019-08-10 20:43:50
【问题描述】:

我正在使用带有 values_update() 的 python 中的 gspread 将 CSV 文件中的数据记录到谷歌表格。

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

创建背景颜色后,我想删除 C 列,包括标题(第 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')???

【问题讨论】:

    标签: python google-sheets-api gspread


    【解决方案1】:
    • 您想使用 gspread 删除列。
    • 您已经能够使用 Sheets 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”列被删除。

    参考资料:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2019-01-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多