简体   繁体   English

Google Spreadsheet API:plicateSheet:没有ID的工作表

[英]Google Spreadsheet API: duplicateSheet: No sheet with ID

I'm trying to duplicate a sheet using the duplicateSheet request using the batchUpdate() method: 我试图使用该复制的片材duplicateSheet使用请求batchUpdate()方法:

body['requests'] = [
    {
        "duplicateSheet": {
            "newSheetName": date_today(),
            "sourceSheetId": int(sheet_number)
        }
    }
]

sheet = service.spreadsheets()
request = sheet.batchUpdate(spreadsheetId=spreadsheetId,
                            body=requestBody)
response = request.execute()

But I'm getting this error message: 但我收到此错误消息:

googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxx_xxxxxxxx-xxxxxxx:batchUpdate?alt=json returned "Invalid requests[0].duplicateSheet: No sheet with id: 3">

But the sheet does have this sheet. 但是这张纸确实有这张纸。 Confirmed the sheet's ID by using this JS code: 通过使用以下JS代码确认工作表的ID:

SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getIndex();

Sheet ID which is used at "sourceSheetId" is gid . "sourceSheetId"处使用的工作表ID为gid The sheet ID can be retrieved by sheets.spreadsheets.get for Python and getSheetId() for GAS. 的片材ID可以通过检索sheets.spreadsheets.get Python和getSheetId()为GAS。 The sample scripts are as follows. 示例脚本如下。

Sample for Python Python示例

sheet = service.spreadsheets()
request = sheet.get(
    spreadsheetId=spreadsheetId,
    fields="sheets(properties(sheetId,title))")
response = request.execute()

Sample for GAS 气体样品

var res = [
  [i.getSheetId(), i.getSheetName()]
  for each (i in SpreadsheetApp.getActiveSpreadsheet().getSheets())
];

Ended up using this code (thanks to my colleague): 最终使用了这段代码(感谢我的同事):

meta = service.spreadsheets().get(spreadsheetId=SPREADSHEET_ID).execute()
sheets = meta.get("sheets", {})

for sheet in sheets:
    sheet_index = sheet.get("properties", {}).get("index")
    sheet_id = sheet.get("properties", {}).get("sheetId")
    if (sheet_index == index):
        return sheet_id

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

相关问题 在公司限制的电子表格上使用谷歌表 API 检索谷歌表值 - Retrieving google sheet values with google sheet api on company restricted spreadsheet 使用 Python 使用 Google API 将电子表格工作表复制到另一个工作表 - Copy spreadsheet sheet to a another one with Google API using Python 如何使用 Google 表格 API 在电子表格中创建新表格 - How to create a new sheet within a spreadsheet using Google Sheets API Google Spreadsheet API错误 - Google Spreadsheet API error 如何使用 python 和谷歌表格 API 从谷歌表格 ID 中获取谷歌表格名称? - How can I fetch Google Sheet name from Google sheet ID using python and google sheets API? 在 Python 中获取 google 电子表格 api v4 中的工作表和最新工作表列表 - Get list of sheets and latest sheet in google spreadsheet api v4 in Python Google API-电子表格 - Google API - Spreadsheet 如何使用 Python 中的 API 重命名 Google Sheets 电子表格中的(工作)工作表? - How do I rename a (work)sheet in a Google Sheets spreadsheet using the API in Python? 如何使用其API迭代解析Google电子表格中每个“子工作表”中的数据? - How do I iteratively parse data from each “sub-sheet” in a Google spreadsheet using their API? 在 Python 中连接 Google Sheet API - Connect Google Sheet API in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM