gpt4 book ai didi

javascript - Try...catch 在 Google Apps 脚本中无法正常工作

转载 作者:行者123 更新时间:2023-12-04 13:17:38 25 4
gpt4 key购买 nike

[根据 T.J. Crowder 的建议进行了编辑以包含一个最小的可重现示例.]

我正在使用 Google Apps 脚本开发一个简单的函数,该函数应该将日期错误字符串 发布到电子表格的列中。该列已有数据验证规则,拒绝任何不是有效日期的值。这一切都很好。

我的问题是:

我尝试使用 try...catch block 来优雅地处理错误,并在值未通过数据验证时仅记录错误消息。 try...catch 似乎根本不起作用。相反,脚本会抛出错误并中断,并且日志为空。

这是使用新代码更新后的屏幕截图(抱歉,Sourabh Choraria,用于覆盖您的更新)。令人惊讶的是,GAS 会在应该发生错误的位置上方突出显示一行。

Screenshot for the updated code

作为背景知识,此脚本获取列中存储的各种其他电子表格的 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:我无权删除一开始出现此问题的数据验证,因为我只是向客户的现有电子表格添加功能。

最佳答案

我能够重现您的问题。

这个确切的问题已经报告给谷歌 (issuetracker.google.com),他们提交了一个内部案例 [1] 关于 Apps 脚本中的 try/catch 语句不处理数据验证错误。

[1] https://issuetracker.google.com/issues/36763134#comment7

关于javascript - Try...catch 在 Google Apps 脚本中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58604400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com