How to Retype Header Automatically

Learn how to automatically retype your header row in Google Sheets using a simple script, ensuring your data remains organized even if the header is accidentally deleted or altered.

Do you ever have the problem where you're hovering over the header row and you accidentally delete it or write some nonsensical text here and you wanna come back and you wanna re head your row, but you don't know or don't remember what it was, or you're working in someone else's sheet, you don't remember what it was, or they don't remember what yours was? So was this item, was this cost or amount or is it total cost? What is it and how do we just retype the header row automatically no matter what. Like if somebody retype it or accidentally deletes it, how do we actually automatically get this header to stay the same? I'm gonna show you, in this video, we're gonna write a little bit of a script, but please don't be scared. It is fairly simple. Just follow along.

Go to Extensions app script . Here we'll find an untitled project and we'll already have a function written called Function , my Function . This is automatic. We can retype this function called right header . Again, Tom, why are you editing my header again? We'll call this. Expenses 'cause it's just the name of the sheet. But every single spreadsheet file has this app script and it's connected to all of our tabs here. And we just wanna write on the spreadsheet named expenses, the first row. So the very first thing we're gonna do in our function is called spreadsheet app. And this is a capital S. A, you will see it actually change colors. That is a good indication that you're typing it correctly.

If you're not, it'll look black. But if you're typing it correctly with a capital S and a capital A, and it's pink, you're on the right track. So spreadsheet app just says, Hey, this is a function for the entire spreadsheet file, but we don't want just the entire file. We want a very specific sheet. So we're gonna get. Active spreadsheet . That is the file we're working on right now. We're gonna add these parentheses at the end, open parentheses and close parentheses do get sheet, and we're gonna have a capital S get sheet, but by the time we get to this B, we have two options here in this auto complete. It actually helps us type this out really quickly, but also make sure that we get the. Correct capitalization. So get sheet by name. Open parentheses.

Parentheses. Make sure you add that. And inside the parentheses we're gonna do all caps expenses. This is because the tab we're working on is all caps expenses. If this sheet was expenses with just a capital E, we would have to go here in our app script and write expenses with just a capital E get cheap by name. Get range , and now we're going to focus more and more inside of this sheet in a specific range . We're gonna go to row one, column one, and we're gonna add commas in between each one of these. This is what's called RC notation , meaning row and column number , not your normal, in quotes, a colon A or A one, colon B two.

You could do that, but I wanna show you this because it's very handy for programming and for typing out app scripts all the time is knowing how this works. One comma. One comma. Now there's gonna be two more things. We need to know how many rows. Well, we know it's only one row. It's the head of row. How many columns? Now this gets a little tricky. It just depends on how many columns you want to write. In this particular case, we want six, so we're gonna just put the number six here. After this parentheses, we're gonna do something called set values . We're gonna move this all to a new line. I'm just gonna hit enter so you can see all of this. We're gonna need to add an array. This is a straight square bracket, and we're gonna write each of these headers that we want to do.

We're gonna write item in quotes. We're gonna do comma category, in quotes, item cost, comma percent in quotes, go back and forth receipts. And source. Now I'm gonna save it, command S, or click this icon that says, save project to drive. Now I've selected up here in this dropdown , there's only one function , so select that, or it's already selected. Click run, and we're going to have to authorize the very first time we do this. Click review permissions. We only have to authorize this once. Scroll down, click continue. If you have any errors, if you've typed anything incorrectly here, it'll show up right here. I have done something wrong, which is set value, and you can see the result. It is all item across. So this needs to be set values with a S at the end for multiple values .

Let's click run again. So it's saying that the parameters don't match the method because it's a bunch of strings. So what we need to do is add in here. I think arrays . Again, we can save project to drive, save it, and run it. Actually, I think we only needed one array in here. This array, click run. And we've executed correctly and we have all of our headers here. Now, once you get that to run once correctly, we don't want to have to come into here. Open up app script , click run every time. Let's automate it. It couldn't be simpler. Go over to the left side. Click triggers in here. Click on the bottom right, add, trigger . Choose which function to run. Yes, we're gonna write the header again. Thanks Tom, for making us do this event source.

This is the trick. Click, time driven. Now we can do this every minute, every hour, but let's just do this every day, once a day. At the end of the work day from six to 7:00 PM Let's click save. And again, as long as we have a run it once and it works. It's going to just basically go in here, click that run again and run it for you between that hour. If you ever want to edit it, just come back to triggers. Click this pencil icon and you can edit it. Change the time change. Maybe you want to have it run every hour or delete it. Click the three buttons over here, click delete, trigger , and you've deleted your trigger forever, but you have not deleted your code. Your code still exists over here in the editor , and we can use

it any amount of time we want. Even if you want to edit the, the actual values here, you don't have to go into the trigger and delete it or edit the trigger . You can just, you can just edit this particular code. Like let's say we don't have source anymore, we only have five things. We're gonna change our range to five at the end here. Number of columns. The number of values here is only five, so we can save that and we don't have to edit our trigger . This function is just gonna keep running. There you go. Hope you enjoyed retyping that header automatically in case somebody messed it up, even if it was us. You are watching better sheets here on YouTube. Make sure you check out this video or this video and subscribe right now to get more tips, tricks, how tos, get more out of your Google

sheets than you ever have before. I'm excited to be making a ton more videos here. Ask me questions down in the comments and I will answer them in future videos. But for right now, right here, one of these videos is gonna be your next Google sheet.