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. I also don't want to copy empty values.
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. but not the same column (so everything moves forward to column 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".
I'm not big on coding, if someone could help me here, that'd be amazing.
In short, the function I need has to;
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 |
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.