Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Leave the blank cells in Column A and copy the rest (Google Sheet)

I am trying to copy the range from sheet1 to sheet2 using a script.
But I want to copy only the filled Cells in Column A and leave the blank cells. What should I add to the script?

This is the script I used to copy from sheet1 to sheet2

function Test() {
   const ss = SpreadsheetApp.getActive()
   const Source = ss.getRange('sheet1!A2:E33').getValues()
   const DestS = ss.getSheetByName('sheet2')
   const lastrow = DestS.getLastRow()
   const DestR = DestS.getRange(lastrow + 1,1,Source.length,Source[0].length)

DestR.setValues(Source);
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can do as following:

function Test() {
    const ss = SpreadsheetApp.getActive();
    const sourceValues = ss.getRange('sheet1!A2:A33').getValues(); // Get only values in Column A
    const filledCells = sourceValues.filter(row => row[0] !== ""); // Filter out blank cells

    if (filledCells.length > 0) {
    const destS = ss.getSheetByName('sheet2');
    const lastRow = destS.getLastRow();
    const destR = destS.getRange(lastRow + 1, 1, filledCells.length, filledCells[0].length);

    destR.setValues(filledCells);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading