简体   繁体   English

将一系列单元格从一行移动到另一个谷歌工作表并删除它

[英]Move a range of cells from a row to another google sheet and delete it

I got the following code working but not as I would like to.我得到了以下代码,但不是我想要的。

function onEdit(event) {
 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  if(s.getName() == "Listed" && r.getColumn() == 21 && r.getValue() == "yes") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Sold");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  }
}

I have 2 sheets, one for Items listed, and one for items sold.我有 2 张纸,一张用于列出的物品,一张用于出售的物品。 I need to delete a row in "Listed" when the column 21 says "yes" and paste it in my "Sold" sheet.当第 21 列说“是”时,我需要删除“已上市”中的一行并将其粘贴到我的“已售”表中。

Is there a way to copy and paste only a range of cells from "Listed" to "Sold" but to delete the whole row ?有没有办法只复制和粘贴从“已上市”到“已售”的单元格范围,但要删除整行?

Explanation:解释:

  • Your goal is to copy only a subset of the edit row and paste it to the destination sheet and then delete that row in the source sheet.您的目标是仅复制编辑行的一个子集并将其粘贴到目标工作表中,然后在源工作表中删除该行。
  • The following script will copy only the first 10 columns of that row.以下脚本将仅复制该行的前 10 列。 Adjust const numColumns = 10 to your needs.根据您的需要调整const numColumns = 10

Solution:解决方案:

function onEdit(e) {
 
  const ss = e.source;
  const s = e.source.getActiveSheet();
  const row = e.range.getRow();
  const col = e.range.getColumn();
  const targetSheet = ss.getSheetByName("Sold");
  const numColumns = 10;

  if(s.getName() == "Listed" && col == 21 && e.range.getValue() == "yes") {
    
    const s_range = s.getRange(row,1,1,numColumns);
    const s_values = s_range.getValues();
    s.deleteRow(row);
    targetSheet.getRange(targetSheet.getLastRow() + 1, 1,s_values.length,s_values[0].length).setValues(s_values);
   
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任何人都可以帮我解决脚本以在 google sheet 应用程序脚本中将一行从一张表移动/删除到另一张表吗? - Can anyone help me troubleshoot a script to move/delete a row from one sheet to another in google sheets apps script? 如何将范围从一个 Google 工作表移动到另一个工作表/标签中的下一个空白行 - How can I move a range from one Google sheet to the next blank row in a different sheet/tab 通过从另一个工作表触发的脚本删除工作表中的单元格。 [谷歌工作表脚本] - Delete Cells in sheets via script that is triggered from another sheet. [google sheet script] 谷歌脚本将单元格范围移动到时间敏感触发器上的存档表 - Google Script to Move Range of cells to Archive sheet on time sensitive trigger 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets 如何将一行从谷歌工作表中的一个选项卡移动到同一个工作表中的另一个选项卡 - how do I move a row from one tab in a google sheet to another in the same sheet Google Sheets 将行复制并粘贴到另一个工作表中,然后从第一个工作表中删除内容 - Google Sheets copy and paste row into another sheet and then delete content from the first sheet Google表格将一行移动到当前行中2个单元格条件的另一张表格 - Google Sheets move a row to another sheet on 2 cell criteria in current row 如何有效地将数据从一个 Google 表格行中的多个单元格传输到另一个具有不同单元格排列的 Google 表格上的模板 - How to efficiently transfer data from multiple cells in one Google Sheet row to a template on another google sheet with different cell arrangement Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM