Home > database >  Exception: Unexpected error while getting the method or property getNextDataCell on object Spreadshe
Exception: Unexpected error while getting the method or property getNextDataCell on object Spreadshe

Time:11-30

I'm trying to get last row and column of a sheet by using Google Apps Script.
When I try the code below it shows me the exception when getting last_col.

var ss = SpreadsheetApp.getActiveSpreadsheet()
var sh = ss.getActiveSheet()

let last_row = sh.getRange(1,1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
let last_col = sh.getRange(1,1).getNextDataCell(SpreadsheetApp.Direction.RIGHT).getColumn();

Exception

Unexpected error while getting the method or property getNextDataCell on object SpreadsheetApp.Range.

It seems last_row is successfully available but somehow the column is not.

When I tried to get lastest column and row by using GAS but it shows me the exception when getting last_col.

CodePudding user response:

Try it this way:

function myfunk() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName("Sheet0");
  let last_row = sh.getRange(1,1).getNextDataCell(SpreadsheetApp.Direction.DOWN).getRow();
  let last_col = sh.getRange(1,1).getNextDataCell(SpreadsheetApp.Direction.NEXT).getColumn();
  Logger.log(last_row);
  Logger.log(last_col);
}

RIGHT is not one of the directions in the enum

SpreadsheetApp.Direction

  • Related