自定义函数,用于google脚本中几何数字和货币系列的总和

时间:2015-03-27 08:14:32

标签: google-apps-script google-sheets custom-function

我希望我的函数能够处理以常规数字,百分比或货币形式传递的单元格值。目前只有普通数字可以使用,但是当我用货币尝试它时会产生错误:

/**
 * 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步“回溯”)时发生错误。

1 个答案:

答案 0 :(得分:0)

百分比值在AppScript中变为十进制数(请参阅here),因此应该没问题。如果您希望将整数作为整数乘以100.对于货币,您可以在要检索的范围上使用setNumberFormat。您可以传入您想要的数字格式(例如:" 0,000.00")。