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

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

问题描述

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



我通过 UiApp api文档,但没有看到自定义此消息的方式。是否有可能抛出自己的错误信息?

解决方案

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

  try {
...
}
catch(error){
throw new Error(更有意义的错误。);
{

这个使用中有很多例子,即使这些问题不完全属于你。

我个人的观点是,如果你检查你的函数的输入并抛出错误(如这个答案),而不是从服务调用中捕获错误。如果您没有实际的验证参数的方法,则可以使用try..catch的适当时间,如此答案

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).

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?

解决方案

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.

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.

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

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