尝试...在Google Apps脚本中无法按预期方式捕获

卡尔提克·西瓦苏布拉玛南

[根据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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在 Google Apps 脚本中尝试 Search Console

尝试在 Google Apps 脚本中编写 IF/AND 语句

在Google脚本中尝试/捕获HTTP响应的最佳方法

无法理解为什么尝试和捕获无法在猫鼬中按预期方式工作

Google Apps脚本-尝试在Gmail上按名称查找Google表格

尝试并使用python中的pyodbc捕获异常无法按预期工作

尝试在Google Maps API中捕获错误消息

尝试在Google Apps脚本中获取文件的文件夹路径

在 elif 中尝试除外,无法按预期工作

尝试创建Google通知,未捕获的TypeError错误

尝试在 Google Apps 脚本中拼凑数据时,如何将 getRange() 函数与 forLoop 结合使用?

Google Apps脚本:尝试打开文件夹中的每个文档,提取其目录,然后将其写入其他文档

尝试通过Google App脚本进行POST调用

尝试找到Google Spreadsheet脚本缓存的解决方法

尝试从Google抓取图片

嵌套公式无法按预期的方式运行Google表格

尝试在Google Apps脚本中使用单独的CSS和HTML文件

尝试在Google Apps脚本中使用PHP cURL发布功能时出现401未经授权的错误

尝试创建 Google Cloud 实例时无法更改启动磁盘

尝试在 Google PubSub python 中创建主题订阅时出错

尝试下载Google Storage中的文件返回json

HTML:尝试从嵌入式Google日历中删除元素

尝试在Google表格公式中添加引号时出错

尝试使用 Google Apps 脚本更改 google 文档中的“createdTime”。出现错误 403 ...“字段不可写”。任何解决方法?

在功能和主脚本中尝试捕获?

尝试将Google Sheet脚本连接到Postgres Google Cloud SQL实例

简单尝试/最终与尝试/捕获

尝试-捕获-结束尝试使用

尝试捕获无法捕获UnhandledPromiseRejectionWarning