Skip to content

Instantly share code, notes, and snippets.

@MauricioMoraes
Created July 27, 2015 16:57
Show Gist options
  • Save MauricioMoraes/a8f4b4bd89b647b749f5 to your computer and use it in GitHub Desktop.
Save MauricioMoraes/a8f4b4bd89b647b749f5 to your computer and use it in GitHub Desktop.
Google Apps Script for Spreadsheets - Find the relative row number within a range that has a a cell with given target value - Range in A1 Notation starting
function findRowNumberForCellWithValue(targetValue, rangeA1Notation, sheet) {
var range = sheet.getRange(rangeA1Notation);
var values = range.getValues()
for (var i = 0; i < values.length; i++) {
if (values[i][0] == targetValue) {
return i+1;
}
}
}
@MauricioMoraes
Copy link
Author

Example: Getting the line number of the first empty cell on first column

var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName('MySheetName');

var emptyLineNumber = findRowNumberForCellWithValue('', 'A:A', sheet);

This is very useful for many logger scripts where you have to fill the last line with the updated value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment