我打算使用另一个脚本(源)的内容更新我的绑定脚本(目标)。我正在尝试以下代码。它不会给出任何错误,但也不会修改目标脚本。
function getScriptSourceCode() {
var srcProjectId = "id of source project"; // Source project ID
var destProjectId = "id of destination project"
var baseUrl = "https://script.googleapis.com/v1/projects";
var accessToken = ScriptApp.getOAuthToken()
var srcName = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + srcProjectId, {
method: "get",
headers: {"Authorization": "Bearer " + accessToken}
}).getContentText()).title;
var content = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
method: "get",
headers: {"Authorization": "Bearer " + accessToken}
}).getContentText();
var url = "https://script.googleapis.com/v1/projects/" + destProjectId
+ "/content";
var options = {
followRedirects: true,
"method" : "PUT",
"muteHttpExceptions": true,
"headers": {
'Authorization': 'Bearer ' + accessToken
},
"contentType": "application/json",
"payload": JSON.stringify(content)
}
var response = UrlFetchApp.fetch(url, options);
}
}
我错过了什么吗?
请原谅我的错误脚本。我稍微修改了脚本,现在它可以按预期工作。但是,似乎我必须运行脚本两次才能获得所需的结果。下面是工作脚本。
function UpdateScript()
{
var destProjectId = "Destination project ID"
var srcProjectId = "Source Project ID"; // Source project ID
var baseUrl = "https://script.googleapis.com/v1/projects";
var accessToken = ScriptApp.getOAuthToken()
var content = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
method: "get",
headers: {"Authorization": "Bearer " + accessToken}
}).getContentText();
// Upload a project to bound-script project.
var response = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + destProjectId + "/content", {
method: "put",
contentType: 'application/json',
headers: {"Authorization": "Bearer " + accessToken},
payload: content
}).getContentText());
}