How to find the last column in a particular row in Google Spreadsheet using Google Apps Script

Akash Mondal
Jan 29, 2018 · 2 min read

Hello everyone. This is my first medium post and I am going to show you how you can find the last column(filled) in a particular row in a Google Spreadsheet. Well it is very much easier to find the last column of a sheet but it’s a bit tricky to find the last column of each row especially when the last column changes with each row.

Here’s is an example of such a case.

Here the isBlank() method of Class Range becomes handy.

So at first you just call the active sheet and find the Lastrow of the sheet as shown below;

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(“Sheet1”);
var lr = sheet.getLastRow();

Once you get the Lastrow of the sheet just add a simple For loop and compute the last column of each row using a While loop as shown below:

for( var k = 2; k <= lr ; k++) {

var m = 1; //simply set a variable with value 1
while( sheet.getRange(k,m).isBlank() == false) {

m = m +1;


}
var lc = m; //gives you the last column from each row.

That’s all. You can use this piece of code if you want ;)

Feel free to share your feedback after all this is my first medium post.

Akash Mondal
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade