2

I currently have a Google Sheet that I'm using as a master template. That is, I'm making a copy of this template for every request. I want to add a Google App Script (which onEdit POSTs to my server when the sheeting is completed) to my master template that will be duplicated and run for every copy of this template.

I've tried doing this from an admin account, however, the scripts don't seem to 'stick' with any of the templates. Is this possible?

2
  • can you open the scripts tool of the templates and see if the codes is there? If it's not, it only makes sense nothing is happening. Apr 24, 2018 at 9:33
  • I copy my code using copy and move to in order to back it up. If you want to save your triggers then stick with simple triggers or do them programmatically.
    – Cooper
    Apr 24, 2018 at 13:28

2 Answers 2

1

Installable triggers can be attached to any spreadsheet (respecting sharing permissions) from any project. You can add a new trigger to your master sheets project for each copy that is made. In the second example here they suggest using SpreadsheetApp.openById() you could also use SpreadsheetApp.openByURL() or the Spreadsheet returned by Spreadsheet#copy() depending on how you are duplicating your spreadsheet.

4
  • 1
    Is it possible to do this when using the google api to copy a file?
    – Kajsa
    Jan 16, 2020 at 13:28
  • 1
    Seconding what @Kajsa said, is it not possible to keep Installable Triggers when using the API? We copy sheets with the API, and need a function to run one time on load.
    – snowe
    May 1, 2020 at 5:02
  • 1
    @snowe did you find a way keep Installable Triggers when using the API? please share it. thank you
    – Chipintoza
    Jul 17, 2021 at 11:28
  • 1
    @Chipintoza, no, and it appears from the ticket I made that they don't care about adding it.
    – snowe
    Jul 19, 2021 at 16:36
0

Personally, what I do in 2022 is including something like this on the beginning:

function scriptSetup() {
  createOnEditFunction();
}
 
function createOnEditFunction() {
  const ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('onEdit')
    .forSpreadsheet(ss)
    .onOpen()
    .create();
}

function onEdit(e) {
  //your stuff here
}

This way, the scriptSetup() is located on the first line so when copying a spreadsheet I would ask them within Sheets to click on Extensions > Apps Script > Run.

As per Jared Pinkham's link referencing Developer documentation, there are a few things Apps Script can use as triggers other than onEdit.

There are several installable triggers for Google Workspace applications:

An installable open trigger runs when a user opens a spreadsheet, document, or form that they have permission to edit.

An installable edit trigger runs when a user modifies a value in a spreadsheet.

An installable change trigger runs when a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column.

An installable form submit trigger runs when a user responds to a form. There are two versions of the form-submit trigger, one for Google Forms itself and one for Sheets if the form submits to a spreadsheet.

An installable calendar event trigger runs when a user's calendar events are updated—created, edited, or deleted.

You can refer to the following link for spreadsheet triggers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.