您如何从 google 应用程序脚本传回自定义错误消息? [英] How do you pass back a custom error message from google apps scripts?

查看:21
本文介绍了您如何从 google 应用程序脚本传回自定义错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从谷歌电子表格运行谷歌应用程序脚本时,如果谷歌 API 之一使用不当,电子表格顶部会显示红色butterbar"错误.此消息通常包含对脚本开发人员有用的信息(来自 google api 的错误消息,例如范围的坐标或尺寸无效.")但不一定对电子表格用户有用(他们的真实世界翻译可以解决它).

When running a google apps script from a google spreadsheet, if one of the google apis is used incorrectly a red "butterbar" error is shown at the top of the spreadsheet. This message usually contains info that is useful to the script developer (an error message from a google api, e.g. "The coordinates or dimensions of the range are invalid.") but not necessarily to the spreadsheet user (a real world translation of what they can do to resolve it).

我搜索了 UiApp api 文档,但没有没有看到自定义此消息的方法.是否可以抛出您自己的错误消息?

I searched through the UiApp api documentation but didn't see a way of customizing this message. Is it possible to throw your own error message?

推荐答案

与任何 javascript 一样,您可以使用:

As with any javascript, you can use:

try {
  ...
}
catch (error) {
  throw new Error( "More meaningful error." );
{

大量示例在使用,即使问题不完全是你的问题.

There are numerous examples of this in use, even if the questions aren't exactly yours.

我个人的意见是,最好检查函数的输入并抛出错误(例如 this answer),而不是从服务调用中捕获错误.使用 try..catch 的合适时机是当您没有实用的方法来验证参数时,如此答案.

My personal opinion is that it's best if you check the input to your function and throw errors on that (like this answer), rather than catching the errors from service calls. An appropriate time to use try..catch would be when you have no practical way to validate parameters, as in this answer.

这篇关于您如何从 google 应用程序脚本传回自定义错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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