Q: Loop in Google Apps Script Error: Cannot read property "length" of undefined
Q: Loop in Google Apps Script Error: Cannot read property "length" of undefined
我得到了 Error: Cannot read property length of undefined
但不太明白这个问题。
从Whosebug的其他帖子我发现函数无法确定解析对象的长度。
我正在做的是:
我确实导入了历史数据,例如google 股票价格 =Index(GOOGLEFINANCE("NASDAQ:GOOG";"price";HEUTE()-60;HEUTE());;2)
到我的电子表格中。
然后我用 =average(A128:A148)
.
从数据中计算出 21 个简单移动平均线
现在我比较股价和 21 SMA 以确定股价是高于还是低于 21 SMA。到目前为止,一切都很好。
最后,我尝试编写一个函数,从最新值开始计算 21 SMA 以上的天数。
function daysabove(input){
var output = 0;
for (var i = input.length-1;i>= 0;i--){
if (input[i][0]=="over" ){
if (input[i+1][0]=="under"){
output++;
break;
}
else {
output++;
}
} `
else {
output--;
}
}
return output;}
在电子表格上,我用 =daysabove(C129:C148)
调用该函数。 C129 至 C148 包含以上或以下。
编辑:我什至尝试 运行 范围内包含手动编写的 "above" 和 "under" 的函数,以便与 google 金融没有任何联系。还是不行。
所以回到问题:应该定义长度或对象。这里有什么问题吗?
Error: Cannot read property length of undefined
发生这种情况是因为 input
未定义,从脚本编辑器调用时没有长度 属性,因为您没有将任何值作为输入传递给 function daysabove
.
Error: Cannot read property[0] from undefined
这是因为 i+1
的计算结果超出了索引范围。比如说,你执行 =daysabove(C1:C4)
。 C1:C4 都将是 "over".
for (var i = input.length-1;i>= 0;i--){
if (input[i][0]=="over" ){
if (input[i+1][0]=="under"){
input.length
将是 4;
input.length-1
将是 3;
i
初始化为3;
请注意,在 javascript 数组中,第一个元素的索引为 0;
["over","over","over","over"]的索引从前到后分别为0,1,2,3;
input[i][0]
== input[3][0]
将是 C1:C4 中的最后一个元素;
下一条语句调用 i+1,即 input[4]
,即 undefined
。未定义不是数组。因此我们无法读取 undefined.
的 属性 [0]
如果你要做一个自下而上的循环,那么确保一切都是相反的。因此,可以通过使用 input[i-1][0]
作为下一个语句并将 for-condition 更改为 i>0
来修复循环。还有其他语法错误。考虑查看 arrays and taking a tutorial
无论如何,
如 Official documentation、
中所写
Historical data cannot be downloaded or accessed via the Sheets API or Apps Script. If you attempt to do so, you will see a #N/A error in place of the values in the corresponding cells of your spreadsheet.
您的脚本在任何情况下都会失败,因为它引用了历史 google 财务数据。
如果你只想计算上面天数下面的天数,使用
=COUNTIF(C1:C4,"over")-COUNTIF(C1:C4,"under")
我得到了 Error: Cannot read property length of undefined
但不太明白这个问题。
从Whosebug的其他帖子我发现函数无法确定解析对象的长度。
我正在做的是:
我确实导入了历史数据,例如google 股票价格 =Index(GOOGLEFINANCE("NASDAQ:GOOG";"price";HEUTE()-60;HEUTE());;2)
到我的电子表格中。
然后我用 =average(A128:A148)
.
现在我比较股价和 21 SMA 以确定股价是高于还是低于 21 SMA。到目前为止,一切都很好。
最后,我尝试编写一个函数,从最新值开始计算 21 SMA 以上的天数。
function daysabove(input){
var output = 0;
for (var i = input.length-1;i>= 0;i--){
if (input[i][0]=="over" ){
if (input[i+1][0]=="under"){
output++;
break;
}
else {
output++;
}
} `
else {
output--;
}
}
return output;}
在电子表格上,我用 =daysabove(C129:C148)
调用该函数。 C129 至 C148 包含以上或以下。
编辑:我什至尝试 运行 范围内包含手动编写的 "above" 和 "under" 的函数,以便与 google 金融没有任何联系。还是不行。
所以回到问题:应该定义长度或对象。这里有什么问题吗?
Error: Cannot read property length of undefined
发生这种情况是因为 input
未定义,从脚本编辑器调用时没有长度 属性,因为您没有将任何值作为输入传递给 function daysabove
.
Error: Cannot read property[0] from undefined
这是因为 i+1
的计算结果超出了索引范围。比如说,你执行 =daysabove(C1:C4)
。 C1:C4 都将是 "over".
for (var i = input.length-1;i>= 0;i--){
if (input[i][0]=="over" ){
if (input[i+1][0]=="under"){
input.length
将是 4;
input.length-1
将是 3;
i
初始化为3;
请注意,在 javascript 数组中,第一个元素的索引为 0;
["over","over","over","over"]的索引从前到后分别为0,1,2,3;
input[i][0]
== input[3][0]
将是 C1:C4 中的最后一个元素;
下一条语句调用 i+1,即 input[4]
,即 undefined
。未定义不是数组。因此我们无法读取 undefined.
的 属性 [0]
如果你要做一个自下而上的循环,那么确保一切都是相反的。因此,可以通过使用 input[i-1][0]
作为下一个语句并将 for-condition 更改为 i>0
来修复循环。还有其他语法错误。考虑查看 arrays and taking a tutorial
无论如何,
如 Official documentation、
Historical data cannot be downloaded or accessed via the Sheets API or Apps Script. If you attempt to do so, you will see a #N/A error in place of the values in the corresponding cells of your spreadsheet.
您的脚本在任何情况下都会失败,因为它引用了历史 google 财务数据。
如果你只想计算上面天数下面的天数,使用
=COUNTIF(C1:C4,"over")-COUNTIF(C1:C4,"under")