简体   繁体   English

Google Sheets App Script中数组的长度

[英]Length of an array in Google Sheets App Script

I'm trying to determine the length of an array, so that I can use the length to determine how many times a for loop will need to iterate.我正在尝试确定数组的长度,以便可以使用长度来确定 for 循环需要迭代多少次。

I'm trying to read from 1 row across many columns to determine which cells need to be written to.我正在尝试从多列中的 1 行读取以确定需要写入哪些单元格。 The code will write data to the rows, but will need to determine if the header has already been written or if the header needs writing also.代码会将数据写入行,但需要确定 header 是否已写入,或者 header 是否也需要写入。 In this function I am focusing on checking the headers to see if the value is already written.在这个 function 中,我专注于检查标题以查看值是否已写入。

I understand that the range values D2:Z2 is inefficient and I am open to another way around this.我知道范围值 D2:Z2 效率低下,我愿意接受另一种解决方法。 I'd be interested to know if anyone is able to determine why the length is 1.0 instead of 4, and if anyone else can think of a way to compare the values, the then determine the column to write the new set of headers in.我很想知道是否有人能够确定为什么长度是 1.0 而不是 4,如果其他人能想出一种比较值的方法,然后确定写入新标题集的列。

Code代码

 function getCurrentHeader() {

  //gets active spreadsheet, saves to array
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Records");
  var rangeValues = Array;
  //D2:Z2 is the current largest range available 15/12/21 
  rangeValues = sheet.getRange("D2:Z2").getValues(); 
  return rangeValues;
}


function checkCurrentHeaders(){
  //gets the array of headers
  var current = getCurrentHeader();
  //Logger.log(current);
  var leng = current.length;

  Logger.log(leng);

}

The above is what i've written so far, but even with the array being logged to view the values, the.length is returning a value of 1, there are 4 values in the array so i was expecting 4 as the return.以上是我到目前为止所写的内容,但即使记录了数组以查看值,the.length 返回值 1,数组中有 4 个值,所以我期待 4 作为返回值。 I've also noted that the output is in two sets of square brackets.我还注意到 output 在两组方括号中。 Please see the log information below.请参阅下面的日志信息。

Logger记录器

11:52:32 AM Notice  Execution started
11:52:32 AM Info    [[B, A, C, DF, , , , , , , , , , , , , , , , , , , ]]
11:52:32 AM Info    1.0
11:52:32 AM Notice  Execution completed

getValues() returns 2 dimensional array. getValues()返回二维数组。 In your script, rangeValues = sheet.getRange("D2:Z2").getValues() is the 2 dimensional array like [[D2, E2, F2,,,]] .在您的脚本中, rangeValues = sheet.getRange("D2:Z2").getValues()是二维数组,如[[D2, E2, F2,,,]] By this, when you check current.length of var current = getCurrentHeader() , 1 is returned.这样,当您检查var current = getCurrentHeader()current.length时,将返回1 In this case, 1 is the number of rows.在这种情况下, 1是行数。 I think that this is the reason of your issue.我认为这是您的问题的原因。

When you want to retrieve the number of columns, please modify as follows.当您要检索列数时,请进行如下修改。

From:从:

var leng = current.length;

To:至:

var leng = current[0].length;

or或者

From:从:

rangeValues = sheet.getRange("D2:Z2").getValues(); 

To:至:

rangeValues = sheet.getRange("D2:Z2").getValues()[0];

or或者

From:从:

rangeValues = sheet.getRange("D2:Z2").getValues(); 

To:至:

rangeValues = sheet.getRange("D2:Z2").getValues()[0].filter(String); // In this case, the empty cells are ignored.

References:参考:

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

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