I’ve been exploring ways to access and manipulate metadata in PowerPoint, particularly Custom Properties. While I’ve come across the option to use Tags, they don’t seem to integrate well with Office-JS for my needs.
In Word, there’s a feature that allows updating fields in a more straightforward way, but I haven’t found a similar solution for PowerPoint. Is there an alternative method or workaround to access and update metadata in PowerPoint using Office-JS?
This is my code for Word and what I've done as a workaround for PowerPoint, but I'm not sure if it's the best way to do it, because I need to update data in text fields and this data should be saved in the metadata of the document. As you can see, I'm only able to insert OOXML with Word. Therefore, in PowerPoint I'm adding the data directly to the slide.
private async insertPlaceholderOoxml(
ooxml: string,
property: DocumentProperty,
fallbackValue?: string
): Promise<void> {
ooxml = this.getOoxmlWithReplacedDocproperty(ooxml, property, fallbackValue);
switch (this.host) {
case Office.HostType.Word:
await Word.run(async (context) => {
const range = context.document.getSelection();
range.insertOoxml(ooxml, Word.InsertLocation.replace);
});
break;
case Office.HostType.PowerPoint:
await PowerPoint.run(async (context) => {
const propertyValue = property.value || fallbackValue || "";
const selection = context.presentation.getSelectedShapes();
selection.load(["items"]);
await context.sync();
if (selection.items.length === 0) {
const shapes = context.presentation.slides.getItemAt(0).shapes;
const textbox = shapes.addTextBox(propertyValue);
textbox.name = property.name;
await context.sync();
} else {
const selectedShape = selection.items[0];
const textRange = selectedShape.textFrame.textRange;
textRange.load("text");
await context.sync();
textRange.text = textRange.text + propertyValue;
await context.sync();
}
});
break;
default:
break;
}
}
private getOoxmlWithReplacedDocproperty(ooxml: string, property: DocumentProperty, fallbackValue?: string): string {
return ooxml
.replace(new RegExp("{{docprop_name}}", "g"), property.name)
.replace(new RegExp("{{docprop_value}}", "g"), property.value)
.replace(new RegExp("{{docprop_fallback}}", "g"), fallbackValue);
}