Reputation: 15
I'm trying to make a process which is working through a dynamic range in Google Sheet. The length of the table can vary from 1 entry to 200. My program needs to be able to work with the table no matter how many rows there are.
I have tried two different approaches. One is very static, and requires me to loop through a lot of unnecessary rows to check if they are empty.
And the other does not handle all tables lengths too well.
Is there any other approach I can use to fix this. I've read about the "getDataRanges". But I'm not sure if this could work in my situation.
Any ideas?
var data = sheet.getRange(2,1,3,6).getValues();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.NEXT).activate();
spreadsheet.getRange('A2:V2').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
var selection = SpreadsheetApp.getActiveSpreadsheet().getSelection();
var activeRange = selection.getActiveRange();
var range = '';
var sel = SpreadsheetApp.getActive().getSelection().getActiveRangeList().getRanges();
for(var i = 0; i < sel.length; i++){
range += sel[i].getA1Notation();
}
Upvotes: 0
Views: 403
Reputation: 1832
var data = sheet.getDataRange().getValues();
for(var i = 0; i < data.length; i++){
if (data[i][0] = "") continue; //blank row
range += data[i].getA1Notation();
}
Will indeed work well for you.
That said, checking for blank rows is not slow as long as you are doing it within an array and not with individual "getValue" statements. What are you hoping to accomplish here?
Upvotes: 2