简体   繁体   中英

Share code between google scripts

I have a script that I use and update frequently for +3 spreadsheets, and I find it annoying to update each scripts separately every time. Would there be any ways to share (import/require) code from one place for each of those scripts?

I cannot have all these spreadsheets in the same file since they concern different clients.

My code contains functions as such:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Custom Export')
      .addItem('iOS', 'exportForIos')
      .addItem('Android', 'exportForAndroid')
      .addItem('JSON-iOS', 'exportJSONForIos')
      .addItem('JSON-Android', 'exportJSONForAndroid')
      .addToUi();
}

Google Apps Script actually allows to use a Script as a library and share code between various scripts.

  1. I created a stand-alone script
  2. Set it's project settings and API execution key on console.developer
  3. Took it's project ID and added it as a library on each of my scripts
  4. Copied the main functions that I needed in the menu using the identifier I set (in this case, Trad):

```

function onOpen() {
  Trad.onOpen();
}
function exportForIos() {
  Trad.exportForIos();
}
function exportForAndroid() {
  Trad.exportForAndroid();
}
function exportJSONForIos() {
  Trad.exportJSONForIos();
}
function exportJSONForAndroid() {
  Trad.exportJSONForAndroid();
}
function exportForAndroidToDrive() {
  Trad.exportForAndroidToDrive();
}
function prefixSelectedCellsWith(chars) {
  Trad.prefixSelectedCellsWith(chars);
}

```

Host it yourself (externally) and refer to it, would that work?

example:

  <script src=" http://www.yourdomain.com/yourscripts/thescript.js"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM