首页 > 解决方案 > GAS onFormSubmit 触发器在第一个 console.log 之后返回

问题描述

问题:对于某些用户,我只能看到第一个 logToConsole()。然后似乎该函数返回而没有抛出错误或其他任何东西。

到目前为止我已经检查/尝试过的内容:

function onSubmitInst(event) {
    logToConsole('onSubmitInst start'); // Is logged

    var form, docProps, valueStatus, formLimits, realFormValueCell, mailFormStatus, formTitle;

    try {
        docProps = PropertiesService.getDocumentProperties();
    } catch (error) {
        logToConsole('onSubmitInst property error'); // Is NOT logged
        return;
    }

    form = event.source;
    formTitle = form.getTitle();

    mailFormStatus = docProps.getProperty('mailForm');

    formLimits = docProps.getProperty('formLimits');
    formLimits = formLimits ? JSON.parse(formLimits) : {};

    logToConsole('onSubmitInst OK'); // Is NOT logged

    // ... more code ...
}

标签: google-apps-script

解决方案


如果脚本作为附加组件发布或绑定到文档,则从文档中PropertiesService.getDocumentProperties()返回有效对象。Properties否则返回null

无论哪种方式,它都不会触发您的try -> catch,null返回不是异常并且不会生成错误消息。

您必须检查返回的对象是否null有效,并相应地调整您的逻辑。

    docProps = PropertiesService.getDocumentProperties();
    if (docProps === null) {
        logToConsole('onSubmitInst property returned null');
        return;
    }

参考:

类属性服务


推荐阅读