您可以使用 Apps Script API 来获取脚本的“部署 ID”。
首先,您需要使用DriveApp 来获取 Apps Script 项目文件的列表。然后需要遍历所有文件,获取文件ID,使用文件ID获取部署信息。
每个项目都有一个部署列表。先获取一个部署,然后从 JSON 对象中获取部署 Id。
要以我概述的方式使用 Apps Script 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 Script 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;
}
List Project Deployements
关键词:Apps Script、项目 ID、部署 ID、Apps Script API、发布 URL