Throwing custom exceptions and error messages in Google Sheets custom function?

后端 未结 3 1029
臣服心动
臣服心动 2021-01-17 22:35

In Google Sheets (as with Excel, etc) if a user enters bad input into a formula, an error code will be printed in the offending cell and a small pop-up provides more detail

相关标签:
3条回答
  • 2021-01-17 23:14

    Use try...catch and the message property of the error object, then return the error message instead of throwing an error. I.E.:

    /**
     *
     * @customfunction
     */
    function myDiv(dividend,divisor){
      var quotient;
      try{
         quotient = dividend / divisor;
      } catch(error) {
         quotient = error.message;
      } finally {
         return quotient;
      }
    
    }
    

    Reference

    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
    • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/Error
    0 讨论(0)
  • 2021-01-17 23:26

    This is a reported problem.

    Visit Issue 4422, star it to vote and for updates.

    0 讨论(0)
  • 2021-01-17 23:27
    if (some_condition)
    {
      // will end execution with error    
      throw 'Error. My custom error description.';
    }
    
    0 讨论(0)
提交回复
热议问题