简体   繁体   English

如何通过Python使用SHEET API v4删除Google电子表格中的行?

[英]How to delete rows in google spreadsheet using SHEET API v4 by Python?

My code is the following 我的代码如下

def write_cells(spreadsheet_id, update_data):
updating = sheet_service.spreadsheets().values().\
        batchUpdate(spreadsheetId=spreadsheet_id, body=update_data)
updating.execute()

spreadsheet_data = [
    { 
        "deleteDimension": {
        "range": {
          "sheetId": sheet_id,
          "dimension": "ROWS",
          "startIndex": 5,
          "endIndex": 100
         }              
        }
       }
]    

update_spreadsheet_data = {
    'valueInputOption': 'USER_ENTERED',
    'data': spreadsheet_data
}

update_data = update_spreadsheet_data

write_cells(spreadsheet_id, update_data)

I have the following error message 我有以下错误信息

HttpError                                 Traceback (most recent call last)
<ipython-input-64-0ba8756b8e85> in <module>()
----> 1 write_cells(spreadsheet_id, update_data)

2 frames 2帧

/usr/local/lib/python3.6/dist-packages/googleapiclient/http.py in execute(self, http, num_retries)
    838       callback(resp)
    839     if resp.status >= 300:
--> 840       raise HttpError(resp, content, uri=self.uri)
    841     return self.postproc(resp, content)
    842 

HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1lAI8gp29luZDKAS1m3P62sq0kKCn8eaMUvO_M_J8meU/values:batchUpdate?alt=json returned "Invalid JSON payload received. Unknown name "delete_dimension" at 'data[0]': Cannot find field.">

I don't understand this: "Unknown name delete_dimension". 我不明白这一点:“未知名称delete_dimension”。 I'm unable to resolve it. 我无法解决。 Any help is appreciated, thanks. 任何帮助表示赞赏,谢谢。

  • You want to delete rows using Sheets API with Python. 您想要使用带有Python的Sheets API删除行。

If my understanding is correct, how about this modification? 如果我的理解是正确的,那么该修改如何?

Modification points: 修改要点:

  • When delete rows in Spreadsheet, please use spreadsheets().batchUpdate() . 删除电子表格中的行时,请使用spreadsheets().batchUpdate()
  • I think that spreadsheet_data is correct. 我认为spreadsheet_data是正确的。
  • In this case, please modify the request body to {"requests": spreadsheet_data} . 在这种情况下,请将请求正文修改为{"requests": spreadsheet_data}

Modified script: 修改后的脚本:

def write_cells(spreadsheet_id, update_data):
    # Modified
    updating = sheet_service.spreadsheets().batchUpdate(
        spreadsheetId=spreadsheet_id, body=update_data)
    updating.execute()


spreadsheet_data = [
    {
        "deleteDimension": {
            "range": {
                "sheetId": sheet_id,
                "dimension": "ROWS",
                "startIndex": 5,
                "endIndex": 100
            }
        }
    }
]

update_spreadsheet_data = {"requests": spreadsheet_data} # Modified

update_data = update_spreadsheet_data
write_cells(spreadsheet_id, update_data)

Note: 注意:

  • This modified script supposes that you have already been able to use Sheets API. 此修改后的脚本假定您已经能够使用Sheets API。

Reference: 参考:

If I misunderstood your question and that was not the result you want, I apologize. 如果我误解了您的问题,而那不是您想要的结果,我深表歉意。

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

相关问题 使用谷歌表 API V4 中的 python 将特定单元格格式化为最新 - Format specific cell to date with python in google sheet API V4 使用 Python (API v4) 从 Google 表格中读取单元格格式 - Read cell format from Google sheet using Python (API v4) 谷歌表格 API v4 删除未清除行 - Google Sheets API v4 Delete not Clear Row 如何从Google Analytics Reporting API v4 Python获取前50个会话 - How to get Top 50 Sessions from Google Analytics Reporting API v4 Python Google Analytics API v4 如何仅过滤目标 &gt; 0 - Google Analytics API v4 how to filter only for goals > 0 谷歌电子表格 api 使用 python 批量更新 - Google spreadsheet api batchupdate using python 如何使用 gspread 删除完整的电子表格,python - How to delete the complete spreadsheet using gspread, python 如果我想在本地下载一个Google表格到CSV,我需要为Google API v4定义“redirect_uris”吗? - If I want to download a Google sheet to CSV locally, do I need "redirect_uris" defined for Google API v4? 将变量传递给 batchGet() 请求 - 谷歌分析报告 API v4 / Python / 循环 - Passing a variable to a batchGet() request - Google Analytics Reporting API v4 / Python / loop 使用API​​使用Python3创建的Google电子表格无法打开(权限) - Created google spreadsheet with Python3 using the API does not open (permissions)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM