简体   繁体   English

将多行复制并粘贴到另一张纸上的下一个空行中

[英]Copying and Paste multiple rows into next empty Row on another sheet

I have the following script:我有以下脚本:

function onEdit(event) {
  // assumes source data in sheet named Needed
  // target sheet of move to named Acquired
  // test column with yes/no is col 5 or E
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();
  if(s.getName() == "IN" && r.getColumn() == 7 && r.getValue() == "Y") {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("ORDERS");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo(target);

  }
}

This is the original post where i found the script: https://support.google.com/docs/forum/AAAABuH1jm0hR40qh02UWE/?hl=en&gpf=%23!topic%2Fdocs%2FhR40qh02UWE这是我找到脚本的原始帖子: https://support.google.com/docs/forum/AAAABuH1jm0hR40qh02UWE/?hl=en&gpf=%23!topic%2Fdocs%2FhR40qh02UWE

I want to make a few small adjustments to the code, but don't know where to start.我想对代码做一些小的调整,但不知道从哪里开始。

Presently, it copies the whole row when a "Y" is entered into column G and places the row contents on lastrow of ORDERS..目前,当在 G 列中输入“Y”时,它会复制整行并将行内容放在 ORDERS 的最后一行。

What i want it to do is:我想要它做的是:

1) only copy columns B,C and E on lastrow of ORDERS, 2) delete values in E and F on IN? 1) 仅在 ORDERS 的最后一行复制 B、C 和 E 列,2) 删除 IN 上 E 和 F 中的值? after code has run for that specific row (don't want it deleting rows that I haven't put a "Y" against) 3) Is there a way to have a button instead, that when the button is clicked it copies all the rows with "Y" at once?在针对该特定行运行代码之后(不希望它删除我没有放置“Y”的行)3)有没有办法让一个按钮代替,当单击该按钮时它会复制所有一次带有“Y”的行?

Here's a link to my sheet if you want to have a play: https://docs.google.com/spreadsheets/d/1Peo5_5QmkxVyL7j5bmgtMs9BL16cvsGhhOSuRV_TsAo/edit?usp=sharing如果你想玩的话,这里是我的表格的链接: https://docs.google.com/spreadsheets/d/1Peo5_5QmkxVyL7j5bmgtMs9BL16cvsGhhOSuRV_TsAo/edit?usp=sharing

Best regards manc最好的问候曼克

I believe your goal as follows.我相信你的目标如下。

  • When the column "G" is Y , you want to copy the values of the columns "B", "C" and "E" to the last row of the sheet ORDERS .当“G” Y时,您希望将“B”、“C”和“E”列的值复制到工作表ORDERS的最后一行。
    • You want to copy all rows that the column "G" is Y when the script is run.您想在脚本运行时复制列“G”为Y的所有行。
  • You want to delete the content of columns "E" and "F" of the copied rows in the sheet of IN , when the script is run.当脚本运行时,您想删除IN工作表中复制行的列“E”和“F”的内容。
  • You want to run the script by clicking a button on the sheet.您想通过单击工作表上的按钮来运行脚本。

For this, how about this answer?为此,这个答案怎么样?

Modification points:修改点:

  • In your script,在你的脚本中,
    • When the script is run by clicking a button on the sheet, the event object cannot be used.通过单击工作表上的按钮运行脚本时,无法使用事件 object。
    • Your script copies one row of the active range.您的脚本复制活动范围的一行。
    • The columns "E" and "F" of the copied rows are not deleted.复制行的列“E”和“F”不会被删除。

It is required to modify above modification points.以上修改点需要修改。 When above points are reflected to the script, it becomes as follows.当以上几点反映到脚本中时,它变成如下。

Modified script:修改后的脚本:

Please copy and paste the following script to the script editor.请将以下脚本复制并粘贴到脚本编辑器中。 And please prepare a button which is drawing and/or image, and assign the function run to the button.请准备一个绘图和/或图像按钮,并将 function run分配给该按钮。 By this, when the button is clicked, the script is run.这样,当单击按钮时,脚本就会运行。 And the columns "B", "C" and "E" of all rows that the column "G" is Y are copied from the sheet "IN" to the last row of the sheet "ORDERS".并且将列“G”为Y的所有行的列“B”、“C”和“E”从工作表“IN”复制到工作表“ORDERS”的最后一行。

function run() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var srcSheet = ss.getSheetByName("IN");
  var dstSheet = ss.getSheetByName("ORDERS");

  // 1. Retrieve the values from "A2:G" of sheet "IN".
  var srcValues = srcSheet.getRange("A2:G" + srcSheet.getLastRow()).getValues();

  // 2. Create an object for putting values and deleting the contents of the columns "E" and "F".
  var obj = srcValues.reduce((o, [,b,c,,e,,g], i) => {
    if (g == "Y") {
      o.values.push([b, c, e]);
      o.ranges.push(`E${i + 2}:F${i + 2}`);
    }
    return o;
  }, {values: [], ranges: []});

  // 3. Copy the values to the sheet "ORDERS".
  dstSheet.getRange(dstSheet.getLastRow() + 1, 1, obj.values.length, obj.values[0].length).setValues(obj.values);

  // 4. Delete the contents of the columns "E" and "F" of sheet "IN".
  srcSheet.getRangeList(obj.ranges).clearContent();
}

Note:笔记:

  • About the button for running the Google Apps Script, I think that this site is useful.关于运行 Google Apps 脚本的按钮,我认为这个站点很有用。 Ref In your case, please create a button on the sheet "IN" and assign the function run to the button. 参考在您的情况下,请在“IN”表上创建一个按钮并将 function run分配给该按钮。 By this, when the button is clicked, the script works.这样,当单击按钮时,脚本就可以工作。
  • Please use this script with V8.请将此脚本与 V8 一起使用。

References:参考:

暂无
暂无

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

相关问题 将粘贴复制到另一张纸并找到没有空行的最后一行 - Copy paste to another sheet and find last row without empty rows 将多个非空行复制到新工作表 - Copying multiple non empty rows to a new sheet 复制一个范围并仅将具有值和图像的行粘贴到另一个工作表中下一个空白行的谷歌工作表中? - Copy a range and paste only rows with values & images into another sheet at the next blank row in the google sheets? 复制一个范围并仅将带有值的行粘贴到下一个空白行的另一个工作表中? - Copy a range and paste only rows with values into another sheet at the next blank row? 将值复制并粘贴到Google表格中的下一个空行中 - Copying and Paste Values into next empty Row in Google Sheets 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down 如何复制值并将其粘贴到另一张纸上的下一个可用行中? - How do I copy values and paste them into the next available rows on another sheet? 如何从工作表复制一行并将具有较小更改的多个版本粘贴到另一工作表 - How to copy a row from a sheet and paste multiple versions with minor changes to another sheet Select 单元格(Ctrl+A)然后复制范围并粘贴到另一个工作表的第一个空行 - Select cells(Ctrl+A) then Copy range and paste to another sheet's first empty row 谷歌应用程序脚本 - 粘贴到下一个空行 - google apps script - paste in next empty row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM