【问题标题】:Google Apps Standalone Script to use Google Apps Script API to update many bound scriptsGoogle Apps Standalone Script 使用 Google Apps Script API 更新许多绑定脚本
【发布时间】:2018-10-01 14:00:39
【问题描述】:

我正在尝试编写一个独立的 Google Apps 脚本,该脚本使用 Google Apps Script API 更新许多 Google 表格的绑定脚本内容。

我拥有从模板创建的大约 200 个 Google 表格的表格 ID。我想将这些工作表上的绑定脚本的项目内容更新为与一组主脚本相同。

在使用 urlFetchApp 获取一张纸的绑定脚本的内容作为测试时,我遇到了身份验证错误。错误看起来像:

Request failed for
https://script.googleapis.com/v1/projects/<SCRIPTID>/content returned code 401. 
Truncated server response: { "error": { "code": 401, 
"message": "Request is missing required authentication credential.
Expected OAuth 2 access token, login cookie ... 
(use muteHttpExceptions option to examine full response) (line 34, file "AddScriptsToSheets")

我正在使用的测试函数如下所示:

function getSheetScriptContent(sheetId) { 
  var sheet = SpreadsheetApp.openById(sheetId);
  // Make a POST request with a JSON payload.
  // Make a GET request and log the returned content.
  var url = PROJECTS_GET_CONTENT_URL.format(sheetId);
  var response = UrlFetchApp.fetch(url);
  Logger.log(response.getContentText());
}

我认为这个OAuth2 library 在这种情况下可能有用,我只是不确定如何使用它。谁能指出我正确的方向?

【问题讨论】:

标签: google-apps-script google-apps-script-api


【解决方案1】:

如果您拥有所有文件,则无需使用 OAuth 库或任何特殊代码来获取访问令牌。您可以从ScriptApp 类中获取访问令牌。

var theAccessTkn = ScriptApp.getOAuthToken();

您可能需要手动编辑 appsscript.json 清单文件并添加范围:

https://www.googleapis.com/auth/script.projects

appsscript.json

{
  "timeZone": "Yours will display here",
  "dependencies": {
  },
  "webapp": {
    "access": "MYSELF",
    "executeAs": "USER_DEPLOYING"
  },
  "exceptionLogging": "STACKDRIVER",
  "oauthScopes": [
    "https://www.googleapis.com/auth/drive", 
    "https://www.googleapis.com/auth/script.projects", 
    "https://www.googleapis.com/auth/drive.scripts", 
    "https://www.googleapis.com/auth/script.container.ui", 
    "https://www.googleapis.com/auth/script.external_request"
  ],
  "runtimeVersion": "DEPRECATED_ES5"
}

覆盖 Apps 脚本文件的代码:

function updateContent(scriptId,content,theAccessTkn) {
try{
  var options,payload,response,url;

  if (!content) {
    //Error handling function
    return;
  }
  
  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";

  options = {
    "method" : "PUT",
    "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);      
  //Logger.log('getResponseCode ' + response.getResponseCode())
  //Logger.log("Response content: " + response.getContentText())

} catch(e) {
  console.log("Error: " + e + "\nStack: " + e.stack)
}
};

【讨论】:

  • 谢谢!我对此进行了一些修改,效果很好。我必须明确添加“googleapis.com/auth/script.projects”范围。最后,我有我制作的所有工作表的工作表 ID。有没有办法从每个工作表 ID 中获取脚本 ID?
  • @jcc parentId 确定哪个工作表是绑定父级,但没有 projects.list 方法来搜索绑定和独立脚本项目,您是 SOL A search query q parentId in 'csv of spreadsheetIds'正是你需要的developers.google.com/apps-script/guides/… (#6) developers.google.com/apps-script/guides/…
  • 我想我可以遍历每个工作表 ID 并以工作表 ID 作为父 ID 调用 projects.create。没关系,因为无论如何我都会覆盖内容。我今晚试试,然后在这里报告。 projects.create
  • @jcconnell 您可能会在第一次运行时遇到重新授权问题(因为每个项目现在都是一个新的 Google Cloud Platform 项目),因此在有人授权脚本运行之前触发器可能无法工作。我不相信有办法以编程方式更改 Apps 脚本项目的 GCP 项目。请务必存储关联的scriptIds 以供将来使用!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-25
  • 2015-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 1970-01-01
相关资源
最近更新 更多