[编辑以包含TJ Crowder建议的最小可重现示例。]
我正在使用 Google Apps 脚本开发一个简单的函数,该函数应该将日期或错误字符串发布到电子表格的列中。该列已具有拒绝任何非有效日期值的数据验证规则。这一切都很好。
我的问题是这样的:
我尝试使用 try...catch 块来优雅地处理错误,并在值未通过数据验证时仅记录错误消息。try...catch 似乎根本不起作用。相反,脚本会抛出错误并中断,并且日志显示为空。
这是带有新代码的更新屏幕截图(抱歉,Sourabh Choraria,覆盖了您的更新)。令人惊讶的是,GAS 突出显示了错误发生位置上方的一行。
作为一点背景知识,此脚本获取存储在列中的各种其他电子表格的 ID,获取每个电子表格的最后更新时间戳,并将结果发布到结果列中。
这是我使用的代码。
function trackDocUpdates() {
//Set the global variables
var ss = SpreadsheetApp.getActive();
var residentSheet = ss.getSheetByName("Resident Documents");
var activeRange = residentSheet.getDataRange();
var numRows = activeRange.getNumRows();
var lastRevision = "No value yet.";
//Loop through the rows of data to get the last updated timestamp of each document
//The first data row is the 5th row …
Run Code Online (Sandbox Code Playgroud) javascript validation try-catch google-sheets google-apps-script