'appscript - get multiple selected rows

If i mark and select 5 rows in google sheet. I want to retrieve an array of retrieved rows. Particully column (0).

function selectRow() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var rowIndex = sheet.getCurrentCell().getRow();
  var rowValues = sheet.getRange(rowIndex, 1, 1, sheet.getLastColumn()).getValues()[0];
  return rowValues;
}

But how to retrieve of all selected rows if I select multiple ?

Edit: enter image description here



Solution 1:[1]

A single active range

function getData() {
  Logger.log(JSON.stringify(SpreadsheetApp.getActiveRange().getValues()))
}

Multiple non overlapping selections

function getData() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  let a = sh.getActiveRangeList().getRanges().map(r => r.getValues());
  Logger.log(JSON.stringify(a));
}

In answer to your question

function getData() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  let a = sh.getActiveRangeList().getRanges().map(r => sh.getRange(r.getRow(),1,r.getHeight(),sh.getLastColumn()).getValues());
  Logger.log(JSON.stringify(a));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1