【问题标题】:How to rename copy of a bound script programatically?如何以编程方式重命名绑定脚本的副本?
【发布时间】: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


【解决方案1】:

看来我还是可以通过DriveApp访问原来的绑定脚本

代码:

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

原文:

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

复制文件:

结论:

  • 我还没有找到任何可能的方法来在复制/复制绑定脚本后自动重命名它。此外,目前还没有任何方法可以让您获取绑定到文件的脚本的 ID。
  • 如果上面的代码对您来说即使是原始代码也无法正常工作,那就更难了。

资源:

【讨论】:

  • 你的脚本绑定和默认 GCP 项目吗?
  • 对@Radek 都同意。如您所见,SpreadsheetApp.getActiveSpreadsheet().getId() 正在返回一个 ID。
  • 如果这是独立脚本,@Radek, SpreadsheetApp.getActiveSpreadsheet().getId() 将导致 TypeError: Cannot read property 'getId' of null
  • 好吧,那么由于其他原因,它对我不起作用......我基本上正在使用
  • 你是对的。我可以确认问题出在电子表格的副本上。绑定的脚本然后表现得很奇怪。
猜你喜欢
  • 2020-01-27
  • 1970-01-01
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2011-05-05
  • 2011-04-28
  • 2011-10-23
相关资源
最近更新 更多