Google Apps脚本:将错误传递到模板HTML [英] Google Apps Script: passing error into templated HTML

查看:68
本文介绍了Google Apps脚本:将错误传递到模板HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

code.gs:

function onOpen() {
    var ui = SpreadsheetApp.getUi();
    ui.createMenu('My Menu')
      .addItem('Test', 'showTestForm')
      .addToUi();
}

function showTestForm() {
    var html = HtmlService.createHtmlOutputFromFile('TestForm');
    SpreadsheetApp.getUi().showModalDialog(html, 'TEST');
}

function Test(formObject){
  Logger.log("TEST")
  var a = new Error( "Allready present "+ formObject);
  a.error_code = 99;
  Logger.log(JSON.stringify(a));
  throw a;
}

TestForm.html

TestForm.html

<!DOCTYPE html>
<html lang="ru">
<head>
    <meta charset="UTF-8" />
    <base target="_top">
    <script>
        function onFailure(error) {   
            var keys = Object.keys(error);
            alert(JSON.stringify(keys));
            alert(JSON.stringify(error.message));
            alert(JSON.stringify(error.error_code));
        }

        function onSuccess() {
            alert("Success");
        }
    </script>
</head>
<body>
    <input type="submit" value="Save" onclick="google.script.run.withFailureHandler(onFailure).withSuccessHandler(onSuccess).Test('1')" />
    <input type="button" value="Close" onclick="google.script.host.close()" />
</body>
</html>

当我从菜单中打开TestForm并按保存"时,我从Logger获得以下日志:

When I open TestForm from menu and press "Save" I've got following log from Logger:

[18-12-24 23:08:24:765 PST] TEST
[18-12-24 23:08:24:766 PST] {"message":"Allready present 1","error_code":99}

所以我看到,该错误对象具有属性'message'和'error_code'.但是在浏览器中,我收到以下警报:

So I see, that error object have properties 'message' and 'error_code'. But in browser I've got following alerts:

["name"]
"Error: Allready present 1"
undefined

因此,我看到,该接收到的错误对象只有一个空的(我已经检查过)属性名称".但是,如果我只引用属性"message,我在原始对象中有一个字符串(但不相同).而且看起来该对象没有"error_code". 怎么了?

So I see, that recived error object has only one empty (i've checked) property "name". But if I but refer to the property "message, I've got string like in original object (but not the same). And it looks like that object haven't poperty "error_code". What's the matter?

推荐答案

根据 @TheMaster 的建议,有必要做到这一点:

In accordance with the proposal of @TheMaster it is necessary to do this:

code.gs

function Test(formObject){
    var a = new Error( JSON.stringify({msg:"Allready present "+ formObject,code:99}));
    throw a;
}

TestForm.html

TestForm.html

// removing "Error: " from message string to get our json back
var json = error.message.replace("Error: ",'')
var msg = JSON.parse(json).msg;
var code = JSON.parse(json).code;

也就是说,我们将json放入Error对象的属性message中,然后,通过剪切json,我们对其进行解析并获取必要的值.

That is, we put json into the attribute message of the Error object, and then, by cutting our json, we parse it and get the necessary values.

这不完全是问题的答案,而是解决问题的好方法.

This is not exactly the answer to the question, but a good way to solve the problem.

这篇关于Google Apps脚本:将错误传递到模板HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆