1
votes

How to detect and trigger custom action during opening specific sheet in spreadsheet?

I could not find proper function in https://developers.google.com/apps-script/guides/triggers/events

2
The tags you chose are not appropriate for this question. Please review What are tags, and how should I use them?Mogsdad

2 Answers

3
votes

To do polling as Mogsdad suggested you could use this code in your Sidebar (or any UI element)

$(function() {
   /**
   * On document load, assign click handlers to each button and try to load the
   * user's origin and destination language preferences if previously set.
   */
   poll();
}

function poll(interval){
    interval = interval || 2000;
    setTimeout(function(){
    google.script.run.withSuccessHandler(loadSheetName)
        .withFailureHandler(showError).getSheetName();
    poll();
    }, interval);
};

function loadSheetName(sheetName) {
   alert(sheetName);
   }

function getSheetName() {
   google.script.run.withSuccessHandler(loadSheetName)
        .withFailureHandler(showError).getSheetName();
   }

And in your .gs code

function getSheetName() {
var sheetName =         SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getName();
return sheetName;
}
2
votes

There is no API provided to trigger on user events, aside from the ones in the documentation you've linked.

I can think of only one work-around: If your application involves having a custom UI element, specifically a sidebar or dialog, then you could poll for the active cell, and determine the active sheet from that.

See How to poll a Google Doc from an add-on for a start.