[英]Cannot read property “0” from undefined. Error Google Apps Script
I´m geting Cannot read property "0" from undefined. 我正在获取无法从未定义中读取属性“ 0”。 Error on line 16. ( while (colunaDias[emptyCell][0] <= dias) )
第16行发生错误。(而(colunaDias [emptyCell] [0] <= dias))
It should be a very simple function for google SpreadSheets. 对于Google SpreadSheets,这应该是一个非常简单的功能。 I can´t see what I´m doing wrong...
我看不到我在做什么错...
The bizarre thing is that if I don´t use the variable "dias" and use a integer instead. 奇怪的是,如果我不使用变量“ dias”,而是使用整数。 The while function works....
while函数起作用。
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var leadsSheet = ss.getSheetByName("Leads Todas as Categorias, menos outros - Days (5days)");
var targetSheet = ss.getSheetByName("feticaria");
var cellLeads = leadsSheet.getRange(1,1).getValue();
//var cellTarget = targetSheet.getRange(1,1).setValue(valor);
var colunaDias = leadsSheet.getRange('B:B').getValues();
var sourceMedium = leadsSheet.getRange('A:A').getValues();
var emptyCell = 16;
var dias = 1;
while (colunaDias[emptyCell][0] != ""){
while (colunaDias[emptyCell][0] <= dias){
dias++;
emptyCell++;
}
emptyCell++;
}
Logger.log(emptyCell);
}
I'm guessing that colunaDias[emptyCell]
is undefined. 我猜
colunaDias[emptyCell]
是未定义的。 It passes the first while
condition, because "undefined" is not equal to "". 它通过了第一个
while
条件,因为“ undefined”不等于“”。 If colunaDias[emptyCell]
is undefined, then either there is something wrong with this line: 如果未定义
colunaDias[emptyCell]
,则此行有问题:
var colunaDias = leadsSheet.getRange('B:B').getValues();
or 要么
colunaDias[emptyCell][0]
Where "emptyCell" is 16 is the problem. 问题是“ emptyCell”是16。
getValues()
returns an object of rectangular grid of values. getValues()
返回矩形值网格的对象。 I would test to see if there is anything in the rectangular grid, by checking [0][0]. 我将通过检查[0] [0]来测试矩形网格中是否有任何东西。
Logger.log('is there any data? ' + colunaDias[0][0])
If there is no data, then the something failed on line: 如果没有数据,则某些操作失败:
var colunaDias = leadsSheet.getRange('B:B').getValues();
If that line is working, then something higher up is wrong. 如果那条线工作正常,那么更高的位置是错误的。
You should be checking the return type of getSheetByName
for null. 您应该检查
getSheetByName
的返回类型getSheetByName
为null。
// The code below will log the index of a sheet named "YourSheetName"
var leadsSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("YourSheetName");
if (leadsSheet != null) {
Logger.log(leadsSheet.getIndex());
}
I think the only thing that could cause that error is if emptyCell
is bigger than the colunaDias
array, ie if your sheet is smaller than 16 rows (if the value you show here is correct). 我认为,可能导致该错误的唯一原因是
emptyCell
大于colunaDias
数组,即,如果工作表小于16行(如果此处显示的值正确)。
Add this line right before the first while
: 在第一
while
之前添加此行:
Logger.log('emptyCell = '+emptyCell+' and colunaDias.length = '+colunaDias.length);
I tested a copy of your script and it runs without error except if I define emptyCell
> 1000 on a 1000 rows Sheet. 我测试了脚本的副本,该脚本的运行没有错误,除非我在1000行工作表上定义
emptyCell
> 1000。
I know this post in from a few years ago, but this was the closest to a problem I am having. 我知道几年前的这篇文章,但这是我遇到的最接近的问题。 I just want to post my answer in case someone else ends up here one day.
我只想发布我的答案,以防其他人有一天在这里结束。
For some reason the check variable "dias" is being passed as a string. 由于某种原因,检查变量“ dias”作为字符串传递。 That's why replacing it with a number allows the script to run.
这就是为什么用数字替换它可以使脚本运行。
dias = parseInt(dias); //use this before the variable needs to be read
I cant say why it is passing a string after being ++ but this will fix it 我不能说为什么它在被++之后传递字符串,但这会解决它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.