How to use a google apps script in multiple docume

2019-01-18 05:06发布

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 script in all those documents, and not copies of that script.

I am aware of the below question, which may qualify as a duplicate, but I am reluctant to accept its answer.

Google Apps Script - How To Have One Script In multiple containers?

So, my question is: is there really no way to share a script among multiple google documents? Must you really create a new script project for every document and copy-and-paste the code from an other? Moreover, if you fix a bug in one of them, do you have to remember which documents use that script and open the script editor in each of them and copy-and-paste the new code?

2条回答
Anthone
2楼-- · 2019-01-18 05:32

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

查看更多
可以哭但决不认输i
3楼-- · 2019-01-18 05:48

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 should into MyScript's script editor:

// adds a menu to the master document's UI
// the document
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(pros) {
  var pro = DocumentApp.getUi().prompt(pros, '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.

查看更多
登录 后发表回答