Skip to content

Instantly share code, notes, and snippets.

@TheGroundZero
Created June 21, 2024 00:25
Show Gist options
  • Save TheGroundZero/e78e9a1f3488cc12c0c0daa117078c69 to your computer and use it in GitHub Desktop.
Save TheGroundZero/e78e9a1f3488cc12c0c0daa117078c69 to your computer and use it in GitHub Desktop.
Google Apps Script to extract TextStyle information from a slide
/**
* 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