Google Sheets – Run Script When a Sheet is Added

google sheetsgoogle-apps-script

As it says in the title, I want to trigger a script to run when the user adds a new sheet or tab to the spreadsheet. I also include using the "Duplicate" function. How do I achieve this?

Best Answer

Use the following function, coupled with a trigger "From Spreadsheet > On Change"

function newSheet(e) {
  if (e.changeType == 'INSERT_GRID') {
    Browser.msgBox('New sheet was added');  // or whatever you want to do
  }
}