Search code examples
google-apps-scriptcopy-paste

Copy and paste from sh1 to sh2 when 2 condition meet (GOOGLE SCRIPT)


Please help me with the below: Starting from row 7 of sheet1: if column E is NOT BLANK and column D is BLANK then I need copy from columns A:L to last empty row of sheet2


Solution

  • I believe your goal as follows.

    • You want to check the columns "D" and "E" on "Sheet1".
    • When the values of column "D" and "E" are empty and not empty, you want to copy the row to the next row of last row in "Sheet2".
    • You want to achieve this using Google Apps Script.

    I this case, how about the following sample script?

    Sample script:

    function myFunction() {
      const ss = SpreadsheetApp.getActiveSpreadsheet();
      const sheet1 = ss.getSheetByName("Sheet1");
      const sheet2 = ss.getSheetByName("Sheet2");
      const values = sheet1.getRange("A7:L" + sheet1.getLastRow()).getValues().reduce((ar, r) => {
        if (r[3].toString() == "" && r[4].toString() != "") ar.push(r);
        return ar;
      }, []);
      sheet2.getRange(sheet2.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    }
    

    References: