Retrieving Start and End Row Numbers of Same Values in a Column on Google Spreadsheet using Google Apps Script

Gists

Retrieving Start and End Row Numbers of Same Values in a Column on Google Spreadsheet using Google Apps Script

This is a sample script for retrieving the start and end row numbers of the same values in a column on Google Spreadsheet using Google Apps Script.

There is a case in that I want to retrieve the rows of the same values in a column on Google Spreadsheet using Google Apps Script. In this post, I would like to introduce a simple sample script for achieving this.

Sample script

function myFunction() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const res = [
    ...sheet
      .getRange("A1:A" + sheet.getLastRow())
      .getDisplayValues()
      .reduce(
        (m, [a], i) => m.set(a, m.has(a) ? [...m.get(a), i + 1] : [i + 1]),
        new Map()
      )
      .values(),
  ].map((e) => ({ start: e[0], end: e[e.length - 1] }));
  console.log(res);
}

 Share!