How To Insert Row Under Header Automatically
Learn how to automatically insert a row below the header in Google Sheets using App Script. This video guides you through the process of creating a custom menu and setting up triggers for automation.
You may want to insert a row, but when you're inserting a row, you want to insert it below the header , and I'm gonna show you how to do this automatically. Now, if we put our cursor here and we right click and we say, insert row below, we are going to have an issue. The design of that header row is copied, so we can't do that. And actually we need to go just underneath it, which is row two, and insert a row above. Now this is how we do it. Manually, you saw me do it right here with some keystrokes, my keyboard and and mouse. But let's do this with App Script and it's going to be very similar Extensions app script . I'm gonna name our project. It's a row. We are gonna create a function insert new row below header and we can do
spreadsheet app, get active spreadsheet . That is the entire spreadsheet file. So we want to get our active sheet and we're gonna do insert row and just like we did before, manually, we actually want to do insert row before. And this is very specific to doing it under a header because we want the row that's under the header , that design to be copied. And so we just put the number two here. Now I can hit command S, or I can save project to drive this button here, and I can run this from here. But there's one special thing I can do. Which is I can go to better sheets.co/snippets and grab this custom menu code, and I'm gonna do that above this function. I'm only gonna need one item, which is this function name is gonna go here
in the second part, and in this first item, it's gonna be a human readable. Name insert row and maybe I say at top or under header if I save this. So this is a function on open. We're going to getting the UI and we're gonna create a new menu, custom menu . I can save this. I don't have to click run. I go over to my sheet, I refresh my sheet, lemme show you a little bit smaller and up at the top. You can see help, accessibility and custom menu . This custom menu I just added and it says insert row on your header , I'm gonna click it and the very first time I ever click it, I'm going to need to authorize it. I am gonna hit continue. And you see a blink row has been added and it's not the copy of the design of the header, it's actually inserting it from below.
Very cool. Right? And now I can do that. From this menu. I can just say, Hey, go insert a row. Boom, it's done and it's all in this extensions app script . Again, you can see that it is one line of script here, but we're just adding this extra menu here on open. Pretty cool. I think this is really fun and exciting. You can get this script on open over at Better Sheets Taco slash snippets. And what if you wanna do this continually or automatically without me having to go to this menu over here in our script for this function , insert row below header . I can go over to the left side and click triggers. I can go to the bottom right and the bl big blue button that says add trigger . Click that. I can choose which function to run, which I now have two functions , but I only want to run insert new row below header . So I'll select that in this dropdown, and I'm gonna select an event source.
Time driven. Time driven. I can now say, is it gonna be every hour, every day, every week? Maybe I only want to do this every day, once a day, and I wanna do it between five and 6:00 AM before the start of the day, or I wanna do it in the afternoon. For the eventual inserting of data later up to you to say, Hey, between these hours, I want to do this. So click save and you'll see this line and you can come back here and check it. If there's any errors, there'll be over here in executions, but also if you're like, Hey, I don't want any more new rows. I want to delete this. So you need to come back to extensions app script . Go over two triggers and over on the right side there's this three buttons. There is this, uh, pencil icon to edit it, but there's also these
three buttons where you click it and you can delete this trigger . And if I wanna delete the trigger , I have to click delete forever. And now no more automation, but I can add it again if I wanna just add trigger here. 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.