0

I am trying to have a function be able to "pick up where it left off" in Google Sheets.

I need to find the last row in a specific column which has data. However:

  • The sheet has many other columns with even more rows, so I cannot use getLastRow on the Sheet.

  • I am trying to determine the range to target, so getLastRow will now work at a range level either, as I do not yet have the range.

  • I cannot array.filter(String), as I am dealing with numeric values.

  • I can not filter to remove falsy elements (often used to filter blank array values).

Example:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange(B1:B);
var values = range.getValues(); //Pseudo Array values = [0,0,1,"",""]
var quantityOfValues= values.filter(?????).length //Goal: quantityOfValues== 3
var dataRange = sheet.getRange(1,2,quantityOfValues,1).... // do stuff to this range
Joshua West
  • 63
  • 1
  • 6

2 Answers2

3

The simplest method to remove all falsy values excelpt zero, you could check for truthy value or for zero.

result = array.filter(v => v || v === 0);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Finding column height

This is what I do:

function getColumnHeight(col,sh,ss){
  var ss=ss || SpreadsheetApp.getActive();
  var sh=sh || ss.getActiveSheet();
  var col=col || sh.getActiveCell().getColumn();
  var rg=sh.getRange(1,col,sh.getLastRow(),1);//This is just a reasonable starting point.
  var vA=rg.getValues();
  while(vA[vA.length-1][0].length==0){
    vA.splice(vA.length-1,1);
  }
  return vA.length;
}
Cooper
  • 59,616
  • 6
  • 23
  • 54