How to use a google apps script in multiple documents

后端 未结 4 1062
心在旅途
心在旅途 2020-12-03 03:13

I have a google apps script that I want to use in multiple documents. I also may want to change it later in those documents, so it is imperative that I use the same

相关标签:
4条回答
  • 2020-12-03 03:44

    Sorry, my reputation was too low to add a comment. :( In Giuseppe's answer use this for find docs - minor change

    function findDoc(pros) {
      var ui =  DocumentApp.getUi();
      var pro = ui.prompt(pros, 'URL:', ui.ButtonSet.OK);
      var url = pro.getResponseText();
      return DocumentApp.openByUrl(url);
    }
    
    0 讨论(0)
  • 2020-12-03 03:49

    Libraries were designed specifically for this situation... please have a look at the documentation here.

    0 讨论(0)
  • 2020-12-03 04:02

    I have come up with a solution that works for me. It allows keeping any number of scripts attached to a sort of master document (let's call it MyScripts). No libraries, no publishing required.

    Create a document and name it MyScripts (or whatever). The document's body can be empty, or you could write some instructions there. Then, paste the following code into MyScript's script editor:

    // adds a menu to the master document's UI
    function onOpen() {
      DocumentApp.getUi()
                 .createAddonMenu()
                 .addItem('Do something', 'doSomething')
                 .addItem('Something else', 'somethingElse')
                 .addToUi()
    }
    
    // returns the target document based on its URL
    // may be tweaked in order to use the documentId instead
    function findDoc(prompt) {
      var ui = DocumentApp.getUi();
      var pro = ui.prompt(prompt, 'Document URL:', ui.ButtonSet.OK);
      var url = pro.getResponseText();
      return DocumentApp.openByUrl(url);
    }
    
    function doSomething() {
      var doc = findDoc('Do something');
      if (doc) {
        // do something with the target document
        var len = doc.getBody().getText().length;
        DocumentApp.getUi().alert('The document is ' + len + ' characters long')
      }
    }
    
    function somethingElse() {
      var doc = findDoc('Something else');
      if (doc) {
        // do something else
      }
    }
    

    The onOpen() function should be self explanatory.

    findDoc() is the real hack. It prompts the user for the URL of the target document, the document we want to act on. If the URL is valid, then findDoc() returns the corresponding document object.

    The last two functions are just stubs and should be replaced with your own code, but notice how findDoc() gets called at the beginning of each.

    When you want to run a script against a document, copy its URL, then open MyScripts, choose the corresponding Add-Ons menu item, paste the URL and click OK.

    Please notice that you will get a scary warning message the first time you attempt to run a script this way. Just be sure that your doSomething(), your somethingElse(), etc. only contain safe code before ignoring the warnings and executing the scripts.

    0 讨论(0)
  • 2020-12-03 04:09

    As of September 6, 2020, using a library implies to create a project to add the reference to the library and some code to make the library functions available to the container document. The only way to have a script available on multiple documents without having to create an script on them and without limitations is by creating a G Suite Editor add-on.

    As of December 2019 all the G Suite editor add-ons are published to the G Suite Marketplace. You could make an add-on unlisted and only users having G Suite accounts could published add-ons limited to be visible by other users from the same domain.

    Test as add-on

    If you don't want to publish and add-on you might use the Run > Test as add-on but there are several limitations. I.E. the following can't be used on this mode:

    • Triggers
    • Custom Functions

    Master project

    As suggested on Giussepes' answer, is to use a "master project" to hold your scripts. This also has several limitations

    • Most of the active methods can't be used
    • Simple triggers can't be used but it's possible to programatically create installable triggers
    • Custom functions only works on spreadsheets add-ons and scripts bounded to the spreadsheet that will use the custom function

    Be resigned

    If you are resigned to have a project on each of your documents you could reduce the burden of keeping the scripts updated by using CLASP or the Goole Apps Script Assistant for GitHub

    Resources

    • Builting editor add-ons | G Suite
    • Command line interface using clasp | Google Apps Script
    0 讨论(0)
提交回复
热议问题