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 :

  • Enable Google Apps Script API at API console.
    • If you have already opened the script editor, you can access there by this link.
  • Retrieve current scopes.
    • On script editor, File -> Project properties -> Scopes
    • Copy scopes.
  • Add a scope of https://www.googleapis.com/auth/script.projects and https://www.googleapis.com/auth/script.external_request to the Manifests file (appsscript.json) of the script editor.
    • On script editor, View -> Show manifest file
    • Add "oauthScopes": ["https://www.googleapis.com/auth/script.projects", "https://www.googleapis.com/auth/script.external_request", "### other scopes ###"] to appsscript.json, and save.
      • If your script needs other scopes, please add them here.
  • Copy and paste this sample script, and run. And please authorize.

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 :

  • This script copies the source project as a new project. So if there are some bound script projects in the Google Docs, this script does NOT affect the existing script.
    • But, 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.
  • For the Slides, I noticed that the bound script cannot be created in only Slides. So I have reported this at here.

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!