Move Blank Rows #googlesheets
Learn how to efficiently move blank rows in Google Sheets using Apps Script with two different methods. This tutorial covers both cutting and pasting as well as copying and deleting rows for optimal results.
So someone asked how to move blank rows . So we have like a column here with info. We have yes or no or blank and we only want to move the blank ones. We want to move the entire row over to the sheet blank. Now how do we do this? I'm going to show you actually two ways to sort of do this in Apps Script and one might be better than the other.
The blank ones are just gonna have nothing. In them. It's gonna be a blank value. Uh, we're gonna get the first sheet info. The second sheet is blank, and that's just the names here. So if you're using this, actually we probably wanna capitalize this whole thing blank. So we get these two sheets. Now this is the most important part. Uh, it's a four loop. We're starting at zero. We're going until the entire length of the column array, which we got up here, the column B, and we just put the range here as we usually use in spreadsheets, B two to B.
meaning there's just two quotes here, so there's nothing inside these quotes. And if that's true, Then it's going to, then the next two things are going to happen, but if it's not true, we're just going to skip it. It's just going to check for each one, basically for each item in this array. Uh, start at zero, go to the zeroth, the first item, check if it's blank. If it is, here's what we're going to do. We're going to go and we're going to get the last row of sheet two, which is an encased blank.
So I plus two here. We're only getting two columns. Out of this we're going from the first column to the second column here. And we're going to use the move to . So we're taking sheet one, getting the range , getting that specific row.
What it did is it actually cut and pasted it. So it, say, this one was blank. This is what it did. It took this two rows, did command X, or did, uh, cut. Went to blank, went to the last row, added one, and then paste. And so we get the exact same result here as if we cut and paste.
And here we have All the blanks. It actually is moving these, but because it's moving no values , when it goes to the last row, it's just adding the last row here.
I'm not going to change it. I'm actually going to create a whole new function here. Duplicate it. I'm going to call it copy to if blank and we're going to use instead of move to and say copy to .
Okay. So let's delete these two. So we now have two that are possibly going to move. We're going to use copy two. And again, it's copying the information over.
we copy it, we're getting the wrong i. So what I think we can do here is instead of deleting all of the rows all at once, what we can do is go variable deleteArray equals, and we'll create a blank array.
So the length of that array. If there's anything in there. We're going to each time do j plus plus. And now for each one, we're going to sheet one dot delete row .
So let's see if this works. Now we have say, Judy and Chelsea here are blank. No others are blank and we should have this fixed. Let's run it. See our execution started.
And I just want to point out again, like moving rows, when we say move, sometimes we don't want to use the app script that's called move. We want to use copy and add delete.