【问题标题】:Error in Google App Script custom function [duplicate]Google App Script自定义函数中的错误[重复]
【发布时间】:2014-05-13 00:06:28
【问题描述】:

我有一个包含三列的电子表格。所有数字都在华氏列中,其他两个是从函数中填充的。

Fahrenheit  Celsius ComputedFahrenheit
-200
-180
-160
-140
-120
-100
-80
-60
-40
-20
0
32
40
60
80
100
120
140
160
180
200

Celsius 和 ComputedFahrenheit 列使用以下函数填充:

function f2c(fahrenheit) {
  if (typeof(fahrenheit) !== "number") {
    Logger.log(typeof(fahrenheit));
    throw "Fahrenheit column must contain numbers";
  }

  var celsius = (fahrenheit - 32) * (5/9);
  return celsius;
}

function c2f (celsius) {
  if (typeof (celsius) !== "number") {
      throw "celsius must be a number";
  }

  var fahrenheit = (celsius*(9/5)) + 32;
  return fahrenheit;
}

所有单元格都显示正确的数据,第 14 行除外:

40     4.444444444      Error: Loading Data ..

注意:f2c 使用华氏列,c2f 使用摄氏列。

我看不到问题所在!

项目密钥:MESQb2p_hPY7vMP_LZjizjHo2ajWN_uqm

SDC 密钥:df47e1281fe5caea

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    好的,所以这似乎是新 Google 表格中的一个错误,与参数中包含 infinitely repeating decimals 的自定义函数相关,可能与 this similar bug 有关。

    我创建了一个spreadsheet using your functions and data。我发现刷新后该值会计算 sometimes,而其他时候会卡住加载。但是,如果您对来自 c2f()f2c() 的输出进行四舍五入,则代码能够全部返回。

    此代码有效,并且会以相同的精度结束,就好像您不对输出进行四舍五入一样:

    ROUND = Math.pow(10, 12);
    
    function f2c(fahrenheit) {
      if (typeof(fahrenheit) !== "number") {
        Logger.log(typeof(fahrenheit));
        throw "Fahrenheit column must contain numbers";
      }
    
      var celsius = (fahrenheit - 32) * (5/9);
      return Math.round(celsius * ROUND) / ROUND;
    }
    
    function c2f (celsius) {
      if (typeof (celsius) !== "number") {
          throw "celsius must be a number";
      }
    
      var fahrenheit = (celsius*(9/5)) + 32;
      return Math.round(fahrenheit * ROUND) / ROUND;
    }
    

    我已经在the sheet that I created 中实现了它。让我知道这是否有效!

    【讨论】:

    • 当我重新加载时,它显示 40,然后返回错误显示。
    【解决方案2】:

    我刚刚重新创建了您的电子表格和函数,在第 14 行我得到:

    40 4.44444444444444 40

    我建议你试试:

    1. 创建新电子表格

    2. 创建新的 google 脚本文件

    3. 重新测试

    【讨论】:

    • 我创建了一个新的电子表格,新的脚本文件,它显示正确。请参阅下面我对 br1ckb0t 的回复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多