Google Apps 脚本警报未按预期工作

Google Apps Script alert not working as expected

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);
} 

第一个警报工作正常,除了它显示 11.0 而不是 11(就像没有标题的简单警报一样)。知道为什么吗? 第二个警报因异常而失败:参数 (String,(class),ButtonSet) 与 Ui.alert 的方法签名不匹配。 为什么;它的语法看起来和我一样? 任何clues/solutions感激不尽。

我试过使用 const 代替 var,但这没有任何区别

描述

显然 Ui 警报不能接受日期对象。您需要 .toString() 它或使用 Utilties.formatDate() 来显示日期值。

要删除多余的小数位,请使用 .toFixed()

注意,习惯上变量名和函数名的首字母小写。首字母大写通常用于对象 Class 或全局变量。

脚本

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);
} 

参考