Created
October 2, 2024 15:06
-
-
Save Jytesh/feabfc2cc9c5cd73a6a9797c72bf3fb8 to your computer and use it in GitHub Desktop.
Colour Code Counter AppScript Extension for Slides
This file contains hidden or 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
| const index = { | |
| 'Prelims': '#00FF00', | |
| 'Pounce Bounce': '#3366cc', | |
| 'Anime': '#FF00FF', | |
| 'Reject': '#FF0000' | |
| } | |
| function myFunction() { | |
| const presentation = SlidesApp.getActivePresentation(); | |
| const slides = presentation.getSlides(); | |
| const count = Object.fromEntries(Object.keys(index).map(i => [i, 0])); | |
| for (let i = 0; i < slides.length; i++) { | |
| const slide = slides[i]; | |
| const shapes = slide.getShapes(); | |
| for (let j = 0; j < shapes.length; j++) { | |
| const shape = shapes[j]; | |
| if (shape.getParentPlaceholder() && shape.getPlaceholderType() === SlidesApp.PlaceholderType.TITLE && shape.getFill().getSolidFill()) { | |
| Logger.log(shape.getFill().getSolidFill().getColor().getColorType()) | |
| const colorType = shape.getFill().getSolidFill().getColor().getColorType(); | |
| let fillColor; | |
| if (colorType == SlidesApp.ColorType.RGB) fillColor = shape.getFill().getSolidFill().getColor().asRgbColor(); | |
| if (colorType == SlidesApp.ColorType.THEME) { | |
| const themeColor = shape.getFill().getSolidFill().getColor().asThemeColor(); | |
| fillColor = slide.getColorScheme().getConcreteColor(themeColor.getThemeColorType()).asRgbColor(); | |
| } | |
| Logger.log(fillColor.asHexString()) | |
| Object.entries(index).forEach(([k,v]) => { | |
| if (fillColor.asHexString().toLowerCase() == v.toLowerCase()) { | |
| count[k]++ | |
| } | |
| }) | |
| } | |
| } | |
| } | |
| const secondSLide = slides[1]; | |
| let table = secondSLide.getTables()[0]; | |
| if (!table) { | |
| table = secondSLide.insertTable(Object.keys(index).length, 2); | |
| } | |
| Object.entries(index).forEach(([k,v], i) => { | |
| const rgb = hexToRgb(v); | |
| const row = table.getRow(i) | |
| const title = row.getCell(0) | |
| title.getText().setText(k) | |
| title.getFill().setSolidFill(rgb.red, rgb.green, rgb.blue) | |
| const textCell = row.getCell(1) | |
| textCell.getText().setText(count[k]) | |
| textCell.getFill().setSolidFill(rgb.red, rgb.green, rgb.blue) | |
| }) | |
| let updatedOn = secondSLide.getShapes()[0]; | |
| if (!updatedOn) updatedOn = secondSLide.insertTextBox(`Updated on ${new Date().toDateString()}`, 50, 350, 400, 50); | |
| updatedOn.getText().setText(`Updated on ${getFmt()} ${new Date().toDateString()}`) | |
| } | |
| function getFmt() { | |
| const now = new Date(); | |
| let hours = now.getHours(); | |
| let minutes = now.getMinutes(); | |
| return hours + ':' + minutes; | |
| } | |
| function hexToRgb(hex) { | |
| hex = hex.replace(/^#/, ''); | |
| const red = parseInt(hex.substring(0, 2), 16) ; | |
| const green = parseInt(hex.substring(2, 4), 16) ; | |
| const blue = parseInt(hex.substring(4, 6), 16) ; | |
| console.log(hex, red, green, blue) | |
| return { red, green, blue }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment