[英]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. 示例脚本如下。
sheet = service.spreadsheets()
request = sheet.get(
spreadsheetId=spreadsheetId,
fields="sheets(properties(sheetId,title))")
response = request.execute()
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.