获取Google电子表格中每行最后一列的索引

时间:2012-08-07 15:08:35

标签: google-apps-script google-sheets

我的Google电子表格中包含所有行和列的范围。我正在遍历每一行。如果我的条件语句为真,我需要将一些数据附加到最右边一列的行中。

关于如何实现这一目标的任何指示?谢谢!

2 个答案:

答案 0 :(得分:2)

关键是将范围数据转换为数组;然后遍历数组;然后写回数组。对单元格之类的电子表格对象进行操作效率相对较低。

function writeRows() {
  // This is the named range you have.
  var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName("myRange");

  // Get the data and iterate through it rather than iterate through rows.
  var dataarray = range.getValues();

  // For each row.
  for (var rowpos = 0; rowpos < dataarray.length; rowpos++) {
    // Get the row data as array.
    var rowdata = dataarray[rowpos];
    Logger.log(rowdata.length);

    // Update last column in this row.
    dataarray[rowpos][rowdata.length - 1] = "value to set";
  }
  // Now write back the array values into the range.
  range.setValues(dataarray);
};

答案 1 :(得分:-1)

这就是我用来在电子表格中附加一行的内容:

  var ss = SpreadsheetApp.openById('spredsheetID').getSheets()[0];
  var range = ss.getRange(ss.getLastRow()+1, 1, 1, 2);
  var values = [[new Date(), mystring]]; 
  range.setValues(values); 

它会更新前两列...