将行范围转移到另一个选项卡,然后在不删除公式的情况下清除选项卡

Transferring range of rows to another tab, then clearing tab without removing formulas

在 Google 工作表中,我正在尝试将范围 A3:G13(如果有文本;有时不会使用整个范围)从装箱单选项卡转移到已完成交易选项卡当 G16 设置为“完成”时,然后在不删除公式的情况下清除 A3:G13。我见过很多单独传输行的脚本,但我需要它们全部传输 G15 的 onEdit。

感谢您的帮助!

https://docs.google.com/spreadsheets/d/1raVbOHsjwK1EHDnIO7jKo1oH4GQXsbfjBD2ormcO87I/edit?ts=5fd250e3#gid=0

代码:

function onEdit(event) { 
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var s = event.source.getActiveSheet(); 
  var r = event.source.getActiveRange(); 
  if(s.getName() == "Packing List" && r.getColumn() == 7 && r.getValue() == "Complete") { 
    var range = r.getRange(A3:G13); 
    var numColumns = s.getLastColumn(); 
    var targetSheet = ss.getSheetByName("Completed Transactions"); 
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1); 
    s.getRange(row, 1, 1, numColumns).moveTo(target); 
    s.deleteRow(row);

您可以执行以下操作:

代码片段:

function onEdit(e) {
  const rangeNotation = e.range.getA1Notation();
  const editedSheet = e.range.getSheet();
  if (editedSheet.getName() === "Packing List" && rangeNotation === "G15" && e.value === "Complete") {
    const sourceRange = editedSheet.getRange("A3:G13");
    const sourceFormulas = sourceRange.getFormulas();
    const sourceData = sourceRange.getValues().filter(row => row[0] != "");
    const targetSheet = e.source.getSheetByName("Completed Transactions");
    const targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1, sourceData.length, sourceData[0].length);
    targetRange.setValues(sourceData);
    sourceRange.clearContent();
    sourceRange.setFormulas(sourceFormulas);
  }
}