Home > Back-end >  How to get the latest entries from a 2D array based on a condition/column using GAS?
How to get the latest entries from a 2D array based on a condition/column using GAS?

Time:08-19

How can I get the highlighted rows, which are the latest entries according to the timestamps of those PO number (col1/A)?

I started playing around trying sort(), but I can't apply it correctly: Dataset

OrderPO TimeStamp Unit
TTL-220218 8/17/2022 20:47:55 Print
TTL-220218 8/18/2022 7:49:49 Print
TTL-220220 8/17/2022 18:00:55 Print
TTL-220220 8/18/2022 9:49:49 Print
TTL-220219 8/17/2022 20:47:55 Print
TTL-220219 8/18/2022 7:49:49 Print
TTL-220219 8/17/2022 20:47:59 Print
TTL-220216 8/18/2022 8:30:49 Print
function getLastestEntries() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('Sheet28');
  let pos = sheet.getDataRange().getValues();

  let uniquePos = pos.map(e => e[0]);
  uniquePos = [...new Set(uniquePos)];

  let latests = [];
  uniquePos.forEach(function (record) {
    let filteredPo = pos.filter(e => e[0] == record);
    filteredPo.sort(function (a, b) {
      let latest = a[1] > b[1] ? 1 : -1;
      latests.push(latest)
    });
  })
  console.log('POS: '   JSON.stringify(latests))
}

Rows to be extracted:

enter image description here

Appreciate directions!

CodePudding user response:

Latest Row for each PO based upon date

function getLastestEntries() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const vs = sh.getRange(2, 1, sh.getLastRow() - 1, sh.getLastColumn()).getValues();
  let rows = { pA: [] }
  vs.forEach((r, i) => {
    let po = r[0];
    if (!rows.hasOwnProperty(po)) {
      rows[po] = [{ row: i   1, date: new Date(r[1]), values: r.join(",") }];
      rows.pA.push(po);
    } else {
      rows[po].push({ row: i   1, date: new Date(r[1]), values: r.join(",") });
      rows[po].sort((a, b) => {
        return new Date(a.date).valueOf() - new Date(b.date).valueOf();//sorts as it collects data in large data sets this would not be the best way.  You could sort when you display the output.
      });
    }
  });

  let html = `Latest Rows`;//using a dialog to display the output
  rows.pA.forEach(po => {
    let x = rows[po].reverse()[0];//this only needs to be done once
    let d = x.date;
    let v = x.values;
    let r = x.row;
    html  = `<br>po: ${po}<br>latest Date: ${d}<br>Row: ${r}<br>values: ${v}<br><hr>`
  });
  SpreadsheetApp.getUi().showModelessDialog(HtmlService.createHtmlOutput(html), "Latest Rows for Each PO")
}

Test Data:

OrderPO TimeStamp Unit COL4 COL5 COL6 COL7 COL8 COL9 COL10
TTL-220218 8/17/2022 20:47:55 Print 4 5 6 7 8 9 10
TTL-220218 8/18/2022 7:49:49 Print 5 6 7 8 9 10 11
TTL-220220 8/17/2022 18:00:55 Print 6 7 8 9 10 11 12
TTL-220220 8/18/2022 9:49:49 Print 7 8 9 10 11 12 13
TTL-220219 8/17/2022 20:47:55 Print 8 9 10 11 12 13 14
TTL-220219 8/18/2022 7:49:49 Print 9 10 11 12 13 14 15
TTL-220219 8/17/2022 20:47:59 Print 10 11 12 13 14 15 16
TTL-220216 8/18/2022 8:30:49 Print 11 12 13 14 15 16 17

Dialog Output:

enter image description here

  • Related