mobile - Google Sheets 自定义函数在移动应用程序中永远显示 "Loading..."

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

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

我的工作表如下所示:

enter image description here

第一个帐单(餐厅)将分摊给所有 5 个帐单,第二个帐单将分摊给除 Peter 之外的所有 5 个帐单,因为 B3 中没有 0。

我的 Apps 脚本函数的输入将是单元格 B1 到 F3(因此,值和名称)。该函数运行良好 - 它计算出正确的结果。我通过浏览器 (sheets.google.com) 和我的手机应用程序 (Google Sheets) 打开该电子表格。但是,在我的手机上,经常会出现结果单元格(公式为 =calc_debt(B1:F3))仅显示 “正在加载...”。有什么问题吗?

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

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;
}

最佳答案

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

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

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

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

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

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

关于mobile - Google Sheets 自定义函数在移动应用程序中永远显示 "Loading...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52741098/

相关文章:

javascript - 在手机上点击 'Go' 时停止表单提交

google-sheets - 在单个单元格中查找多个值(逗号分隔),然后将值返回到单个单元格(也以逗号分隔)

json - Google Scripts WebApp POST 正文

google-apps-script - 具有可安装日历事件触发器的工作区附加组件停止与新部署配合使用

google-apps-script - 将电报机器人与谷歌应用程序脚本连接

google-sheets - 谷歌表格: Whats the formula to count all Sundays from a list of dates

CSS如何防止键盘将内容向上移动?

javascript - JQuery Mobile 在离开时不会更新或重新发布数据

mobile - 使用 Ember.js 构建移动混合应用程序

javascript - 找不到函数 stringify Google App 脚本