I am working on creating a taskPane integration for microsoft powerPoint web and I am trying to get the font of each slide. This is the code I am working with
async function validateSlideDeck() {
await PowerPoint.run(async (context) => {
const slides = context.presentation.slides;
slides.load("items");
await context.sync();
for (let slide of slides.items) {
slide.shapes.load("items");
await context.sync();
for (let shape of slide.shapes.items) {
// Check if the shape has a text frame
if (shape.textFrame) {
shape.textFrame.textRange.load("text");
shape.textFrame.textRange.load("font"); // Load font properties to validate
await context.sync();
// Ensure there is actual text in the text range
if (shape.textFrame.textRange.text && shape.textFrame.textRange.text.length > 0) {
const font = shape.textFrame.textRange.font;
console.log(font.name);
}
}
}
The code is working fine and returning the correct font whenever I am selecting the text and changing the font after it is written. But it is returning empty value whenever I am changing the font via the Home tab and then writing any text.
Is there any way to get the value of a font when it is applied via Home Tab and not selecting the text and changing it after?
Thanks
I can't seem to replicate it exactly as you said it, however I was able to get empty string on font.name
when the shape contains text of different font where the Font selector is empty when the shape is selected
If this is the case for you, a very hacky workaround would be use the getSubstring() function to get only the first letter of the text and attempt to get the font again
const firstLetterTextRange = shape.textFrame.textRange.getSubstring(0,1);
firstLetterTextRange.load("font");
await context.sync();
console.log(firstLetterTextRange.font.name);
Full updated code:
await PowerPoint.run(async (context) => {
const slides = context.presentation.slides;
slides.load("items");
await context.sync();
for (let slide of slides.items) {
slide.shapes.load("items");
await context.sync();
for (let shape of slide.shapes.items) {
// Check if the shape has a text frame
if (shape.textFrame) {
shape.textFrame.textRange.load("text");
shape.textFrame.textRange.load("font"); // Load font properties to validate
await context.sync();
// Ensure there is actual text in the text range
if (
shape.textFrame.textRange.text &&
shape.textFrame.textRange.text.length > 0
) {
const firstLetterTextRange = shape.textFrame.textRange.getSubstring(
0,
1
);
firstLetterTextRange.load("font");
await context.sync();
console.log(firstLetterTextRange.font.name);
}
}
}
}
});