Skip to content

Instantly share code, notes, and snippets.

@johnwards
Created May 15, 2012 11:39
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnwards/2701061 to your computer and use it in GitHub Desktop.
Save johnwards/2701061 to your computer and use it in GitHub Desktop.
get the value of last row in a sheet in google docs
function lastValue(column) {
var parts = column.split("!");
if (parts.length == 2) {
var sheetName = parts[0];
var column = parts[1];
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
} else {
var column = parts[0];
var sheet = SpreadsheetApp.getActiveSheet();
}
var lastRow = sheet.getMaxRows();
var values = sheet.getRange(column + "1:" + column + lastRow).getValues();
for (; values[lastRow - 1] == "" && lastRow > 0; lastRow--) {}
return values[lastRow - 1];
}
@Vexercizer
Copy link

Your function produces an error. This works.

// ignore zeros at end too

function lastNoZero(myRange) {
    lastRow = myRange.length;
    for (; myRange[lastRow - 1] == "" || myRange[lastRow - 1] == 0 && lastRow > 0 ; lastRow--)  { 
       /*nothing to do*/ 
    }
    return myRange[lastRow - 1];
}

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