0

I've got a master sheet where a user selects rows to "reserve". My code so far is:

var selectedRow = ss.getActiveRange().getRow();
var mySelection = master.getSelection();
var myRange = mySelection.getActiveRangeList().getRanges();

for ( var i = 0; i < myRange.length; i++) {
    var reserveData = myRange[i].getValues()
} 

var numRows = myRange.length

if (numRows = 1) //one row selected { 
      a1notation = "A" + selectedRow + ":" + "U" + selectedRow;
      headers = master.getRange(1, 1, 1, 21);
      newRange = master.getRange(a1notation);
      //copy headers
      headers.copyTo(resSheet.getRange(1, 1));
} else if (numRows >= 2) //multiple rows selected {
      a1notation = "A" + selectedRow + ":" + "U" + ( ??? );  
      }

I'm unsure how to get the other rows that are selected. These could all be adjacent rows or non-adjacent rows (if that matters) Any help would be greatly appreciated.

added

a1notation = "A" + selectedRow + ":" + "U" + selectedRow;
newRange = master.getRange(a1notation);
newRange.copyTo(resSheet.getRange(resSheet.getLastRow() + 1, 1));
2
  • selectedRow is undefined Commented Mar 30, 2020 at 15:35
  • @TheMaster, it's not, I just had it higher up in my code. Was trying to just show what was relevant and I missed that part :) I added it. Thanks for catching. Commented Mar 30, 2020 at 15:39

1 Answer 1

1

Use map to get all a1 notations:

const a1nots = mySelection.getActiveRangeList().getRanges().map(range=>range.getA1Notation());
console.log(a1nots);

In addition, myRange.length would give the number of ranges in this selection and not the number of rows in each range.

References:

Class Selection

Sign up to request clarification or add additional context in comments.

7 Comments

I will give this a try, thank you. It actually does return the number of rows selected, so I'm going with it.
@Mich only if it's non adjacent. It it's adjacent, it won't
I just tried it with a single row, and multiple rows (both adjacent and non adjacent) and it works... Ok so the above answer does return what I need! Thank you. However, before, I was using a1notation.copyTo( copies the row data to new sheet ) I cannot use .copyTo() with the above. Would you mind telling me how to accomplish that?
Just added the part I'm referring to as reference.
@Mich a1notation is a string. copyTo is a method of range class. you don't need a1notation. range=>range.copyTo() should work. If you need more help, consider asking a new question
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.