Use a single context.sync() and batch operations on the shape to reduce the performance impact, especially in PowerPoint for the web.
shapes.addTextBox returns a Shape that can be fully configured before synchronizing with the host. The following pattern minimizes round trips and is consistent with the documented usage of addTextBox:
await PowerPoint.run(async (context) => {
const shapes = context.presentation.slides.getItemAt(0).shapes;
const textbox = shapes.addTextBox(text, {
left: 700,
top: calculatedTop,
height: 85,
width: 210,
});
// Configure all properties before syncing
textbox.fill.transparency = 0; // example of batching with other fill props
textbox.fill.setSolidColor('#CEC2FF');
textbox.textFrame.textRange.font.name = 'Arial';
textbox.textFrame.textRange.font.color = '#2905B7';
textbox.textFrame.textRange.font.size = 8;
textbox.textFrame.autoSizeSetting = 'AutoSizeShapeToFitText';
// Single sync for all operations
await context.sync();
});
Also ensure that:
-
shapes is obtained once per batch (as in the example above) rather than repeatedly in a loop with separate PowerPoint.run calls.
- If multiple text boxes are added, create and configure all of them in the same
PowerPoint.run and call context.sync() only once at the end.
This approach matches the documented pattern for ShapeCollection.addTextBox, which is designed to batch shape creation and property updates efficiently.
References: