3
votes

I've seen many answers about how to use scripts to copy sheets to another Spreadsheet in Google Spreadsheets, such as this copyTo method.

But now I have a big spreadsheet with many tabs, and I created a new version of it with updates in the bound scripts. The data in this new sheet is test data. So this is what I have:

  • Old spreadsheet file (consistent data) / Old script project (obsolete)
  • New spreadsheet (test data) / New script project (current)

I need then to copy the new script project to the old spreadsheet (which has the consistent data).

I know I could make an effort to copy each sheet instead, but that is really not the question here (besides also creating lots of trouble with named ranges). The question is how to copy the script project to another spreasheet.

4
Possibly related to issue 2922Bryan P

4 Answers

4
votes

I think the Google Clasp tool can help you automate this. I'm starting to use it and it's promising. For instance you may issue a clasp clone [scriptId] of both old and new projects on different folders. Then you can copy all your new scripts files to the old folder and then issue a clasp push. But since I'm not a clasp expert I suggest you tests this on a proof of concept :)

Edit (2019-06-21): After using the product for several months I confirm that this approach works very well.

4
votes

Are you looking for the solution of this question yet? If you still do, how about this answer? Recently, Google Apps Script API was added. By this, users got to be able to easily manage GAS project files. This sample script uses this API.

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

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.

By these flow, the project (bound script) of source spreadsheet is copied to the destination spreadsheet.

Sample script :

Before launch this sample, please input the project ID of source spreadsheet and the destination spreadsheet ID.

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

  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 sceipt, at first, please test using a new project and new Google Docs. By this, please understand the work of this script.

References :

If I misunderstand your question, I'm sorry.

Updated: July 4th, 2019

At April 8, 2019, the specification of Google Apps Script Project was changed. Please see the detail at Google Cloud Platform Projects. When you use Google Apps Script API with the GAS project created after At April 8, 2019, please create a new Cloud Platform Project and link the GAS project to the created Cloud Platform Project. (At script editor, Resources --> Cloud Platform project) By this, the above flow can be used.

2
votes

There appears to be no direct way to "Copy" the script from on spreadsheet to another But you can assign whatever triggers you want to the second spreadsheet from the script in the first one (or from anywhere actually) using Installable Triggers

e.g. you have an onEdit(e) trigger in the first sheet that does some stuff, you can easily apply it to the second spreadsheet by using Script App like

var ss = SpreadsheetApp.openById(secod_spreadsheet_id); //or whatever way you like
ScriptApp.newTrigger('onEdit')  //or whatever its name is ...
  .forSpreadsheet(ss)
  .onEdit()  //or onCreate() or onChange()
  .create();

and this will add the trigger to your second sheet, but you must read the restrictions and Limitaions for the installable triggers as they will always run on your account (see explaination in links)

Last word,
If you know previously that you are going to use many sheets you might want to make a Standalone script with all your triggers and assign installable triggers to your sheets

Also note that these triggers can't be edited only removed and re-assigned from each sheet using a loop or something like that

You might also want to check out This question

0
votes

Just go to the script editor old spreadsheet and delete the script (or comment it out). Then open the script editor of the new spreadsheet and copy the script. Paste the script in the old spreadsheets script editor. Save it and you should be good to go.