简体   繁体   English

Google Apps 脚本中的 getValues() 如何返回“未定义”值?

[英]How did getValues() in Google Apps Script return a 'undefined' value?

I was trying to count the number of filled cells in a column and once it is empty, the count is supposed to stop.我试图计算一列中填充单元格的数量,一旦它为空,计数就应该停止。 As such I imposed this condition:因此,我强加了这个条件:

sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1')
numberOfRows = 0
while(sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues()[numberOfRows] != '') {
  numberOfRows ++
}

I turned on the debugger on Google Apps Script and found out that the above while loop did not stop.我在 Google Apps Script 上打开调试器,发现上面的 while 循环并没有停止。 Upon further testing, I found that the cells that were empty were 'undefined', instead of the conventional empty string that they were supposed to return.经过进一步测试,我发现空单元格是“未定义”的,而不是它们应该返回的传统空字符串。 Any possible reason that the cells could return an 'undefined' instead of ''?单元格可能返回“未定义”而不是“”的任何可能原因?

The undefined values are not the issue, the loop is not finishing because each row is an Array, and an Array is never equal to ''.未定义的值不是问题,循环没有结束,因为每一行都是一个数组,而数组永远不会等于 ''。

When the code calls sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues()[numberOfRows] the return value is an Array of columns.当代码调用sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues()[numberOfRows]时,返回值是一个列数组。 If you selected 3 columns in your range, a row would look something like: ['A','B','C'] .如果您在范围内选择了 3 列,则一行将类似于: ['A','B','C']

To access the value of a column in the row, you need to specify it's index.要访问行中列的值,您需要指定它的索引。 In this case, you are only selecting a single columnm ( ['A'] ), so the index is 0.在这种情况下,您只选择了一个列 ( ['A'] ),因此索引为 0。

Optionally, you might find the code is cleaner if you put the values in a variable first, this is a style choice.或者,如果您先将值放入变量中,您可能会发现代码更清晰,这是一种风格选择。

  var values = sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues();
  var numberOfRows = 0;

  while(values[numberOfRows][0] != '' ){
     numberOfRows++;
  }

The code has some problems:代码有一些问题:

  1. as mentioned in a question's comment, it's not a good idea to use getValues() in a loop如问题评论中所述,在循环中使用getValues()不是一个好主意
  2. the following expression can't be visualized in the Google Apps Script editor debugger以下表达式无法在 Google Apps 脚本编辑器调试器中可视化

    sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues()[numberOfRows]
  3. the above expression returns an Array, not a single value.上面的表达式返回一个数组,而不是单个值。
  4. the loop doesn't stops when the last row has been reached.当到达最后一行时,循环不会停止。


The assumption that getValues() returned undefined is wrong. getValues()返回undefined的假设是错误的。 That's is caused by numberOfRows having a value equal or greater than the number of elements returned by getValues() as JavaScript arrays indexes are zero based. 这是由numberOfRows的值等于或大于getValues()返回的元素数引起的,因为 JavaScript 数组索引是从零开始的。

In order to able to use the Google Apps Script debugger you should add more code lines为了能够使用 Google Apps 脚本调试器,您应该添加更多代码行

var values = sheet.getRange(2,1,sheet.getLastRow()-1,1).getValues();

Regarding the关于

I was trying to count the number of filled cells in a column and once it is empty, the count is supposed to stop.我试图计算一列中填充单元格的数量,一旦它为空,计数就应该停止。

I think that a better wording is "find the first empty cell".我认为更好的措辞是“找到第一个空单元格”。 Anyway, it should stop also when the last row of the the column is reached.无论如何,当到达该列的最后一行时,它也应该停止。

var numberOfRows = 0;
while(numberOfRows < values.length && values[numberOfRows][0] !== ''){
  numberOfRowss++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM