Trigger when viewing a certain sheet in google apps script

52 Views Asked by At

Is there a trigger in Google apps script when a certain sheet comes to view? Please give some sample code. Thanks in advance..

2

There are 2 best solutions below

0
Ariz0m On

Maybe you can use trigger when someone is editing and getActiveSheet method. Something like:

let ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = 'stack questions';
let activeSheet = ss.getActiveSheet();
    if (activeSheet === sheet) {
    console.log(`The sheet ${sheet} is in use`):
    }
    else { console.log(`The sheet ${activeSheet} is the one in use`)}
5
Boris Baublys On

There is no trigger to determine which sheet is currently being viewed. And even if you try to use a combination of a time trigger and getActiveSheet() or getCurrentCell(), you will get Sheet1. But by using onEdit(), you can determine which sheet is currently being edited.

function onEdit() {
  var activeSheet = SpreadsheetApp.getActiveSheet();
  Logger.log(activeSheet.getName());
  if (activeSheet.getName() === "Sheet2") {
    Logger.log("Gotcha!");
  }
}