0

I intend to update my bound script (destination) with contents from another script (source). I am trying the following code. It does not give any error, but do not modify the destination script either.

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);
  }
}

Am i missing something?

Tanaike
  • 181,128
  • 11
  • 97
  • 165
Partha S. Pal
  • 169
  • 1
  • 2
  • 14
  • Is your script the latest one? Because I think that when the project is saved after your script was copied and pasted, an error occurs. And also ``srcName`` is not used. If you have the other latest script, could you please update your question? And also, if you modified the Manifests file (appsscript.json), please provide the information. – Tanaike Jan 23 '19 at 00:13

1 Answers1

0

Excuse me for the erroneous script. I modified the script little bit and now its working as desired. However it seems I have to run the script twice to get the desired result. Below is the working script.

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());

}
Partha S. Pal
  • 169
  • 1
  • 2
  • 14