Google表格自定义功能会在移动应用中永久显示“正在加载...”

问题描述 投票:3回答:1

我在Apps脚本中为Google表格编写了一个自定义函数。目标是有一张表自动计算谁欠谁多少钱(例如分开账单)。

我的表看起来像这样:

enter image description here

第一个账单(餐厅)将在所有5个账户中分开,第二个账单将在除Peter之外的所有5个账户中分配,因为B3中没有0。

我的Apps脚本功能的输入将是单元格B1到F3(因此,值和名称)。该功能工作正常 - 它计算正确的结果。我通过浏览器(sheets.google.com)和我的手机应用程序(Google表格)打开该电子表格。但是,在我的手机上经常会发生结果单元格(公式为=calc_debt(B1:F3))只显示"Loading ..."。有什么问题?

为了完整起见,这里是自定义函数的代码:

function calc_debt(input) {
  var credit = [0, 0, 0, 0, 0]; // credit[0] = Peter, credit[1] = Mark ...
  for (var i = 1; i < input.length; i++) { // starting at i = 1 to skip the first row, which is the names!
    // first: calculate how much everybody has to pay
    var sum = 0;
    var people = 0;
    for (var j = 0; j <= 4; j++) {
      if (input[i][j] !== "") {
        sum += input[i][j];
        people += 1;
      }
    }
    var avg_payment = sum / people;
    // second: calculate who has payed too much or too little
    for (var j = 0; j <= 4; j++) {
      if (input[i][j] !== "") {
        credit[j] += input[i][j] - avg_payment;
      }
    }
  }

  // this function is needed later
  function test_zero (value) {
    return value < 0.00001;
  };

  var res = ""; // this variable will contain the result string, something like "Peter to Mark: 13,8 | Katy to ..."

  while (!credit.every(test_zero)) {
    var lowest = credit.indexOf(Math.min.apply(null, credit)); // find the person with the lowest credit balance (will be minus!)
    var highest = credit.indexOf(Math.max.apply(null, credit)); // find the person with the highest credit balance (will be plus!)
    var exchange = Math.min(Math.abs(credit[lowest]), Math.abs(credit[highest])); // find out by how much we can equalize these two against each other
    credit[lowest] += exchange;
    credit[highest] -= exchange;
    res += input[0][lowest] + " to " + input[0][highest] + ": " + exchange.toFixed(2) + "  |  "; // input[0] = the row with the names.
  }
  return res;
}
mobile google-apps-script google-sheets custom-function
1个回答
0
投票

我在Android应用程序中遇到类似的问题,加载自定义公式有时只显示“正在加载...”,而在网络中它总是正常工作。我找到了一个解决方法来加载Android应用程序中的公式:

菜单 - >导出 - >另存为 - > PDF。

这将花费一些时间,在模态加载指示器后面,您将看到配方器最终解决。只要您看到公式得到解决,您就可以等待导出完成或取消导出。

通过菜单切换使文档可脱机也可以解析公式。

您可以做的另一件事是在脚本中使用缓存。因此,无论何时使用Web版本渲染更复杂的公式,结果都会存储并立即加载到移动应用程序中。遗憾的是,Google缓存的时间有限,几小时后无效。有关更多信息,请参见此处:https://developers.google.com/apps-script/reference/cache/

这两件事情很有效。但是,我正在寻找更好的解决方案。如果你找到一个,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.