1

I have this script in Google Sheets which is able to archive range Sheet1!A5:H to Sheet2

function tc() {
    var reference = 'sheet1!A2'; 
    var rng = SpreadsheetApp.getActiveSpreadsheet().getRange(reference);
    rng.setValue(rng.getValue()+1);
    var ss = SpreadsheetApp.getActiveSpreadsheet ();
    var ss = SpreadsheetApp.getActiveSpreadsheet ();
    var source = ss.getRange ("Sheet1!A5:H");
    var destSheet = ss.getSheetByName("Sheet2");
    var lastRow = destSheet.getLastRow();
    destSheet.insertRowAfter(lastRow);
    source.copyTo(destSheet.getRange(lastRow + 1,1), {contentsOnly: true});
}

The issue is that this will also archive all empty rows from that range.

Is there a way how to skip archiving empty rows from a given range?

I tried to add something like var filtered_input = source.filter(String); but this didn't do the trick. Can someone help me out?

Sheet1 looks like:

Sheet2 looks like this after running the script 2 times:

1

How about this modification? I think that you can use getValues() and setValues() in your situation, because contentsOnly: true is used to copyTo(). So please modify as follows.

From:

var lastRow = destSheet.getLastRow();
destSheet.insertRowAfter(lastRow);
source.copyTo(destSheet.getRange(lastRow + 1,1), {contentsOnly: true});

To:

var values = source.getValues().filter(function(e) {return e.some(function(f) {return f})});
destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);

References:

If I misunderstood your question, please tell me. I would like to modify it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.