简体   繁体   English

在 Google 表格单元格 (GAS) 中打印未指定数量的数组内容

[英]Printing Unspecified Number of Array Contents in Google Sheets Cells (GAS)

I am collecting information from a database, based on a filter and is stored in an array of arrays.我正在根据过滤器从数据库中收集信息,并存储在 arrays 数组中。 Once this information is collected I want to be able to print this data onto a new sheet like this:收集到这些信息后,我希望能够将这些数据打印到这样的新工作表上:

___________________________________  
|*****|John Buck, Associate, Luxura|
------------------------------------
|*****|Jane Doe, Manager, Eclipse  |
------------------------------------
|*****|Jim Bob, Executive, Lokia   |
------------------------------------
|*****|Suzzy Sue, Director, RoomX  |
___________________________________

The most important part is that each time I run this function the array may be a different size, so I need it to continue through the array until all records have been printed down the sheet.最重要的部分是,每次我运行这个 function 时,数组的大小可能不同,所以我需要它继续遍历数组,直到所有记录都打印在工作表上。 I've tried a few things, but I am only used to using forEach or for loops when there is a condition, where as this one would just print the specified info in every array.我已经尝试了一些事情,但我只习惯在有条件时使用 forEach 或 for 循环,因为这个只会在每个数组中打印指定的信息。

function testArray() {
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = activeSpreadsheet.getActiveSheet();
  var activeRange = activeSpreadsheet.getActiveRange();
  var activeCell = activeSpreadsheet.getActiveCell();

  var startCell = activeSheet.getRange(5, 4);
  var ratingStartCell = activeSheet.getRange(5, 3);

  var newArray = [
    ["John Buck", "*****", "Associate", "Luxura"][
      ("Jane Doe", "****", "Manager", "Eclipse")
    ][("Jim Bob", "***", "Executive", "lokia")][
      ("Suzzy Sue", "*****", "Director", "RoomX")
    ],
  ];

  newArray.forEach((r, o) => {
    startCell.setValues(
      newArray[r][0] +
        ", " +
        newArray[r][2] +
        ", " +
        newArray[r][3] +
        ", " +
        newArray[r][4]
    );
  });

  newArray.forEach((r, o) => {
    ratingStartCell.setValues(newArray[r][2]);
  });
}

You could post the newArray to activeSheet in one go using the setValues(values) method .您可以使用setValues(values) 方法在一个 go 中将 newArray 发布到 activeSheet。

This is more efficient than writing to the sheet on cell at a time.这比一次写入单元格上的工作表更有效。 See Best Practices .请参阅最佳实践

activeSheet.getRange(1, 1, newArray.length, newArray[0].length).setValues(newArray);

The size of the array does not need to be specified.不需要指定数组的大小。 newArray.length is the number of rows and newArray[0].length is the number of columns. newArray.length是行数, newArray[0].length是列数。

You need to be careful when constructing the array: each row must have the same number of columns.构造数组时需要小心:每行必须具有相同的列数。 If the number of columns is different, you'll get an error and the setValues() will fail.如果列数不同,您将收到错误并且setValues()将失败。

By the way, I modified your array.顺便说一句,我修改了你的数组。 I'm not sure why it contains "()", you can refer to JavaScript Array to learn more on how to declare and use arrays.我不知道为什么它包含“()”,你可以参考JavaScript 数组来了解更多关于如何声明和使用 arrays。

You can refer to this sample code:你可以参考这个示例代码:

function testArray() {
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = activeSpreadsheet.getActiveSheet();
  Logger.log(activeSheet.getSheetName());

  var newArray = [
    ["John Buck", "*****", "Associate", "Luxura"],
    ["Jane Doe", "****", "Manager", "Eclipse"],
    ["Jim Bob", "***", "Executive", "lokia"],
    ["Suzzy Sue", "*****", "Director", "RoomX"]
  ];
  Logger.log(newArray);

  //Create new array that will contain expected row values
  var rowValues = [];
  newArray.forEach((r,o)=>{
    //Push a 2-D array, Col1 will be the rating (****) while Col2 will be the remaining strings
    //separated by a comma and space
    rowValues.push([r[1], r[0]+", "+r[2]+", "+r[3]]);    
  });
  Logger.log(rowValues);
  //Set the values for the selected range, using the size of the 2-d array starting from row 5.
  activeSheet.getRange(5,3,rowValues.length,rowValues[0].length).setValues(rowValues);
 
}

What it does?它能做什么?

  1. After fixing your array, I created a new array rowValues which will contain 2 information:修复数组后,我创建了一个新数组rowValues ,其中包含 2 个信息:
    • Rating评分
    • Concatenated strings separated by a comma and a space.由逗号和空格分隔的串联字符串。
  2. Once I have created the desired grouping and output for each row.一旦我为每一行创建了所需的分组和 output。 I now set the values of the selected range based on the rowValues created using Sheet.getRange(row, column, numRows, numColumns) and Range.setValues(values)我现在根据使用Sheet.getRange(row, column, numRows, numColumns)Range.setValues(values)创建的rowValues设置所选范围的值

I set the start row to 5 and start column to 3 based on your earlier example.根据您之前的示例,我将起始行设置为 5,将起始列设置为 3。 Feel free to adjust your start row and column index based on your preference.随意根据您的喜好调整起始行和列索引。

OUTPUT: OUTPUT:

在此处输入图像描述

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

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