Created
October 19, 2020 14:22
-
-
Save rmariuzzo/32227f7e4a4476123580b492b41227cd to your computer and use it in GitHub Desktop.
GApps Script / Shuffle selected slides
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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