How to Rename a Sheet based on a Date Automatically
Learn how to automatically rename a Google Sheet based on a date using Google Apps Script. This tutorial covers setting up triggers to rename your sheet daily or based on specific actions.
We've got this wonderful question, which is, can we rename the sheet or the spreadsheet title, like the entire file name? Can we rename that either every day or based on some actions? And yes, we can go over to extensions app script . We're going to do that there, but the title we're going to get first is going to be this B1. I'm going to show you some really cool things that you can do based on triggers and other things. But for right now, the first thing we're going to do is we're going to rename the sheet to whatever is in B1. And I'll show you that function . So come over to extensions, Apps Script . We'll write function , rename sheet . We're going to add a variable name or actually title. We will get spreadsheet app dot get active spreadsheet . We're going to get the name. We need to get it from the value of the cell. So we're going to get sheet by name, the name of the sheet back here.
It's called title. So in quotes, do title dot get range . I think it was, let's not guess, it is B1 and get value. So now, based on that, we have the new name we want to get. Now we want to rename it. So we're going to do spreadsheet app. Dot get active spreadsheet , do rename. It's gonna be pretty simple. Rename. What are we gonna rename it? The title. So anytime we run this, or we're gonna hit save, anytime we run this, like save, click, run and we will rename the spreadsheet to whatever that title is. So once we run it, you can see that it says, name me this, so rename me something else. Let's do something very long. Let's run it again. Execution started and it's renamed me something else. So anything we put into this, we can even put some emojis. Let's do done. Let's go hit run.
Now I think you might be asking the next question is, okay, we can rename the sheet, but we don't want to keep hitting run. We don't want to keep come back to the extensions after. How do we do this? We can set a trigger . We can trigger this maybe every day or every hour. Click over triggers, click down below here. Add trigger , which function are we going to run? We're going to say rename sheet . And instead of from spreadsheet as the event source, we're going to use time driven. So here we can use an hour, maybe every five minutes, let's go every day at the end of the day, or at the beginning of the day, maybe two to 3am, we will Run this. So whatever that day is in this cell. So maybe we do equals today. And so we have a date and we say, maybe we have an ampersand here. We go, I went in front of today. We use quotes and we say, Hey, project.
And we put a pipe and then add a ampersand. And so now we have this number , right? You can format this as well. Can wrap this with something like year and we can format this and we can say. Another ampersand, some quotes, another ampersand, and say month, and wrap that around today, and there we go. It's going to always update. Maybe we add more stuff here like ampersand, another quote, or another slash, ampersand, and day, today. And there we go, we get the 8th of August, 2024. Cool. So we can create that kind of date. And now every time we run this or every time it runs by this trigger , it'll just go in here for us and click run and it will update the name here. We can click run and you'll see, instead of done, it'll say the date.
There we go. We've renamed the project, but now what if, so that's all great. Right? We take a single cell and we say, hey, go and change the date, name, change the name. But let's say we have a list of names we want to call it and we have the date. We want to say, hey, today, or not today, but on the 8th of August, change the title of this to first day. On the ninth, call it second day, right? So, every day we want a different one. So, we have this trigger that's running every day. And every day, right now, it's going to do this. It's going to just change a title, whatever that cell is. But now we have an array. So this gets a little bit more complicated, but it's doable. Let's call renameSheet thisTitle. So we're going to create a whole new function . And instead of getting the title as just this one specific thing, we're going to basically get the range of B.
Here, pick out which day, find out what day it is, and then get the C, in the C. So we have two things here. We need variable titles. Is going to be SpreadsheetApp . getActiveSpreadsheet. GetSheetByName. And here we have a new thing called Daily. That's this. Daily. We're going to get a range of the C column. And we want to get values . We want, so this is going to be an array of all of those values . We want to put this in quotes. Now, instead of, not instead of titles, but actually we want all the dates as well. So variable dates. is going to be the B column. Now, we have to go through a, what's called a for loop. We say for I equals zero, I is less than dates dot length. Length means the amount of items in that array. I plus, we're going to iterate through each one and we're going to
say if dates I is equal to today. So what is today? We have to get a variable that looks, uh, acts, acts just like this variable here today. So we're going to do variable today equals new date. We do need to format this a little bit because this is a timestamp and it's always going to look different. So we're going to say utilities dot format date. How do we want to format this? Oh, we want to get the time zone . Sure, GMT, whatever, plus eight, minus six, whatever you want to do. The format is going to be here. Actually, what is that? No, not utilities. It is day slash month slash year. Okay. So we hopefully get the correct date and we can also look at them. So we can log logger dot log today. Let's actually just log that variable and see what it looks like.
And if these two things are the same, we want to do this rename to instead of title, we are going to use titles and we're going to get the same row, basically in that array. We'll do that. We'll, we'll also log this just in case there's an error , logger. log titles i. So we might have to do zero here. Let's look at it though. Okay, so let's just run this and see what we get. We may get some errors. We'll work through them. So we didn't get an error , but we did log. Something and only one thing we only logged today so we can find we know that these dates and this today are not the same format this they're not finding the same exact format here so let's look at that and see maybe we have to do some format here format this text as a number actually custom text Date and time.
So let's just pick out the one that's probably, Oh, I see. It's, I think it's, we need zeros and we need it. Oh, we have month. Okay. Month day. So we need to change our format here. Month day. So we just need to make sure that these two variables are similar format so that if it does match , it's going to rename, if this works, it's going to rename our project as first day. Let's go. I just saw one more thing. As I ran that, I realized one more thing is we have. Just two numbers for year here. So let's actually keep this all four numbers and change this format . Format number , custom date and time, and use full digit. Okay, perfect. Now let's see if this works. So again, these two numbers need to be aligned. See if it works. Nope. Let's do one more thing to check and log.
Actually. Just all of this, we will log it. Even if it's not just to see what it looks like. We can also just, we're going to delete all of these rows. So we only go through a few rows and let's look at those. Ah, I think we're looking at. Of course we need to change this. I know dates. Let's go actually let's log dates instead. That was the mistake, save it and run it. Ah, so we do have an entire timestamp here. So let's make sure that these dates are the same. So variable date to check equal to utilities, a format , and we're going to format this date GMT on the exact same format as up here there. So now we're going to format the date. Just to make sure we check it appropriately.
Let's run it. Exception have here. Parameters don't match . Oh, format date. That's what we're getting. So I think what we have to do is actually make it a format here. So we have to wrap it with new date. But even though it's dates i. Let's run that again and see if that works. Perfect. So now we see we have renamed it actually second day. Okay. So we are somehow getting the wrong date, but we are doing something correctly. Great. So what I had to do is make sure that the time zone was actually the right time zone where I am, because the dates were not lying correctly. These dates were actually different. They're different timestamps. And if we're in a different time zone, it's going to look at it differently. That's the answer of why it was doing a correct doing, actually doing the change.
But it was not doing it to the right title. So make sure that your time zones are all correct. And this is it. Yeah. So now we can rename sheet this title. We can run this function every day and it will based on the date, rename the title to what is in the column here. So I hope this is helpful to you. You already saw how to create a trigger . Earlier in the video where we did rename the sheet. If you want to delete triggers, which may happen if you're not wanting to rename the sheet anymore, go over here in triggers and delete trigger . There you go. If you are a better sheets member, you can watch this on better sheets. co and you can get this exact app script and the sheet down below. If you're not a member yet, become a member today.