Michal_Szulc
Michal_Szulc

Reputation: 4177

How to detect user changing sheet?

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

Upvotes: 2

Views: 1836

Answers (2)

Rafał Pydyniak
Rafał Pydyniak

Reputation: 539

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;
}

Upvotes: 3

Mogsdad
Mogsdad

Reputation: 45710

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.

Upvotes: 2

Related Questions