Joshua's Docs - Google Apps Scripts

Google Sheets

Get the Last Rows / Columns of Data

Option A is to use sheet methods to get the coordinates of the last area of data, and then pull values by coordinates.

const dataRange = sheet.getDataRange();
const lastRow = sheet.getRange(dataRange.getLastRow(), dataRange.getColumn(), 1, dataRange.getNumColumns());
const lastRowVal = lastRow.getValues()[0];

Option B is probably less efficient; pull all data on the sheet into JS, and then return the last values with JS:

const mdDataArr = sheet.getDataRange().getValues();
const lastRowVal = mdDataArr[mdDataArr.length - 1];

Either of these approaches could easily be modified to return more than one row, just the last column, etc.

Get the very first sheet of a Spreadsheet Doc

const doc = SpreadsheetApp.getActiveSpreadsheet();
const firstSheet = doc.getSheets()[0];
Markdown Source Last Updated:
Mon Dec 28 2020 10:14:10 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Mon Dec 28 2020 05:35:19 GMT+0000 (Coordinated Universal Time)
© 2024 Joshua Tzucker, Built with Gatsby
Feedback