Can I get alerted when a tab name is changed?
In this video, we explore how to set up alerts for changes to tab names in Google Sheets using Apps Script. Through experimentation, we demonstrate the use of triggers to notify users when structural changes occur in their spreadsheets.
So there's something that happens in BetterSheets, and when members ask me questions, or non members sometimes, ask me questions, uh, sometimes I don't know the answer. For instance, someone might ask, if a tab name is changed, how do I get an alert for that? How can I create some kind of alert system if someone in my organization changes the name of the tab? Can I do that? And the answer is I don't know. But what I'm going to show you here is me experimenting with it. So at the moment, I literally don't know if this is possible, but we're going to do an experiment with Apps Script because I do know that there is an onEdit simple trigger we can use, but the edit means the user changes the value of a cell.
There is, however, another type of trigger we can install that trigger . And when we install a trigger , we can use the on change. Now, on change means you change the structure of a sheet. So you add a column, add a sheet tab. It doesn't in the documentation include any information about the changing name. So we're gonna experiment with that today, and I'm gonna show you this experiment literally as I do it, um, because I have never done this before and I wanted to show you that it, this kind of. Uh, answering these questions can be very playful and very much just playing around with what is available and understanding what is available and trying new things. So we're going to create a function , we're going to say alert me on form, no, tab name change.
Okay. So this is the thing we want to do. So what we want it to do, let's do something pretty simple first. Toast, spreadsheet app dot, um, we want toast, a spreadsheet toast. We'll just go here and I always forget how this works. Get active toast. That's all we need, especially to get active and we're going to do toast. And the message we're going to say is you've changed the tab name, so we're not using any variables yet. We're not doing anything crazy. We're just going to change, check and experiment this. Uh, we're going to save that. We can call this experiment tab name change. And let's see what the next thing we need to do is actually Can I get alerted when a tab name is changed? Getting Started Coding in Apps Script Sync Tabs in Different Sections Advanced Coding Tips in Google Sheets for Programmers
create the trigger for it. So we'll go over here to triggers. It will need a moment on the bottom right. Click add trigger . Choose which function to run. There is only one function . We don't need to change that. The select event source is going to be from spreadsheet and it's this on change. Again, we're experimenting if the tab name is a change. You also have on edit here, but again, that is just editing a value in a cell, uh, and not the editing the sheet name , I think. But let's see, on change should be it, or if it would include it, so save. Now, we can also test this on change in another way, so actually we need to authorize these things.
Let's authorize it. I think there's another way we can test this. Uh, to show you that this will work. If it doesn't work, I'll show you that it will work. But let's just save that. Okay, now we are triggering any change. So again, any change meaning, actually this is the other way to test it. So we're going to duplicate that. See if we get a, now we have changed, but, okay, the message is, the message is, uh, Just a hard coded. We haven't actually changed the tab name. Um, but let's go and change this to data2. That is a change of the structure. And it is, you've changed the tab name. That is fantastic. So, it works.
So now we know definitively that a change in the structure, uh, is happening. But also changing the tab name is this onChange, uh, change. Trigger . That is really cool, and so, let's say you are an organization, and you are like, hey, I want to know if one of my people has changed this sheet in some way, meaning they've added a tab, they've deleted columns, uh, added columns, they're changing the name, that's all included here. So this toast is going to show up in the, uh, In the spreadsheet . So you've changed something we should probably say, because it's not so specific. Also, what we can do here is add a mail app or Gmail app, uh, send email.
And we're going to send an email to, uh, probably do the owner , not the session. So here in Apps Script , we have a get owner , so we can use this app active spreadsheet , get owner , get email. Here, so, we will send an email to the owner . We can do variable owner equals get owner , get email. There we go. We're going to send an email and we're going to say, hey, um, hey, something's changed. Uh, and in the body we'll say, uh, check out this spreadsheet. And we want to add a URL here.
So we'll get variable URL equals spreadsheet app dot get active spreadsheet , get URL. There we go. Uh, and we can also, let's look at this body as it's sent. So variable body equals this, and then we want to logger dot log body. So we can see it in here. We'll send that email. So now we don't have to update the trigger . The trigger will still work. Uh, we just have to save this and we will go and update something. So let's duplicate this and we can go back into our executions. Look at the, uh, we see we failed.
Let's see what we did here. Uh, if we refresh, we should be able to get the error message. Script does not have permission. So, um, what I think we need to do. Go to the editor . If I just hit run on here, I should be able to access the authorization . There we go. We just have to authorize it again. Or not again. One more time with all of these new things in it. Uh, we may get a failure here from running it in here. But cool. Okay. Let's change something again. Let's change this to data three. Go back to our, uh, Execution. Sorry. Let's see. It is completed.
Um, refresh. And this is the body of the email. And this is what the email looks like over here. It is just check out the spreadsheet . Something's changed. Uh, there we go. So this ended up being a pretty interesting dive into emailing upon every change, but it started with the question of, Can I get an alert every time a tab name is changed? And the answer is yes. And we can also go much deeper into this and any kind of structural change to the spreadsheet can be alerted. Uh, also, we can send an email fairly quickly, uh, with these alerts. Hopefully, this was pretty helpful and pretty interesting to you because I didn't know the answer to this at the beginning of this and now we went through an experiment.
Eight minutes. Went by and we are, we know the definitive answer.