Add script to spreadsheet using a different script

57 views
Skip to first unread message

Jacob Deremer

unread,
Sep 20, 2020, 11:18:39 AM9/20/20
to Google Apps Script Community
I have a script that I plan to use on multiple spreadsheets. I would like to be able to run a separate script to add the script to the sheets automatically instead of having to go in and add it manually

Jean-Luc Vanhulst

unread,
Sep 20, 2020, 7:41:17 PM9/20/20
to google-apps-sc...@googlegroups.com
Deploy as an add-on!

On Sun, Sep 20, 2020 at 11:18 AM Jacob Deremer <jder...@wildsidesmoke.com> wrote:
I have a script that I plan to use on multiple spreadsheets. I would like to be able to run a separate script to add the script to the sheets automatically instead of having to go in and add it manually








--


You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.


To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.


To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/3f3c53bf-a820-4f9a-a3ca-277d3172df5fo%40googlegroups.com.


--

Alan Wells

unread,
Sep 20, 2020, 7:57:03 PM9/20/20
to Google Apps Script Community
The Apps Script API can update an existing script.

Whether using the API is a good idea or not, depends on how much work it would be to learn and write the code,
versus other options.

Your other options are:
Have a Sheets template. Copy the template which will also copy the bound project.
Use a library.  You still need to add the library to the bound project, and then add code to run the library.
Publish an add-on.  You'll need to provide graphics and meet requirements even to publish a private add-on.

Some of the things that the Apps Script API can do:

 - Create a new Apps Script file with a base manifest file.  https://developers.google.com/apps-script/api/reference/rest/v1/projects/create
 - Put content into an existing Apps Script file.  Including updating the appsscript.json file: https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent

An Apps Script project can be used as the basis for different things:

 - A Web App
 - An Add-on - Editor Add-on or G Suite
 - A library
 - API Executable
 - Bound to a document (Sheet, Form, Doc, Slides)

How the Apps Script file behaves is controlled through the manifest file.

The Apps Script API can be used from an Apps Script project by using the REST API and `UrlFetchApp.fetch(url,options)`

You don't need to use an OAuth library to do this from Apps Script.  An
access token can be obtained with:

    var accessTkn = ScriptApp.getOAuthToken()

The structure of the content to update the Apps Script file is described at:
https://developers.google.com/apps-script/api/reference/rest/v1/projects/updateContent#request-body

An example of the code to overwrite an existing Apps Script file is:

    function overWriteAppsScriptFile_(scriptId,content,theAccessTkn) {
    try{
      var options,payload,response,url;
    
      if (!content) {
        throw new Error('failed to pass in content');
        return;
      }
      
      if (!theAccessTkn) {
        theAccessTkn = ScriptApp.getOAuthToken();
      }
      
      url = "https://script.googleapis.com/v1/projects/" + scriptId + "/content";
    
      options = {
        "method" : "PUT",
        "muteHttpExceptions": true,
        "headers": {
          'Authorization': 'Bearer ' +  theAccessTkn
         },
        "contentType": "application/json",//If the content type is set then you can stringify the payload
        "payload": JSON.stringify(content)
      };
      
      response = UrlFetchApp.fetch(url,options);
      
      //llz('typeof response 38',typeof response)
      //llz('response.getResponseCode() 39',response.getResponseCode())
      
      //llz('response 39',JSON.stringify(response).slice(0,45))
    
      return response;
    } catch(e) {
      myErrorHandling_(e,'Error updating Apps Script file');
      //llz('error',e.message)
      //llz('stack',e.stack)
    }
    };

Andrew Roberts

unread,
Sep 21, 2020, 4:29:36 AM9/21/20
to google-apps-sc...@googlegroups.com
Create a library and have a minimal bound script in the GSheet or, more involved, create a private add-on

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.

CBM Services

unread,
Sep 21, 2020, 1:14:23 PM9/21/20
to google-apps-sc...@googlegroups.com
Another idea for you yo consider depending on your situation.

A script can operate on any spreadsheet that you have access to. It does not need to be tied to that spreadsheet in any way. All you need is to be able to provide the spreadsheet id to the script.

From: Andrew Roberts
Sent: ‎2020-‎09-‎21 1:29 AM
To: google-apps-sc...@googlegroups.com
Subject: Re: [Apps-Script] Re: Add script to spreadsheet using a differentscript

Reply all
Reply to author
Forward
0 new messages