[根据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 of the sheet currently, hence the for loop starts at 5
for (i = 5; i <= numRows; i++) {
//Get the document URL from the current row. Currently the second column has the document URLs
var docURL = residentSheet.getRange(i, 2).getValue();
//Extract the document's ID from the URL
var docId = docURL.split("/")[5];
//As long as there's a valid-looking document ID, get the last updated timestamp for the current document in the loop
if (docId != undefined) {
lastRevision = getLastRevision(docId);
Logger.log(lastRevision);
}
else {
lastRevision = "No document URL found";
Logger.log(lastRevision);
}
//Post the last updated timestamp in the appropriate result cell
postLastUpdatedTime(lastRevision, i, 9);
}
//Function to get the last updated timestamp for a given document
function getLastRevision(docId) {
//Try to get the last updated timestamp for the given document ID
try {
var revisions = Drive.Revisions.list(docId);
if (revisions.items && revisions.items.length > 0) {
var revision = revisions.items[revisions.items.length-1];
var lastModified = new Date(revision.modifiedDate);
//var modifiedDateString = Utilities.formatDate(lastModified, ss.getSpreadsheetTimeZone(), "MMM dd, yyyy hh:mm:ss a");
return lastModified;
}
else {
return 'No revisions found.';
}
}
//If the file cannot be accessed for some reason (wrong docId, lack of permissions, etc.), return an appropriate message for posting in the result cell
catch(err) {
return "File cannot be accessed.";
}
}
//Function to post the last updated timestamp for a given document in the given result cell
function postLastUpdatedTime(message, rowIndex, colIndex) {
//If there's no argument is passed to colIndex, set its value to be 11
colIndex = colIndex || 11;
var cellToPost = residentSheet.getRange(rowIndex, colIndex);
try {
cellToPost.setValue(message);
cellToPost.setNumberFormat('MMM dd, yyyy hh:mm:ss AM/PM');
}
catch(err) {
Logger.log(err);
residentSheet.getRange(rowIndex, 12).setValue(err);
}
}
//Update the last refreshed time of the script in the first row of the result column
var scriptUpdatedTime = new Date();
postLastUpdatedTime(scriptUpdatedTime, 1);
}
谁能帮助我了解我哪里出了问题?
PS:我无权删除首先提出此问题的数据验证,因为我只是向客户的现有电子表格中添加功能。
我能够重现您的问题。
已向Google(issuetracker.google.com)报告了此确切问题,他们提出了内部案例[1],涉及Apps Script中的try / catch语句未处理数据验证错误。
[1] https://issuetracker.google.com/issues/36763134#comment7
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句