Skip to content

@johnwards /lastValue.js
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

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
Something went wrong with that request. Please try again.