我想在code.gs中使用google.script.run调用的函数中抛出一个自定义错误,这样我就可以在侧栏中显示足够的信息。到目前为止,我已经测试了以下代码而没有运气:
code.gs
function UserException(type, text) {
this.type = type;
this.text = text;
//this.stack = (new Error()).stack;
}
UserException.prototype = Object.create(Error.prototype);
UserException.prototype.constructor = UserException;
function assignRangeToTechnician(technician)
{
if(technician!=null)
{
//some code
}else
throw new UserException("Error","Technician was not selected");
}
sidebar.html
...
<script>
function btnSelectTech()
{
google.script.run
.withSuccessHandler(rangeSelected)
.withFailureHandler(techniciansMessage)
.assignRangeToTechnician(document.getElementById('selectTechnician').value);
}
function techniciansMessage(Message)
{
var outputMessage = document.getElementById('message');
//here is where I log the Message value
google.script.run.myLog("In techniciansMessage() - Message: " + Message);
if (Message == null)
outputMessage.innerHTML = "<p style='color:red;'>Error occured</p>";
else
if (Message.type == "Error")
outputMessage.innerHTML = "<p style='color:red;'>" + Message.text + "</p>";
else if (Message.type == "Message")
outputMessage.innerHTML = "<p style='color:#f3f3f3;'>" + Message.text + "</p>";
}
</script>
...
当我运行代码时,会调用.withFailureHandler,但Message
不能保存正确的值。当我记录该消息时,我将“错误:”视为“消息”参数的内容。
答案 0 :(得分:0)
您可以参考此SO thread。尝试在函数中添加错误参数。例如:
google.script.run.withFailureHandler(function (error) {
showError(error, 'getMe');
}).getMe();
可能有用的其他参考资料:https://github.com/google/google-apps-script-samples/blob/master/translate/Sidebar.js.html