Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active September 24, 2023 12:48
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tanaikech/bc09d1e03a0ebed3af894a6ed61cf12d to your computer and use it in GitHub Desktop.
Save tanaikech/bc09d1e03a0ebed3af894a6ed61cf12d to your computer and use it in GitHub Desktop.
Google Apps Script: Running Specific Function When Specific Sheet is Edited on Google Spreadsheet

Google Apps Script: Running Specific Function When Specific Sheet is Edited on Google Spreadsheet

This is a sample Google Apps Script for running the specific function when the specific sheet is edited.

Sample script

Please copy and paste the following script to the container-bound script of Spreadsheet and set sheets object.

// When the cells are edited, this function is run by the fire of event trigger.
function onEdit(e) {
  // Please set the sheet name and function as follows.
  const sheets = {
    Sheet1: functionForSheet1, // Sheet1 is the sheet name. functionForSheet1 is the function name of function which is run when Sheet1 is edited.
    Sheet2: functionForSheet2,
  };

  const sheetName = e.range.getSheet().getSheetName();
  if (sheets[sheetName]) {
    sheets[sheetName](e);
  }
}

// In this sample, when Sheet1 is edited, this function is run.
function functionForSheet1(e) {
  console.log("Sheet1 was edited.");

  // do something
}

// In this sample, when Sheet2 is edited, this function is run.
function functionForSheet2(e) {
  console.log("Sheet2 was edited.");

  // do something
}
  • In this sample script, when the cells of "Sheet1" and "Sheet2" are edited, functionForSheet1() and functionForSheet2() are run, respectively. When other sheets are edited, no functions are run.
  • In this sample script, onEdit of the simple trigger is used. When the functions you want to run include the methods which are required to authorize, please use the installable trigger.

Note

  • This method can be also used for other event triggers like OnChange, OnSelectionChange and so son.

References

Testing

  • October 16, 2020: I could confirm that this sample script worked. In the current stage, it seems that the specification at Google side is not changed.
@cwlind
Copy link

cwlind commented Oct 4, 2020

Thank you. I have learned more about the elegance of coding from you than anyone else. Your code always teaches me something new and elegant, and how to think about the process, not just the code.

@tanaikech
Copy link
Author

Thank you for your comment. I'm honored for it. Your comment is more encouraging than you think.

@Max-Makhrov
Copy link

Thank you. I have learned more about the elegance of coding from you than anyone else. Your code always teaches me something new and elegant, and how to think about the process, not just the code.

Agreed. The code by @tanaikech becomes classical examples for developers.
Not only useful, but also "How to code" guide.

@tanaikech
Copy link
Author

Thank you for your comment. I'm honored for it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment