[英]Copy row to another sheet via for loop google sheets script
I am below a newbie and have tried this code several ways and I just can't seem to figure out what is going on.我是新手,已经尝试了几种方法,但我似乎无法弄清楚发生了什么。
What I am trying to do is copy any new lines that get added to one sheet ( Source
) to another sheet ( Target
).我想要做的是将添加到一张纸(
Source
)的任何新行复制到另一张纸( Target
)。 There is a column (7) that is there to indicate not to copy that row from the Source
to the Target
, and is set to CP
when the program runs through and copies to the Target
.有一列 (7) 指示不要将该行从
Source
复制到Target
,并且在程序运行并复制到Target
时设置为CP
。
The code below seems to work and copy to the Target
with the exception that it copies every row from the Source
every time I run it, even when the Source rows have been set to CP
in col 7 ( CP
means copied if you are wondering).下面的代码似乎可以工作并复制到
Target
,但每次我运行它时它都会从Source
复制每一行,即使在第 7 列中将源行设置为CP
(如果您想知道, CP
意味着复制) .
Any help would be appreciated in helping me figure this out.如果能帮助我解决这个问题,我们将不胜感激。 I've banged my head against the desk a few times already but that does not seem to help either.
我已经用头撞过桌子好几次了,但这似乎也无济于事。
function copyRowToTarget() {
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Source')
var targetSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Target')
var startRow = 2;
var sSLastRow = sourceSheet.getLastRow();
var con = sourceSheet.getRange(1, 7, sSLastRow); //col that indicates if it should be copied or not
var conVals = con.getValues(); //con values
var rangetocopy = sourceSheet.getRange(1, 1, sSLastRow, 6);
var rowstocopy = rangetocopy.getValues(); //values to copy
for (var i = 1; i < conVals.length; i++){
if (conVals[i] !== "CP"){
targetSheet.appendRow(rowstocopy[i]);
}
sourceSheet.getRange(startRow + i, 7).setValue("CP");
}
}
Try this:试试这个:
function copyRowToTarget() {
const ss=SpreadsheetApp.getActive();
const ssh = ss.getSheetByName('Source')
const tsh = ss.getSheetByName('Target')
const startRow = 2;
const vals=ssh.getRange(startRow, 6, ssh.getLastRow() - startRow + 1, 2).getValues();
vals.forEach((v,i)=>{
if (v[1] != "CP"){
tsh.appendRow([v[0]]);
ssh.getRange(startRow + i, 7).setValue("CP");
}
});
}
I tested this version:我测试了这个版本:
function copyRowToTarget() {
const ss=SpreadsheetApp.getActive();
const ssh = ss.getSheetByName('Sheet1')
const tsh = ss.getSheetByName('Sheet2')
const startRow = 2;
const vals=ssh.getRange(startRow, 6, ssh.getLastRow() - startRow + 1, 2).getValues();
vals.forEach((v,i)=>{
if (v[1] != "CP"){
tsh.appendRow([v[0]]);
ssh.getRange(startRow + i, 7).setValue("CP");
}
});
}
Works fine:工作正常:
Data:数据:
COL1![]() |
COL2![]() |
COL3![]() |
COL4![]() |
COL5 ![]() |
COL6![]() |
COL7 ![]() |
COL8 ![]() |
COL9 ![]() |
COL10 ![]() |
---|---|---|---|---|---|---|---|---|---|
6 ![]() |
13 ![]() |
10 ![]() |
27 ![]() |
1 ![]() |
17 ![]() |
CP ![]() |
8 ![]() |
25 ![]() |
0 ![]() |
0 ![]() |
0 ![]() |
12 ![]() |
16 ![]() |
26 ![]() |
1 ![]() |
CP ![]() |
25 ![]() |
14 ![]() |
5 ![]() |
2 ![]() |
8 ![]() |
10 ![]() |
25 ![]() |
6 ![]() |
20 ![]() |
CP ![]() |
10 ![]() |
3 ![]() |
0 ![]() |
16 ![]() |
15 ![]() |
21 ![]() |
12 ![]() |
2 ![]() |
1 ![]() |
CP ![]() |
9 ![]() |
15 ![]() |
0 ![]() |
29 ![]() |
10 ![]() |
9 ![]() |
10 ![]() |
25 ![]() |
23 ![]() |
CP ![]() |
27 ![]() |
29 ![]() |
6 ![]() |
28 ![]() |
24 ![]() |
4 ![]() |
13 ![]() |
2 ![]() |
26 ![]() |
CP ![]() |
23 ![]() |
19 ![]() |
19 ![]() |
29 ![]() |
17 ![]() |
3 ![]() |
0 ![]() |
9 ![]() |
1 ![]() |
CP ![]() |
3 ![]() |
27 ![]() |
15 ![]() |
20 ![]() |
8 ![]() |
7 ![]() |
15 ![]() |
28 ![]() |
26 ![]() |
CP ![]() |
10 ![]() |
24 ![]() |
13 ![]() |
27 ![]() |
16 ![]() |
29 ![]() |
2 ![]() |
25 ![]() |
14 ![]() |
CP ![]() |
21 ![]() |
4 ![]() |
12 ![]() |
Sheet2:工作表 2:
17 ![]() |
1 ![]() |
20 ![]() |
1 ![]() |
23 ![]() |
26 ![]() |
1 ![]() |
26 ![]() |
14 ![]() |
13 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.