Copy a google script project from a Spreadsheet to another

后端 未结 4 1920
既然无缘
既然无缘 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

提交回复
热议问题