本文介绍了通过gspread Python(例如sheet.delete_row)在Google工作表中删除(删除)列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gspread谷歌工作表中是否有类似worksheet.delete_row的方法?我试过了:

Is there a method like worksheet.delete_row in gspread google-sheet?I tried:

delete = sheet.range('A1:A1000')
        for cell in delete:
            cell.value = ""
            sheet.update_cells(delete)

但这仅删除所有值,而不删除列.博德可以帮我吗?

but that only delete all values, not column.Can anybode help me?

推荐答案

答案:

gspread 中没有方法可以删除整个列,例如 Workbook.delete_row ,但是您可以通过批量更新来做到这一点.

Answer:

There is no method in gspread to delete an entire column, like Workbook.delete_row, however you can do this with a batch update.

spreadsheetId = "your-spreadsheet-id"
sheetId = "id-of-sheet-to-delete-column-from"

sh = client.open_by_key(spreadsheetId)

request = {
    "requests": [
        {
            "deleteDimension": {
                "range": {
                    "sheetId": sheetId,
                    "dimension": "COLUMNS",
                    "startIndex": 0,
                    "endIndex": 1
                }
            }
        }
    ]
}
result = sh.batch_update(request)

此示例将删除列A,但请确保将 startIndex endIndex 更改为您要删除的列范围.

This sample will delete column A, but make sure to change the startIndex and endIndex to be of the column range you wish to delete.

修改:如果您不知道给定工作表的 sheetId ,则可以使用以下方法获取它:

If you do not know the sheetId of a given sheet, you can get it using the following:

sheetName = "theSheetName"
sheetId = sh.worksheet(sheetName)._properties["sheetId"]

请注意,电子表格的原始表不需要这样做,因为 总是 0 .

Note that this is not needed for the original sheet of a Spreadsheet, as this will always be 0.

希望对您有帮助!

  • Method: spreadsheets.batchUpdate | Sheets API | Google Developers
  • API References - gspread 3.4.0 documentation - batch_update(body)

此脚本今天从拉请求#中与 gspread master合并.759 作为方法 delete_column().

This script was merged with gspread master today from pull request #759 as method delete_column().

用于 delete_columns()的方法也作为并行方法从拉动请求#761 .

A method for delete_columns() was also added as a parallel method to the existing delete_rows() from pull request #761.

这篇关于通过gspread Python(例如sheet.delete_row)在Google工作表中删除(删除)列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 02:32