[英]google apps script loop through range argument
I'm trying out google apps script editor and a simple for loop gives me weird results. 我正在尝试使用Google Apps脚本编辑器,而一个简单的for循环给了我奇怪的结果。
Say I have a spreadsheet 说我有一个电子表格
in script editor i write a function: 在脚本编辑器中,我编写了一个函数:
function calculate(array) {
var result = 0;
for (var i = 0; i < array.length; i++) {
result += array[i];
}
return result;
}
I would expect a returned value to be 9 but instead I get 045 returning array.length I get 2 as expected returning array[0] I get 4 as expected 我希望返回的值是9,但是相反,我得到045返回的array.length我得到的是预期的2返回array [0]我得到的是预期的4
I tried googling it but i found some weird workarounds using array.map function. 我尝试使用Google搜索,但是使用array.map函数发现了一些怪异的解决方法。 Am I doing something wrong, is it not supposed to be plain javascript?
我在做错什么,不是应该使用纯JavaScript吗?
Got it. 得到它了。 I had to convert values to number:
result += Number(array[i]);
我不得不将值转换为数字:
result += Number(array[i]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.