Michael
Michael

Reputation: 121

Not getting full range of data using .getSelection.getActiveRangeList().getRanges()

I've been going back and forth with this for hours and I'm about to pull my hair out.

I have a master spreadsheet that I am pulling data from, copying it, and pasting it into another sheet. This is done based on what rows the user selects. Selecting single rows works as expected, multiple rows (if adjacent) also works. Now I am starting another if block to account for multiple rows being selected that are NON-adjacent using this code block:

    var myRange = mySelection.getActiveRangeList().getRanges();
    Browser.msgBox(myRange.length)
    for ( var i = 0; i < myRange.length; i++) {
        var reserveData = myRange[i].getValues();
        Browser.msgBox(reserveData[i]);
    }

Let's say the user selects 4 rows to copy. The first MsgBox will show the length as 4. Great! However, the MsgBox within the for loop, will only display data for the first iteration of the loop. The 2nd, 3rd, and 4th MsgBox's contain no data.

Upvotes: 2

Views: 182

Answers (1)

TheMaster
TheMaster

Reputation: 50799

Issue:

getValues() returns a 2D array. But i changes each iteration. The change in i is irrelevant to indexing the retrieved two dimensional array. For example, In the second iteration, i is 1, but the second range (and thus the second reserveData)might not contain 2 rows(row 0 and row1) and so reserveData[i] will be undefined.

Solution:

Use

Browser.msgBox(reserveData);//[i] removed

Upvotes: 2

Related Questions