Spreadsheet rename event with apps script

996 views Asked by At

Is it possible to listen to "Spreadsheet renamed" events?

The corresponding event object has changeType equal to 'OTHER'. It works when the change is renaming a sheet, but doesn't work when the whole spreadsheet is renamed.

1

There are 1 answers

0
Brian On BEST ANSWER

You can't run a function on sheet rename, no. You can do it from a Spreadsheet-bound script, but you need to set some properties and cascade the triggers. This is a really rough working example:

// Globals
var ss = SpreadsheetApp.getActiveSpreadsheet();
var name = ss.getName();

// Each edit, check for a rename
function onEdit() {

  // Grab the stored name from opening
  var setName = PropertiesService.getDocumentProperties().getProperty("name");

  // Get the current name of the sheet.
  var currentName = SpreadsheetApp.getActiveSpreadsheet().getName();

  // If it doesn't match, there's been a rename. Log and reset the stored name.
  if(setName != currentName) {
    Logger.log("Renamed from " + setName + " to " + currentName);
    PropertiesService.getDocumentProperties().setProperty("name", currentName)
    // do something else
  }
}

// Store the name when the sheet is opened
function onOpen() {
  var currentName = PropertiesService.getDocumentProperties().setProperty("name", name);
  Logger.log(currentName.getProperty("name"));
}