Can I get alerted when a tab name is changed?

Can I get alerted when a tab name is changed?

Use Apps Script to receive emails whenever there’s a change in your Google Sheet structure

Are you an organization with multiple people working on the same Google Sheets file? Then, you might want to know when one of your people changes something in the spreadsheet, whether that be adding or deleting columns, changing the format, or adding tabs.

In particular, one of our community members recently asked, “Can I get alerted when a tab name is changed?”

To be honest, I didn’t know the answer right away. I know that in Apps Script, there is an “On Change” trigger we can use so that the script is executed whenever there is a change in the structure of the sheet. However, the documentation does not indicate whether changing the tab name is included as a trigger. 

So I started experimenting.

Testing the “On Change” trigger

Once you’re already on Apps Script, testing the new trigger would only take us three steps.

  1. Create the new function

First, we need to create a new function using the code below. I named mine “alertMeOnTabNameChange”. Meanwhile, the second line functions to display a “toast” or a pop-up notification saying “You’ve changed the Tabe Name”.

function alertMeOnTabNameChange() {
 SpreadsheetApp.getActive().toast("You’ve changed the Tab Name")
 }
  1. Add the “On Change” trigger

Next, go to the Triggers tab and add a new trigger. Under “Select event type,” choose “On change. After hitting save, you might be prompted to authorize Apps Script to allow access to your files.

  1. Test it

Finally, we can test it by changing the name of one of our tabs. Since the toast appeared on the lower-left corner, we can verify that changing the tab name works as an “On Change” trigger.

Setting up email notifications

Now we know that changing the tab name counts as an “On change” trigger. But instead of a pop-up notification, you may also want to set up the script so that it can notify you through email even when you’re not actively working on the spreadsheet at the moment.

Using the template GmailApp.sendEmail(owner,subject,body), we want to do two things: 

  1. Send an email to the owner of the Google Sheets file, and 
  2. Include in the email body the link to the file where the change was made.

After setting up the variables and indicating the email subject and body text, our code now looks like this:

 var owner = SpreadsheetApp.getActive().getOwner().getEmail()
 var url = SpreadsheetApp.getActive().getUrl
 var body = "check out this spreadsheet " + url
 GmailApp.sendEmail(owner, "hey something's changed",body)

You may want to modify the texts to suit your style more. You may also want to include Logger.log(body) on another line so that you can see the message on the execution logs.

After that, the code now looks like this:

function alertMeOnTabNameChange() {
     SpreadsheetApp.getActive().toast("You've changed something!")
     var owner = SpreadsheetApp.getActive().getOwner().getEmail()
     var url = SpreadsheetApp.getActive().getUrl
     var body = "check out this spreadsheet " + url
     Logger.log(body)
     GmailApp.sendEmail(owner, "hey something's changed",body)
}

Granting additional permissions

If you change anything to the sheet to test the script, you might encounter an error similar to what is shown below. 

This happens because we added a new command and we do not yet have permission to send emails through Apps Script. To solve, simply go to the Editor and hit Run. It should show the authorization pop-up. 

The script should now be able to send you an email that looks like this:

Is it possible? Yes!

At the start of this experiment, we set out to explore whether you can get alerts when a tab’s name was changed in Google Sheets. Is it possible? Yes!

After a little bit of testing, we have verified that changing the tab name counts as an “On change” trigger in Apps Script. From there, we also set up a function to send the file owner an email whenever changes to the sheet structure occurs. I hope this helps you better manage your or your organization’s spreadsheets.

Watch full video here