我在 Apps 脚本中为 Google 表格编写了一个自定义函数。目标是拥有一张自动计算谁欠谁多少钱的表格(例如分摊账单)。
我的工作表如下所示:
第一个帐单(餐厅)将分摊给所有 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/