ISSUE:
I have 2 identical sheets and want to copy all non-empty rows from the first sheet to the second sheet. And this copying process should start at row 2 of column A of the first sheet. In other words, I do NOT want to include the headers/headlines in the copy.
ATTEMPTED SOLUTION:
The script below copies the whole first sheet (including the headers/headline) instead of starting to copy at row 2. I am pretty sure I am missing something and won´t be able to find it out, as I am still learning how to deal with Apps Script.
QUESTION:
Can somebody please correct the code below, so that it can copy only the non-empty rows from the first sheet to the second sheet and without the headers/headline?
Thank you so much in advance.
function copyAndAddNewEntries() {
var sourceSheet = SpreadsheetApp.openById('SOURCE_SHEET_ID').getSheetByName('SOURCE_SHEET_NAME')
var sourceSheetrange = sourceSheet.getDataRange();
var sourceSheetData = sourceSheetrange.getValues();
var targetSheet = SpreadsheetApp.openById('TARGET_SHEET_ID').getSheetByName('TARGET_SHEET_NAME');
targetSheet.getRange(targetSheet.getLastRow() 1, 1, sourceSheetrange.getHeight(), sourceSheetrange.getWidth()).setValues(sourceSheetData);
}
CodePudding user response:
I believe your goal as follows.
- You want to copy the rows that all empty columns are not empty from the source sheet, and want to put the values to the destination sheet.
- You want to remove the header row from the source values.
In this case, how about the following modification?
Modified script:
function copyAndAddNewEntries() {
var sourceSheet = SpreadsheetApp.openById('SOURCE_SHEET_ID').getSheetByName('SOURCE_SHEET_NAME');
var sourceSheetrange = sourceSheet.getDataRange();
var [, ...sourceSheetData] = sourceSheetrange.getValues();
sourceSheetData = sourceSheetData.filter(e => e.join("") != "");
var targetSheet = SpreadsheetApp.openById('TARGET_SHEET_ID').getSheetByName('TARGET_SHEET_NAME');
targetSheet.getRange(targetSheet.getLastRow() 1, 1, sourceSheetData.length, sourceSheetData[0].length).setValues(sourceSheetData);
}