简体   繁体   English

Google Apps脚本和Google Spreadsheet中的数组问题

[英]Problems with arrays in Google Apps Script and Google Spreadsheet

Ok, the problem is that there is no way to set the range explictly without all the information on width and height. 好的,问题在于,没有所有关于宽度和高度的信息,就无法明确设置范围。 If I have 13 cells, why wouldn't it just write 13 cells? 如果我有13个单元格,为什么不只写13个单元格呢? So, here is the script: 因此,这是脚本:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = ss.getSheetByName("Analytics");
  var entries = [{
    name : "Read Data",
    functionName : "readRows"
  }];
  ss.addMenu("Script Center Menu", entries);
  var rows = targetSheet.getDataRange();
  var numRows = rows.getNumRows();
  var values = rows.getValues();
  targetSheet.getRange(1,1,values.length,values[0].length).setValues(values);
  values[1] = readRows();
  rows.setValues(values);
};

readRows() returns the array of values. readRows()返回值数组。 It worked with appendRow() but I can't find a way to override the second row in the table without all the complications. 它与appendRow()一起使用,但我找不到一种没有所有复杂性的方法来覆盖表中的第二行。 I tried everything. 我尝试了一切。 This way it keeps complaining about incorrect range height. 这样,它会一直抱怨范围高度不正确。 Some other ways it even wrote the whole array to each cell of the row. 它甚至以其他方式将整个数组写入行的每个单元格。 I just can't get how this thing works. 我只是不知道这件事是如何工作的。 It seems absolutely illogical. 看来绝对不合逻辑。

I mean I'm looking for something like setRow(2, readRows()) that will take exactly as many cells as there are values in the array and will override anything that have been written there before that, if any. 我的意思是我正在寻找类似setRow(2, readRows())东西,该东西将与数组中的值完全一样地容纳单元格,并将覆盖在此之前已写入的所有内容(如果有)。

Ok, found the answer. 好的,找到答案了。 It now looks this way: 现在看起来是这样的:

function onOpen() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var targetSheet = ss.getSheetByName("Analytics");
  var array = [];
  var data = [];
  var entries = [{
    name : "Read Data",
    functionName : "readRows"
  }];
  ss.addMenu("Script Center Menu", entries);
  array = readRows();
  data[0]=array;
  var range = targetSheet.getRange('A2:M2');
  range.setValues(data);
};

The trick is to add the array to another array and achieve the two-dimensional array — the only format the setValues() and getValues() functions work with even if you want to modify a single line. 诀窍是将数组添加到另一个数组中并实现二维数组setValues()getValues()函数可以使用的唯一格式,即使您要修改一行。 It seems that there is no simpler option like setArray() or something like that. 似乎没有更简单的选项,例如setArray()或类似的东西。

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

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