[英]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.我相信你的目标如下。
Y
, you want to copy the values of the columns "B", "C" and "E" to the last row of the sheet ORDERS
.Y
时,您希望将“B”、“C”和“E”列的值复制到工作表ORDERS
的最后一行。
Y
when the script is run.Y
的所有行。IN
, when the script is run.IN
工作表中复制行的列“E”和“F”的内容。 For this, how about this answer?为此,这个答案怎么样?
It is required to modify above modification points.以上修改点需要修改。 When above points are reflected to the script, it becomes as follows.
当以上几点反映到脚本中时,它变成如下。
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();
}
run
to the button. run
分配给该按钮。 By this, when the button is clicked, the script works.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.