Ashton Fei
Ashton Fei

Reputation: 114

Google Slides Elements Selection Order Issue

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
}

Upvotes: 2

Views: 401

Answers (1)

Mateo Randwolf
Mateo Randwolf

Reputation: 2920

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.

Upvotes: 2

Related Questions