[英]Copy a specific range to from one sheet to the first empty row and specific column in another sheet
I use Google Sheets and I'm trying to use a script that copies certain ranges from a sheet (that reads CSV files) to a separate sheet's last available row, but to a certain column.我使用 Google 表格,我正在尝试使用一个脚本,该脚本将某些范围从表格(读取 CSV 文件)复制到单独的表格的最后一个可用行,但复制到某个列。 I also don't want to copy empty values.
我也不想复制空值。
https://i.stack.imgur.com/lgGP3.png https://i.stack.imgur.com/lgGP3.png
So I need to copy values only of a specific range from a sheet, like Sheet1:A5,D14, where it ignores empty values, to a different sheet's first empty row.因此,我只需要将特定范围的值从工作表(如 Sheet1:A5,D14,它忽略空值)复制到不同工作表的第一个空行。 but not the same column (so everything moves forward to column CF).
但不是同一列(因此所有内容都移至 CF 列)。
I was able to use this, to copy things to a specific column:我能够使用它来将内容复制到特定列:
function copyRange(e){
var copyFromRange = 'Sheet1!B3:J10'; // no row for second cell reference
var copyToRangeStart = 'Sheet2!B12';
copyValuesOnly(copyFromRange, copyToRangeStart);
}
function copyValuesOnly(copyFromRange, copyToRangeStart) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange(copyFromRange);
source.copyTo(ss.getRange(copyToRangeStart), {contentsOnly: true});
}
However, this copies empty rows too, and can't find the first empty row in the other sheet.但是,这也会复制空行,并且在另一张工作表中找不到第一个空行。
I tried using code like this;我尝试使用这样的代码;
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("B10:AH13");
var destSheet = ss.getSheetByName("Test");
destSheet.appendRow(source.getValues()[0]);
}
This only copies a single row, and I can't copy it to a certain column on the other sheet, will always start in "A".这只会复制一行,我不能将它复制到另一张纸上的某一列,总是从“A”开始。
I'm not big on coding, if someone could help me here, that'd be amazing.我不擅长编码,如果有人可以在这里帮助我,那就太棒了。
In short, the function I need has to;简而言之,我需要的 function 必须;
EDIT:编辑:
Thanks to anyone who takes the time to help in advance:)感谢任何花时间提前帮助的人:)
function copyRange(){
const ss=SpreadsheetApp.getActive();
const srcrg = 'Sheet1!B3:J10';
const torg = 'Sheet2!B12';
const desrg=ss.getRange(torg);
const row=desrg.getLastRow();
const col=desrg.getColumn();
const dessh=desrg.getSheet();
const src=ss.getRange(srcrg).getValues();
let cnt=0
src.forEach((r,i)=>{
if(r.join("")!="") {
dessh.getRange(row+1+cnt++,col,1,r.length).setValues([r]);
}
});
}
Source:资源:
COL1 ![]() |
COL2 ![]() |
COL3 ![]() |
COL4 ![]() |
COL5 ![]() |
COL6 ![]() |
COL7 ![]() |
COL8 ![]() |
COL9 ![]() |
COL10 ![]() |
---|---|---|---|---|---|---|---|---|---|
10 ![]() |
21 ![]() |
14 ![]() |
11 ![]() |
6 ![]() |
9 ![]() |
3 ![]() |
11 ![]() |
9 ![]() |
2 ![]() |
29 ![]() |
29 ![]() |
21 ![]() |
19 ![]() |
14 ![]() |
4 ![]() |
24 ![]() |
11 ![]() |
1 ![]() |
0 ![]() |
2 ![]() |
11 ![]() |
25 ![]() |
2 ![]() |
9 ![]() |
2 ![]() |
1 ![]() |
11 ![]() |
5 ![]() |
29 ![]() |
3 ![]() |
3 ![]() |
24 ![]() |
18 ![]() |
28 ![]() |
6 ![]() |
6 ![]() |
20 ![]() |
19 ![]() |
23 ![]() |
4 ![]() |
16 ![]() |
4 ![]() |
17 ![]() |
25 ![]() |
14 ![]() |
8 ![]() |
22 ![]() |
4 ![]() |
15 ![]() |
26 ![]() |
15 ![]() |
7 ![]() |
25 ![]() |
7 ![]() |
2 ![]() |
10 ![]() |
26 ![]() |
10 ![]() |
19 ![]() |
4 ![]() |
18 ![]() |
6 ![]() |
5 ![]() |
18 ![]() |
27 ![]() |
2 ![]() |
27 ![]() |
5 ![]() |
0 ![]() |
15 ![]() |
5 ![]() |
26 ![]() |
22 ![]() |
13 ![]() |
18 ![]() |
6 ![]() |
27 ![]() |
28 ![]() |
24 ![]() |
Destination:目的地:
29 ![]() |
21 ![]() |
19 ![]() |
14 ![]() |
4 ![]() |
24 ![]() |
11 ![]() |
1 ![]() |
0 ![]() |
|
11 ![]() |
25 ![]() |
2 ![]() |
9 ![]() |
2 ![]() |
1 ![]() |
11 ![]() |
5 ![]() |
29 ![]() |
|
3 ![]() |
24 ![]() |
18 ![]() |
28 ![]() |
6 ![]() |
6 ![]() |
20 ![]() |
19 ![]() |
23 ![]() |
|
16 ![]() |
4 ![]() |
17 ![]() |
25 ![]() |
14 ![]() |
8 ![]() |
22 ![]() |
4 ![]() |
15 ![]() |
|
15 ![]() |
7 ![]() |
25 ![]() |
7 ![]() |
2 ![]() |
10 ![]() |
26 ![]() |
10 ![]() |
19 ![]() |
|
18 ![]() |
6 ![]() |
5 ![]() |
18 ![]() |
27 ![]() |
2 ![]() |
27 ![]() |
5 ![]() |
0 ![]() |
|
5 ![]() |
26 ![]() |
22 ![]() |
13 ![]() |
18 ![]() |
6 ![]() |
27 ![]() |
28 ![]() |
24 ![]() |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.