How to Move Row To Section in Google Sheets

Learn how to automatically move a row to a specific section in Google Sheets using Apps Script. This tutorial demonstrates how to set up a function that responds to edits in your sheet, making data organization easier.

We have an interesting problem here. We wanna move a row to a section in a sheet. So normally when we're moving rows, we're moving them between tabs, and that's what I prefer. I prefer to have different tabs and have my data in different tables so that I can just move it to another table . However, very often we have created sections. But the table and the sheet doesn't really know that. So in this particular case, we have a little intergalactic zoo and we have put the sectors, uh. In column B, however, we have made the habitat also in the B column. You can see the, uh, you can see as a human, not as a computer and not as a spreadsheet , but as a human, we can see, oh, there's these sectors or

sections that are aquatic, volcanic zero G, and we have 'em in the B column, whereas the habitat we've selected each one is in the A column. So if we wanna change. Where something is like, let's say this is unassigned the silver back lighter, but we know this is gonna go into the forest. We wanna select that forest and we want this whole row to move to the forest quote unquote section. Now it's fairly difficult because this section title is in B. We need to go, okay, as a human we can see this, we can go, okay, B. Here in B 11, it's forest. So I wanna move this row right there. Not that hard to do manually. To do it automatically. A little tricky because we need to look through all of this, find this, know

that we have to go a row underneath. So let's do that. Let's go up to extensions app script and we're gonna start our function . We need to do this in function on edit. Uh, we need this because. We are editing the sheet. When we do this selection , it's actually, the thing we're doing is we're editing the text in the cell to the word zero G, volcanic, aquatic, or forest. So know that. And we are gonna get this E, which is an event, and it's just gonna give us some data , like where we are, what row we're on, what column we're on. But we need to grab that information. So let's do that. Let's get the sheet. That's E source dot get active sheet . We need the range .

We're gonna have a row, which is range get row. Constant call is range . Do get column. And the value that is the thing we're going to look at the text SQL to E value. So that E here is work doing a lot of work for us. It's, it's grabbing all of the data and then we're gonna access that data through each of these variables and let's make sure that we're on the right. Page on the right place. We can say if an in parentheses sheet is equal to sheet one, or let's say zoo, and we can change this name of this sheet to zoo, 200% is the word and row is greater than one. Meaning we're not editing the header row .

And call is equal to one, meaning we are in that first column. Any edits we do everywhere else, we don't want to touch. We only wanna edit the column A and then we'll actually move this row. So we'll put some curly brackets here so that we know, okay, now that those things are true, let's do something and let's get the values of column C. We are just naming this call C Values . You can name this anything you want, which maybe you wanna call it like sectors. We're gonna do sheet dot get range . And I like to start on row one, column one and go until we get the last row, which is sheet dot get last row. And then outside of this range , so get range . Now we're gonna get values and let's get a target row.

Let's set that to negative one for now, and we're gonna create what's called a for loop. I is equal to zero and we're gonna go however far the length of this is, so sectors length, and we're gonna iterate plus plus, which just means iterate one time, one, one each time. And if sectors, I. Zero is equal to this new, this value, then what are we gonna do? Well, our target row is going to change to I plus one. So what we're doing here is we're saying, Hey, just take all of those values in, in the sectors in this actually, uh. This is row one, column one. We want column three. Sorry, this is, sorry, B, not C. So two. So we're saying get all of these sectors, get all of the data here in column B

and however long that is, go through it each one and find the word that matches the word we have just edited. Once we have found that, now that's our target row. And now we can say sheet dot. We are gonna dig to sheet, move rows. We're gonna move. We're gonna move something, we're gonna do comma target row plus one. But before that, the first section is we need to find the row to move, which is sheet get range . Row one, and this is comma one, meaning just the columns. It's gonna get that whole row . Let's save this and check it. So we'll check this to forest. I do think there's some, one weird thing that's going on is we're. We're getting the sheet here, but we're checking it against the name.

So let's do sheet. Do get name. Let's try this again. Let's straight in just a forest. And there it goes. So maybe we wanna change this to zero G. And there it goes. You can see it moving right away. So we have volcanic. And that's moved as well. There you go. So sections are moving with all of this code, 20 lines of code, some spaces. We're taking the active sheet , finding the range , getting the row, the column, what value we're looking for as well, and then we're saying, Hey, make sure it's within these parameters . We're on the right sheet. We're in the right row, we're on the right column that we want to care about. Then we are finding all of the possibilities here, the sectors here. Then look through them all, find out which one it is. And add one. And move the row.

Very cool. Right? Hopefully this was very helpful to you. 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.