Home > Blockchain >  google apps script addition to variable
google apps script addition to variable

Time:04-05

Why when I run this function, "1" is appended to the variable, instead of added to it. I guess my syntax is wrong , but couldent figure out why? :\

  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var dataSheet = ss.getSheetByName("TEST");

  //get last cell with data on cloumn A
  var columnA = dataSheet.getRange("A"   dataSheet.getMaxRows());
  var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
  
  //get last cell with data on cloumn C
  var columnC = dataSheet.getRange("C"   dataSheet.getMaxRows());
  var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);
  
  
  for (var counter = Clast   1; counter <= Alast; counter = counter  1) {
  Logger.log(counter);}
  }

Here is the logger output:

1
11
111
Execution cancelled

Thanks

CodePudding user response:

I thought that columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1) and columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1) return the string value. I thought that this is the reason of your issue. In your script, how about the following modification?

From:

//get last cell with data on cloumn A
var columnA = dataSheet.getRange("A"   dataSheet.getMaxRows());
var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);

//get last cell with data on cloumn C
var columnC = dataSheet.getRange("C"   dataSheet.getMaxRows());
var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1);

To:

//get last cell with data on cloumn A
var columnA = dataSheet.getRange("A"   dataSheet.getMaxRows());
var Alast = columnA.getNextDataCell(SpreadsheetApp.Direction.UP).getRow(); // Modified

//get last cell with data on cloumn C
var columnC = dataSheet.getRange("C"   dataSheet.getMaxRows());
var Clast = columnC.getNextDataCell(SpreadsheetApp.Direction.UP).getRow(); // Modified
  • By this modification, Alast and Clast are the values of integer.

Reference:

  • Related