Search code examples
google-apps-scripttriggersgoogle-apps-script-addon

Is there a way to publish an add-on that uses the OnEdit trigger?


I am currently developing a budgeting add-on that allows users to automatically update their monthly transaction sums whether it be income or expenses whenever they enter in transaction amounts. The add-on uses an installable trigger, onMyEdit(e) to update the monthly transaction sums. For example, if a user types into cell A1 that they spent $5 in January, the expenses cell in January will update the cell with =SUM(A1). The script has a settings menu that the user can type in, so that it knows the spreadsheet boundaries. The script maintains the settings that the user entered by using documentProperties to manage them. In the documentation, I found that simple triggers won't work on add-ons because they lack authorization, and I don't know if installable triggers will work either.

Am I able to release an add-on that uses this installable trigger, onMyEdit(e)? Should I update the settings menu with a 'Enable on this Document' checkbox, so that the script knows which spreadsheet to be enabled on or scrap the whole idea of having an onEdit add-on?

This is my first stack overflow post, please let me know if you need more clarification. I am very confused on what I am able to do with Google Apps Script. Thank you for looking at my issue.


Solution

  • How to use triggers in Add-ons

    As described in the documentation for triggers in add-ons, you can use both simple and onEdit installable triggers in Add-ons - whereby the restrictions will be different, depending on the kind of trigger you prefer to use.

    It is important to know that for the specific case of Add-ons:

    Add-ons can programmatically create and modify installable triggers with the Apps Script Script service. Add-on installable triggers can't be created manually. Unlike simple triggers, installable triggers can use services that require authorization.

    See: Installable triggers in add-ons

    How to create installable triggers for an Add-on and how would the script know which sheet the user is working on?

    The flow is the following:

    • create a simple onOpen trigger that will create a custom menu - for the specific spreadsheet that has been opened
    • bind to the custom menu a function that will build an onEdit trigger programmatically
    • When building a trigger programmatically, you need to specify as a parameter to which spreadsheet the trigger should be linked
    • The obvious choice would be the active spreadsheet - the one from which the custom menu function has been run.

    Sample:

    function onInstall(e) {
      onOpen(e); 
    }
    function onOpen(e) {
      var ui = SpreadsheetApp.getUi();
      ui.createMenu('Custom Menu')
      .addItem('Add a trigger', 'addTriggers')
      .addToUi();
    }
    
    
    function addTriggers() {
      var ss = SpreadsheetApp.getActive();
      ScriptApp.newTrigger('doItOnEdit')
        .forSpreadsheet(ss)
        .onEdit()
        .create();     
      }
    function doItOnEdit(){
      console.log("An edit has taken place");
    }