【问题标题】:Moving specific cells from last row to new spreadsheet将特定单元格从最后一行移动到新电子表格
【发布时间】:2021-04-01 15:39:47
【问题描述】:

我正在尝试编写一个脚本,该脚本将在 Google 表单中查找新条目到 Google 表格中,并将数据从特定单元格移动到单独的电子表格中。我有以下脚本,并且在使用 rowValues 变量时遇到了问题。以下是当前编写的脚本。

function onEdit(){
var ss = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyFromSheet");

var rowId = sheet.getActiveRange().getRowIndex();
var rowValues (rowId);
Logger.log(rowValues);
var destValues = [];
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][1]);// copy data from col B to col B
destValues.push(rowValues[0][2]);// copy data from col C to col C
destValues.push(rowValues[0][5]);// copy data from col F to col D
destValues.push(rowValues[0][6]);// copy data from col G to col E
destValues.push(rowValues[0][7]);// copy data from col H to col F
destValues.push(rowValues[0][8]);// copy data from col I to col G
destValues.push(rowValues[0][21]);// copy data from col V to col H
destValues.push(rowValues[0][23]);// copy data from col X to col I

var dest=SpreadsheetApp.openById('SheetID#').getSheetByName('CopyToSheet'); 
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);

}

【问题讨论】:

  • 请问I have the following script and am having trouble with the rowValues variable.trouble with the rowValues variable的详细情况? var rowValues (rowId); 是什么?
  • onEdit 触发器不会在 Google 表单将另一行写入工作表时触发。您应该使用 onFormSubmit 触发器。

标签: google-apps-script google-sheets


【解决方案1】:
function onFormSubmit(e){
  var destValues = [];
  destValues.push(e.values[0]);
  destValues.push(e.values[1]);
  destValues.push(e.values[2]);
  destValues.push(e.values[5]);
  destValues.push(e.values[6]);
  destValues.push(e.values[7]);
  destValues.push(e.values[8]);
  destValues.push(e.values[21]);
  destValues.push(e.values[23]);
  var dest=SpreadsheetApp.openById('SheetID#').getSheetByName('CopyToSheet'); 
  dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}

【讨论】:

  • Cooper,使用 onFormSubmit 函数很有意义。感谢您指出了这一点。运行此脚本时,它正在推送数据,但在这种情况下,来自第一个数组条目 [0][0] 的数据被发送到目标工作表/单元格,每个单元格 1 个字符。例如,日期是正在传输的第一个单元格中的数据。在 CopyTo 表中,第一个单元格有“1”,第二个单元格有“2”,第三个单元格有“/”等。我假设 dest.getRange(dest.getLastRow() +1,1,1,destValues/length)...
  • 是的,我刚刚注意到我犯了一个错误,只是删除了第一个子脚本零
  • 从任何行中删除第一个子脚本 0 会导致我无法保存脚本的错误。从数组中删除第一行只会停止写入第一个字符“1”。
  • 应该这样做
  • 成功了!谢谢,库珀!
猜你喜欢
  • 2019-01-15
  • 1970-01-01
  • 2018-04-22
  • 2022-07-05
  • 2012-08-12
  • 2020-05-21
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多