Home > database >  Google Apps Script alert not working as expected
Google Apps Script alert not working as expected

Time:04-06

function FindLastRowAndDate() {
  var ss = SpreadsheetApp.getActive().getSheetByName('Math');
  ss.activate();
  var LastRow = ss.getDataRange().getNumRows();
  var Date = SpreadsheetApp.getActiveSheet().getRange(LastRow, 1).getValue();
  SpreadsheetApp.getUi().alert("The Value for LastRow is ", LastRow, SpreadsheetApp.getUi().ButtonSet.OK);      
  SpreadsheetApp.getUi().alert("The Value for Date is ", Date, SpreadsheetApp.getUi().ButtonSet.OK);
} 

The first alert works fine except it shows 11.0 instead of 11 (like a simple alert without the title does). Any idea why? The second alert fails for Exception: The parameters (String,(class),ButtonSet) don't match the method signature for Ui.alert. Why; its syntax appears identical to me? Any clues/solutions gratefully received.

I've tried using const instead var, but that didn't make any difference

CodePudding user response:

Description

Apparently a Ui alert can not take a Date object. You need to .toString() it or use Utilties.formatDate() to present the date value.

To remove the extra decimal digits use .toFixed().

Note, it is customary to have the first letter as lowercase in variable and function names. First letter uppercase is normally used for Object Class or global variables.

Script

function findLastRowAndDate() {
  var ss = SpreadsheetApp.getActive().getSheetByName('Math');
  ss.activate();
  var lastRow = ss.getDataRange().getNumRows();
  var myDate = SpreadsheetApp.getActiveSheet().getRange(LastRow, 1).getValue();
  SpreadsheetApp.getUi().alert("The Value for LastRow is ", lastRow.toFixed(), SpreadsheetApp.getUi().ButtonSet.OK);      
  SpreadsheetApp.getUi().alert("The Value for Date is ", myDate.toString(), SpreadsheetApp.getUi().ButtonSet.OK);
} 

Reference

  • Related