Copy a google script project from a Spreadsheet to another

后端 未结 4 1919
既然无缘
既然无缘 2021-01-14 03:35

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 sprea

相关标签:
4条回答
  • 2021-01-14 04:02

    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 :

    • Google Apps Script API
    • Manifests

    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.

    • Flow for linking Cloud Platform Project to Google Apps Script Project
    0 讨论(0)
  • 2021-01-14 04:17

    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 讨论(0)
  • 2021-01-14 04:23

    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.

    0 讨论(0)
  • 2021-01-14 04:26

    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.

    0 讨论(0)
提交回复
热议问题