Created
June 21, 2024 00:25
-
-
Save TheGroundZero/e78e9a1f3488cc12c0c0daa117078c69 to your computer and use it in GitHub Desktop.
Google Apps Script to extract TextStyle information from a slide
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
/** | |
* Extract TextStyle info for the text inside slide element based on Body placeholder | |
* @param {Number} slidenumber Number of the slide to analyze (starting from 1) | |
*/ | |
function analyseBodyStyles(slidenumber) { | |
const presentation = SlidesApp.getActivePresentation(); | |
const slide = presentation.getSlides()[slidenumber-1]; | |
const body = slide.getPlaceholders().find(ph => ph.asShape().getPlaceholderType() === SlidesApp.PlaceholderType.BODY); | |
const textRange = body.asShape().getText(); | |
const textRuns = textRange.getRuns(); | |
textRuns.forEach((run, i) => { | |
const tStyle = run.getTextStyle(); | |
const bgColor = tStyle.getBackgroundColor(); | |
const bgColorType = bgColor.getColorType(); | |
let colorBack = bgColorType; | |
if (bgColorType == SlidesApp.ColorType.RGB) { | |
colorBack = bgColor.asRgbColor().asHexString(); | |
} else if (bgColorType == SlidesApp.ColorType.THEME) { | |
colorBack = bgColor.asThemeColor().getThemeColorType(); | |
} | |
const fgColor = tStyle.getForegroundColor(); | |
const fgColorType = fgColor.getColorType(); | |
let colorFore = fgColorType; | |
if (fgColorType == SlidesApp.ColorType.RGB) { | |
colorFore = fgColor.asRgbColor().asHexString(); | |
} else if (fgColorType == SlidesApp.ColorType.THEME) { | |
colorFore = fgColor.asThemeColor().getThemeColorType(); | |
} | |
const isBold = (tStyle.isBold()) ? "B" : ""; | |
const isItalic = (tStyle.isItalic()) ? "I" : ""; | |
const isUnderline = (tStyle.isUnderline()) ? "U" : ""; | |
Logger.log( | |
i + " - " | |
+ run.asString().replace(/\n/g, "//") | |
+ " - B: " + colorBack | |
+ " - F: " + colorFore | |
+ " [" + isBold + isItalic + isUnderline + "]" | |
) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment