Copying and Overwriting GAS Project

Gists

Pattern 1

This is a sample script for copying GAS project to a container-bound script of Google Docs (Spreadsheet, Document and Form (and Slides)). The project is created as a new project.

In order to use this sample, please do the following installation flow.

If you use this sample script, at first, please test using a new project and new Google Docs. By this, please understand the work of this script.

Installation :

Flow of script

  1. Retrieve filename of source project.
  2. Retrieve source project.
  3. Create new project (bound script) in the Google Docs.
  4. Update the created new project by retrieved source project.

Sample script 1 :

function main() {
  var srcProjectId = "### project ID ###"; // Source project ID
  var dstGoogleDocsId = "### file ID of Google Docs ###"; // Destination file ID of Google Docs

  var baseUrl = "https://script.googleapis.com/v1/projects";
  var accessToken = ScriptApp.getOAuthToken();

  // Retrieve filename of bound-script project.
  var srcName = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + srcProjectId, {
    method: "get",
    headers: {"Authorization": "Bearer " + accessToken}
  }).getContentText()).title;

  // Retrieve bound-script project.
  var obj = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
    method: "get",
    headers: {"Authorization": "Bearer " + accessToken}
  }).getContentText();

  // Create new bound script and retrieve project ID.
  var dstId = JSON.parse(UrlFetchApp.fetch(baseUrl, {
    method: "post",
    contentType: 'application/json',
    headers: {"Authorization": "Bearer " + accessToken},
    payload: JSON.stringify({"title": srcName, "parentId": dstGoogleDocsId})
  }).getContentText()).scriptId;

  // Upload a project to bound-script project.
  var res = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + dstId + "/content", {
    method: "put",
    contentType: 'application/json',
    headers: {"Authorization": "Bearer " + accessToken},
    payload: obj
  }).getContentText());
}

Note :

Pattern 2

This is a sample script for overwriting GAS project to the existing GAS project. The project is NOT created as a new project. The existing project is overwritten by the source project. So when you use this sample script, please be careful.

If you use this sample script, at first, please test using a new project and new Google Docs. By this, please understand the work of this script.

In order to use this sample, please do the above installation flow.

Sample script 2 :

function main() {
  var srcProjectId = "### project ID ###"; // Source project ID
  var dstProjectId = "### project ID ###"; // Destination project ID

  var baseUrl = "https://script.googleapis.com/v1/projects";
  var accessToken = ScriptApp.getOAuthToken();

  // Retrieve bound-script project.
  var obj = UrlFetchApp.fetch(baseUrl + "/" + srcProjectId + "/content", {
    method: "get",
    headers: {"Authorization": "Bearer " + accessToken},
    muteHttpExceptions: true
  }).getContentText();

  // Upload a project to bound-script project.
  var res = JSON.parse(UrlFetchApp.fetch(baseUrl + "/" + dstProjectId + "/content", {
    method: "put",
    contentType: 'application/json',
    headers: {"Authorization": "Bearer " + accessToken},
    payload: obj,
    muteHttpExceptions: true
  }).getContentText());
}

References :

 Share!