0

我希望我的函数能够处理作为常规数字、百分比或货币传递的单元格值。目前只有纯数字有效,但当我用货币尝试时,它会产生错误:

/**
 * Returns the sum of a geometric serie
 *
 * @param {number} a0 The first element of the serie
 * @param {number} q The ratio
 * @param {number} n Number of elements to sum
 * @return The sum of first n elements.
 * @customfunction
 */
function SUMGEOMSERIE(a0, q, n) {

  if (typeof a0 != 'number') {//|| (typeof q != 'number') || (typeof n != 'number') {
      return null;
   }

  if (q==1)
    return a0*n;
  else
    return a0*(1-Math.pow(q, n))/(1-q);
}

更新

当我作为输入传递给函数的单元格是“复杂”计算(3 步“回溯”)的结果时,会发生错误。

4

1 回答 1

0

百分比值在 AppScript 中变为十进制数(请参阅此处),所以应该没问题。如果您想将其作为一个整数,只需乘以 100。对于货币,您可以setNumberFormat在要检索的范围上使用。您可以传递您希望数字采用的任何格式(例如:“0,000.00”)。

于 2015-03-27T15:58:14.893 回答