How do you pass back a custom error message from google apps scripts?

后端 未结 3 1619
甜味超标
甜味超标 2020-12-16 13:15

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.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-16 13:18

    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.

提交回复
热议问题