2

I was trying to get the selected shapes from the user's current selection in Google slides with Apps Script. The shapes return are not always match the user's selection order.

Example: Let's say we have 4 shapes on the slide, #1, #2, #3, #4. If I select #4, #2, #3, #1 on the slide one by one, the return result should match [shape#4, shape#2, shpae#3, shape#1]. However, I found sometimes the return result is a mess, not reflect the actual order I select. Not sure if this is a bug or something.

Here is my code to get the selected shapes to put them in a list.

function getSelectedShapes(){
    let shapes = []
    let selections = SlidesApp.getActivePresentation().getSelection()
    let pageElementRange = selections.getPageElementRange()
    if (pageElementRange){
        let elements = pageElementRange.getPageElements()
        shapes = elements.map(element=>element.asShape())
    }
    return shapes
}
TheMaster
  • 45,448
  • 6
  • 62
  • 85
Ashton Fei
  • 114
  • 2
  • 7
  • 4
    I could confirm about your issue. I'm not sure whether this is the bug or the current specification at Google side. So How about reporting this to Google issue tracker? [Ref](https://developers.google.com/issue-tracker) Although I had searched the same issue at Google issue tracker, I couldn't find it. So as the current answer, when several objects are selected on Google Slides, the order of elements retrieved by `getSelection().getPageElementRange().getPageElements()` is not the same with the order of selection by the bug or the current specification. I apologize for this. – Tanaike Jun 21 '20 at 22:38
  • 2
    @Tanaike we can say my this issue https://stackoverflow.com/questions/63172078/not-all-elements-convert-to-the-same-size-as-the-size-of-the-first-element also a selection bug in getPageElements() ? correct ? – Naresh Jul 30 '20 at 17:08
  • 1
    @Puzzled Boy I saw [the question](https://stackoverflow.com/questions/63172078/not-all-elements-convert-to-the-same-size-as-the-size-of-the-first-element). I think it's yes. In the current stage, it seems that this bug is still not resolved. By this, in your case, the 1st selected image cannot be retrieved. I think that this is the current reason of your issue. – Tanaike Jul 30 '20 at 22:48
  • 1
    Does this answer your question? [Not all elements convert to the same size as the size of the first element](https://stackoverflow.com/questions/63172078/not-all-elements-convert-to-the-same-size-as-the-size-of-the-first-element) – TheMaster Jul 31 '20 at 05:41

1 Answers1

2

As stated by @Tanaike, the method getPageElements returns the elements and shapes rendered on the page in no particular order and therefore you will not be able to retrieve them in any particular order.

As @Tanaike mentioned, you can make a feature request asking for getting these elements in the order of your selection.

Mateo Randwolf
  • 2,823
  • 1
  • 6
  • 17