将行复制到单独的电子表格onEdit列

时间:2013-11-14 15:46:36

标签: google-apps-script google-sheets

以下是我在另一张纸上将行复制到“季度报告”时实施的脚本。

如何编写此代码以将该行复制到单独的电子表格中?我尝试了很多变化。

function onEdit(event) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var s = event.source.getActiveSheet();

  var r = event.source.getActiveRange();

  if(s.getName() == "Sheet1" && r.getColumn() == 73 && r.getValue()) {

    var row = r.getRow();

    var numColumns = s.getLastColumn();

    var targetSheet = ss.getSheetByName("Quarterly Report");

    if(targetSheet.getLastRow() == targetSheet.getMaxRows()) {
      targetSheet.insertRowAfter(targetSheet.getLastRow());
    }

    var target = targetSheet.getRange(targetSheet.getLastRow()+1,1);

    s.getRange(row, 1, 1, numColumns).copyTo(target);
  }
}

1 个答案:

答案 0 :(得分:0)

您可以尝试以下内容:

/* CODE FOR DEMONSTRATION PURPOSES */
function onEdit(e) {
  var range = e.range, s = range.getSheet(), row = range.getRow();
  var sheetTarget, lastColumnTarget, lastColumnSource;
  if (e.value && s.getName() === 'Sheet1' && range.getColumn() === 73) {
    sheetTarget = e.source.getSheetByName('Quarterly Report');
    lastColumnSource = s.getLastColumn(), lastColumnTarget = sheetTarget.getLastColumn();
    lastColumnTarget = lastColumnTarget > lastColumnSource ? lastColumnTarget : lastColumnSource; 
    s.getRange(row, 1, 1, lastColumnTarget).copyTo(sheetTarget.getRange(row, 1, 1, lastColumnTarget));
  }
}

根据需要进行调整。