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

Rob*_*ert 6 mobile google-sheets google-apps-script custom-function

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

我的工作表是这样的:

在此处输入图片说明

第一张帐单(餐厅)要分给所有 5 人,第二张帐单要分给除彼得以外的所有 5 人,因为 B3 中没有 0。

我的 Apps 脚本函数的输入将是单元格 B1 到 F3(因此,值和名称)。该函数工作正常 - 它计算出正确的结果。我通过浏览器 (sheets.google.com) 和我的手机应用程序 (Google Sheets) 打开该电子表格。但是,在我的手机上,结果单元格(带有公式=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;
}
Run Code Online (Sandbox Code Playgroud)

Stu*_*t92 4

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

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

这将需要一些时间,在模态加载指示器后面,您将看到公式最终得到解析。您可以等待导出完成,或者在看到公式已解决后立即取消导出。

此外,通过菜单切换使文档离线可用也可以解决公式问题。

您可以做的另一件事是在脚本中使用缓存。因此,每当您使用网络版本渲染更复杂的公式时,结果都会被存储并立即加载到移动应用程序。不幸的是,谷歌缓存有时间限制,几个小时后就会失效。请参阅此处了解更多信息: https ://developers.google.com/apps-script/reference/cache/

这两件事运作得很好。但是,我正在寻找更好的解决方案。如果你找到了请告诉我。