【发布时间】:2021-09-22 04:10:42
【问题描述】:
我想定期备份在 Google 电子表格上编码的项目 (default GCP project)。最重要的是它是原始电子表格的副本。所以项目脚本文件是bound到电子表格。
Bound scripts generally behave like standalone scripts except that they do not appear in Google Drive 来自上述文档链接的引用。
我猜这就是我收到错误的原因
Error GoogleJsonResponseException: API call to drive.files.get failed with error: File not found:
12OX6dRqsEHRsR4MttkDQ71yW_I8R2UqjfcSq4FB backupSS @ web functions.gs:3958
在下面代码的第 4 行。注意scriptId存在但文件不可访问
var spreadSheetId = SpreadsheetApp.getActiveSpreadsheet().getId()
var scriptId = ScriptApp.getScriptId()
console.log(Drive.Files.get(spreadSheetId))
console.log(Drive.Files.get(scriptId))
有了这个结果
9:24:12 PM Notice Execution started
9:24:12 PM Info PR-Digitalizace objednávek
9:24:12 PM Error GoogleJsonResponseException: API call to drive.files.get failed with error: File not found: 12OX6dRPKwwt1-vOtCsbDEHRsR4MttkDQ71yW_I8R2Uqq4FB
backupSS @ web functions.gs:3958
console.log(DriveApp.getFileById(scriptId).getName()) givess me this error `Error
Exception: No item with the given ID could be found. Possibly because you have not edited this item or you do not have permission to access it.
backupSS @ web functions.gs:3959`
有没有办法以编程方式编辑脚本名称?为什么我没有很多同名的脚本。
更新
-
Google Apps Script API 开启
-
请求的身份验证范围不足。
function getScriptContent(scriptId,content,theAccessTkn) { 尝试{ var options,payload,response,url;
if (!scriptId) { scriptId = ScriptApp.getScriptId() } if (!content) { //Error handling function console.log("no content") } if (!theAccessTkn) { theAccessTkn = ScriptApp.getOAuthToken(); } //https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent url = "https://script.googleapis.com/v1/projects/" + scriptId +"content" url = "https://scriptmanagement.googleapis.com/v1/projects/" + scriptId //+"content" options = { "method" : "GET", "muteHttpExceptions": true, "headers": { 'Authorization': 'Bearer ' + theAccessTkn }, "contentType": "application/json",//If the content type is set then you can stringify the payload // "payload": JSON.stringify(content) }; response = UrlFetchApp.fetch(url,options); console.log('getResponseCode ' + response.getResponseCode()) console.log("Response content: " + response.getContentText()) console.log("finished " ) } catch(e) { console.log("Error: " + e + "\nStack: " + e.stack) }};
给我
Response content: {
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
【问题讨论】:
-
您可以尝试使用 Apps Script API。它允许您管理文档绑定脚本。请参阅文档:developers.google.com/apps-script/api
-
您是在重命名“Untitled project”还是“Code.gs”?
-
@NaziA:这个项目已经有了名字。但我想复制电子表格。在这种情况下,项目被克隆,然后我得到了两个同名的项目。如果我每天都创建备份,那么 ....
-
@Radek,我明白了。我不确定为什么你的不起作用,因为我没有从上面的 sn-p 中得到任何错误。虽然如果这仍然不起作用,你可以试试
DriveApp.getFileById(scriptId).setName('Renamed project')吗?它对我有用。只需确保在运行后刷新它以查看更改。 -
我不会工作 .. 我的脚本已绑定。从电子表格创建脚本并尝试。
标签: google-apps-script google-cloud-platform