Google脚本可将特定列从一个工作表复制到另一个工作表的最后一行onEdit

时间:2014-08-04 01:04:06

标签: google-apps-script google-sheets

我正在寻找谷歌应用脚​​本的一些帮助。目前我有一张3张电子表格。第一张是来自谷歌表单的数据。第二张是"开放"选项卡和第三个是"关闭"标签。

当数据从表单进入电子表格时,它只包含表单所具有的列。 "打开" sheet具有来自表单(表单1)和一些其他单元格的数据组合,以向我添加更多信息,而不是提交表单的人员。由于此列匹配不匹配,我不能只将整行从表1复制到表2.这也需要在编辑触发器上完成。因此,当编辑触发器触发时,我想将该行数据存储到存储器中,然后将列B复制到选项卡2的列C的最后一行。列C> E,列D> B.等..

如何将整行存储到内存(我假设的数组)中,然后将特定单元格按特定顺序复制到不同工作表的最后一行?

希望有道理: - /

2 个答案:

答案 0 :(得分:2)

正如您所说,您必须使用数组来获取数据并将其写回所选订单中的另一个工作表。 这是非常基本的操作,有很多方法可以实现它。 一种非常容易理解的方法如下:

  • 获取数组中的行数据
  • 逐个获取每个值并存储在另一个数组中
  • 回写另一张纸上的最后一行+1。

请注意,数组从0开始编制索引,行和列从1和A开始,因此您必须进行一些数学运算!

function copyRow(){
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getActiveSheet();
  var rowIdx = sheet.getActiveRange().getRowIndex();
  var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
  Logger.log(rowValues);
  var destValues = [];
  destValues.push(rowValues[0][0]);// copy data from col A to col A
  destValues.push(rowValues[0][2]);// copy data from col C to col B
  destValues.push(rowValues[0][1]);// copy data from col B to col C
  destValues.push(rowValues[0][3]);// copy data from col D to col D
  // continue as you want
  var dest = ss.getSheets()[1];//go on chosen sheet (here is the 2cond one)
  dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);//update destination sheet with selected values in the right order, the brackets are there to build the 2D array needed to write to a range
}

如果您希望该功能在编辑时运行,那么只需将其调用onEdit()即可完成!

答案 1 :(得分:0)

来源:https://stackoverflow.com/a/64297866/14427194

background-repeat: no-repeat;