我们继承了相当多的Google脚本项目,这些项目来自我们客户之前的开发人员。Apps脚本是通过嵌入式小部件部署在Google (sites.google.com)上的。每当我们需要在其中一个项目上开展工作时,我们就能够通过以下方式找到该项目:
这是一个相当繁琐的过程,但到目前为止已经奏效了。
其中一个小工具开始显示“需要授权.”当通过sites.google.com访问时,我们需要跟踪它所属的项目。我们完成了步骤1-3 (上面),但是我们找不到任何具有与该小工具匹配的URL的项目。
我的直觉是,组织中的其他人(不是开发人员的帐户)拥有这个项目,但可能有5到6个不同的人,他们中没有一个是开发人员,或者是技术上特别有头脑的人。另一种可能是开发人员帐户拥有该项目,但它的名称不太好,我并不兴奋地经历了5-7步几十次才能找到它。
有没有一种方法可以根据项目的URL来定位特定的项目?script.google.com的搜索工具似乎只搜索项目名称,不幸的是,这在本例中没有帮助。
发布于 2018-06-22 13:28:43
您可以使用Apps脚本API来获取脚本的“部署ID”。
首先,您需要使用DriveApp获取应用程序脚本项目文件的列表。然后,您需要遍历所有文件,获取文件ID,并使用文件ID获取部署信息。
每个项目都有一个部署列表。首先获取一个部署,然后从JSON对象获取部署Id。
要以我概述的方式使用Apps脚本API,您必须在appsscript.json清单文件中设置所需的作用域。
下面是设置应该如何显示的示例:
{
"timeZone": "America/New_York",
"dependencies": {
},
"webapp": {
"access": "ANYONE",
"executeAs": "USER_ACCESSING"
},
"exceptionLogging": "STACKDRIVER",
"oauthScopes": ["https://www.googleapis.com/auth/script.projects",
"https://www.googleapis.com/auth/drive.scripts",
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.scriptapp",
"https://www.googleapis.com/auth/script.deployments",
"https://www.googleapis.com/auth/script.deployments.readonly"]
}当您第一次运行代码时,您将得到一个授予权限的提示。但是,即使在您授权权限之后,您仍然需要转到开发人员控制台,并为项目启用Apps脚本API。
因此,您将从一个项目运行代码,以获得所有Apps脚本文件的列表,然后获取每个项目的部署,从部署中获取部署ID。
第一次运行代码时,请检查错误消息。例如,查看日志。您将在日志中看到如下所示的错误消息:
[18-06-22 08:51:32:841 EDT] response: {
"error": {
"code": 403,
"message": "Apps Script API has not been used in project abc123 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
"status": "PERMISSION_DENIED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.Help",
"links": [
{
"description": "Google developers console API activation",
"url": "https://console.developers.google.com/apis/api/script.googleapis.com/overview?project=abc123"
}
]
}
]
}
}将Url复制到开发人员仪表板,然后将其粘贴到浏览器地址栏中。在仪表板中,启用API。
下面是您需要使用的代码示例:
function searchForProjectWithCertainID() {
var files,params,projectID_toFind,rtrn,thisFileID;
projectID_toFind = "Put ID to find here";
//params = 'mimeType contains "json"';
//files = DriveApp.searchFiles(params);
files = DriveApp.getFilesByType(MimeType.GOOGLE_APPS_SCRIPT);//Get all Apps Script files
while (files.hasNext()) {
thisFileID = files.next().getId();
//Logger.log(thisFileID)
rtrn = getDeploymentID(thisFileID);
if (rtrn === projectID_toFind) {
break;
}
}
}
function getDeploymentID(scriptId) {
var errMsg,L,options,response,theAccessTkn,url;
theAccessTkn = ScriptApp.getOAuthToken();
url = "https://script.googleapis.com/v1/projects/" + scriptId + "/deployments";
options = {
"method" : "GET",
"muteHttpExceptions": true,
"headers": {
'Authorization': 'Bearer ' + theAccessTkn
}
};
response = UrlFetchApp.fetch(url,options);
Logger.log('response: ' + response)
response = JSON.parse(response);//The response must be parsed into JSON even though it is an object
L = response.deployments.length;
//Logger.log('response.deployments.length: ' + response.deployments.length)
if (typeof response === 'object') {
errMsg = response.error;
if (errMsg) {
errMsg = errMsg.message;
return 'err' + errMsg;
}
}
//Logger.log(response.deployments[L - 1].deploymentId);
return response.deployments[L - 1].deploymentId;
}关键词:应用程序脚本,项目ID,部署ID,应用程序脚本API,发布的URL
https://stackoverflow.com/questions/50986609
复制相似问题