Javascript 如何操作Google sheets自定义函数中的值?
下面是我计算过滤总和的尝试:Javascript 如何操作Google sheets自定义函数中的值?,javascript,google-sheets,google-sheets-api,Javascript,Google Sheets,Google Sheets Api,下面是我计算过滤总和的尝试: function computeBalance(account, from, to, amounts) { var balance = 0 from.forEach(function (name, index) { if (name == account) balance - toCents(amounts[index].toString()) }) to.forEach(function (name, index) { if (
function computeBalance(account, from, to, amounts) {
var balance = 0
from.forEach(function (name, index) {
if (name == account) balance - toCents(amounts[index].toString())
})
to.forEach(function (name, index) {
if (name == account) balance + toCents(amounts[index].toString())
})
return balance
}
// Some money helpers
function toCents (dollarString) {
return +dollarString.replace(/[\$\,\.]/g, '')
}
function toDollars(cents) {
return (cents / 100)
}
所以,问题是toString()函数。它恰好存在于从amounts数组返回的googlesheets对象上。我不知道如何实际得到值本身,我可以转换成美分的美元字符串,这样我就可以做数学运算了
有什么想法吗?您可以尝试创建自己的“toString”方法,或者尝试在项目中添加引号。是的
(amount[index] + "")
这就产生了和我现在得到的结果一样的结果。该死,既然你有一个使它们成为int的过程,你能把它们作为字符串放到数组中吗?或者,现在看到这一点,可能是因为你把它们转换成字符串,然后从int中减去它们?也许它应该读为(余额+/-金额[i])toString。另外,您在哪里检查它是否成为字符串,看起来您这样做只是为了让字符串通过美分方法,为什么不将其更改为双精度或整数?其中一个挑战是,用于此的调试工具非常糟糕。我所能做的就是在Google Sheets单元格对值进行操作后,查看自定义函数返回的内容。我无法登录到控制台或任何东西。这真的很烦人。嗯,那太糟糕了,我会尝试改变你的“toCents”方法来接受所传递的任何内容,或者制作你自己的“toString”版本并称之为其他东西。我需要一个加号等于,而不仅仅是调整平衡中的加号或减号。不要再在深夜为我编写代码了:(