Reputation: 75
I'm writing a script for Google Sheets and would like to select all the columns in a given row. I know I can do the converse and select all rows in a given column using something like sheet.getRange("A1:A").getValues()
Is there an equivalent syntax for selecting all the columns (non-empty, preferably) in Row A?
Upvotes: 3
Views: 8126
Reputation: 1772
The Google sheets syntax for an entire row is: "A1:1" (row 1) or "A2:2", (row 2) etc. So that you could use that in your script.
GAS also has:
var lastColumn = sheet.getLastColumn();
which could allow you to create the range dynamically within your code.
row = "A1:"+lastColumn;
I think the difficulty is around 'non-empty', Do you mean stop at the first empty column, or the last, is a key question? Assuming the first, then the following might help:
function last_column() {
var sheet = SpreadsheetApp.getActiveSheet();
var values = [];
values = sheet.getRange("A1:1").getValues();
// getValues() returns a multi-dimensional array, so syntax
// for the entire row is values[0]
for (var i = 0; i < values[0].length; i++) {
if (values[0][i] === "") {
Logger.log("found empty column: %s",i+1);
break;
}
}
return i+1;
}
Upvotes: 0