0

我想定期备份在 Google 电子表格上编码的项目(默认 GCP 项目)。最重要的是它是原始电子表格的副本。所以项目脚本文件绑定到电子表格。

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 脚本 API 已启用

  • 请求的身份验证范围不足。

    function getScriptContent(scriptId,content,theAccessTkn) { try{ 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"
  }
}
4

1 回答 1

1

看来我仍然可以通过访问原始绑定脚本DriveApp

代码:

DriveApp.getFileById(scriptId).setName('tester');

原来的:

成功

但不是在复制的文件上运行它时

复制文件:

错误

结论:

  • 在复制/复制绑定脚本后,我还没有找到任何可能的方法来自动重命名绑定脚本。此外,目前没有任何方法可以让您获取绑定到文件的脚本的 ID。
  • 如果上面的代码对你来说即使在原始代码上也不起作用,那会让它变得更加困难。

资源:

于 2021-09-21T19:12:40.673 回答