I need to copy data from a selected range (Y5:Z198) to cell (Y206) but somehow I can only make it to appendRow and paste only on column A. Can someone help me, please?
function CopyData(CopyData) {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("CAPA");
var sh2 = ss.getSheetByName("CAPA");
var rg1 = sh1.getRange("Y5:Z198");
var vA = rg1.getValues();
for (var i = 0; i < vA.length; i++) {
if (vA[i][1]) {
sh2.appendRow(vA[i]);
}
}
}
Try this code to copy the data
function CopyData(CopyData) {
const dstRow = 206;
let ss = SpreadsheetApp.getActive(),
sheet = ss.getSheetByName('CAPA'),
srcRange = sheet.getRange('Y5:Z198'),
srcValues = srcRange.getValues(),
filtered = srcValues.filter(item => item[1]); // filter the data being copied
// Define the range to insert
// 'Y'+dstRow -> Y206
// filtered.length -> the number of rows in the filtered array of data
// ':Z'+(dstRow-1+filtered.length) -> bottom right cell
sheet.getRange('Y'+dstRow+':Z'+(dstRow-1+filtered.length)).setValues(filtered);
}