Skip to content

Instantly share code, notes, and snippets.

@rmariuzzo
Created October 19, 2020 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmariuzzo/32227f7e4a4476123580b492b41227cd to your computer and use it in GitHub Desktop.
Save rmariuzzo/32227f7e4a4476123580b492b41227cd to your computer and use it in GitHub Desktop.
GApps Script / Shuffle selected slides
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex)
currentIndex -= 1
temporaryValue = array[currentIndex]
array[currentIndex] = array[randomIndex]
array[randomIndex] = temporaryValue
}
return array
}
function shuffleSelectedSlides() {
if (SlidesApp.getActivePresentation().getSelection().getSelectionType() !== SlidesApp.SelectionType.PAGE || SlidesApp.getActivePresentation().getSelection().getPageRange().getPages().length <= 1) {
SlidesApp.getUi().alert('Cannot shuffle slides', 'Select 2 or more slides.', SlidesApp.getUi().ButtonSet.OK)
return
}
var slideIds = SlidesApp.getActivePresentation().getSlides().map(function(slide) { return slide.getObjectId() })
var selectedSlideIds = SlidesApp.getActivePresentation().getSelection().getPageRange().getPages().map(function(slide) { return slide.getObjectId() })
var indexOffset = slideIds.indexOf(selectedSlideIds[0])
var selectedSlides = SlidesApp.getActivePresentation().getSelection().getPageRange().getPages().map(function(page) { return page.asSlide() })
shuffle(selectedSlides)
selectedSlides.forEach(function(slide, index) {
slide.move(index + indexOffset)
})
}
function onOpen() {
var ui = SlidesApp.getUi()
ui.createMenu('≈ Extra ≈')
.addItem('Shuffle selected slides', 'shuffleSelectedSlides')
.addToUi()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment