2

Consider a Google Spreadsheet. Each row contains some measurement data from a given patient.

I wrote a Google Script function which iterates through the selected rows and exports each patient's data into a separate PDF file.

function Export() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("master-lab");
  var range = sheet.getActiveRange();
  var row0 = range.getRow();
  var row1 = row0 + range.getNumRows();
  for (i = row0; i < row1; ++i) {
    ExportRow(i);
  }
}

This code does not work if the user selects a non-contiguous array of rows, like in the following figure:

enter image description here

Is it there a way/workaround to iterate through non-contiguous ranges in Google Sheet?

2 Answers 2

-1

The problem is not with iterating through ranges (having an array of ranges and an additional for loop to go over them is not a problem). The issue is the script will not know what the ranges are.

Google Apps Script is unable to access multiple ranges selected by a user. Only the last selected range is visible to it. There is a feature request Add support to allow use and manipulate disjoint Ranges which was filed in 2014 and remains outstanding.

2

Since 2018, disjunct selected ranges can be accessed through the method getActiveRangeList:

SpreadsheetApp.getActiveSheet().getActiveRangeList().getRanges();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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