Skip to content

Commit 9119ed5

Browse files
davidchesnutElizabethSamuel-MSFTlindalu-MSFT
authored
[PowerPoint] New articles for working with tables and shape bindings (#5105)
* Draft article on working with table APIs * draft article on working with bindings * Fixes to samples and image * add powerpoint articles to toc * Apply suggestions from code review Co-authored-by: Linda Cannon <[email protected]> * Update image text and ms.date --------- Co-authored-by: Elizabeth Samuel <[email protected]> Co-authored-by: Linda Cannon <[email protected]>
1 parent d6ea371 commit 9119ed5

12 files changed

+600
-1
lines changed
59.4 KB
Loading
63.5 KB
Loading
7.32 KB
Loading
1.14 KB
Loading
22 KB
Loading
21 KB
Loading
3.1 KB
Loading
5.05 KB
Loading
4.92 KB
Loading
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
title: Bind to shapes in a PowerPoint presentation
3+
description: Learn how to bind shapes and access them from your add-in to keep them up to date.
4+
ms.topic: how-to
5+
ms.date: 04/02/2025
6+
ms.localizationpriority: medium
7+
---
8+
9+
# Bind to shapes in a PowerPoint presentation
10+
11+
Your PowerPoint add-in can bind to shapes to consistently access them through an identifier. The add-in establishes a binding by calling `BindingCollection.add` and assigning a unique identifier. Use the identifier at any time to reference the shape and access its properties. Creating bindings provides the following value to your add-in.
12+
13+
- Establishes a relationship between the add-in and the shape in the document. Bindings are persisted in the document and can be accessed at a later time.
14+
- Enables access to shape properties to read or update, without requiring the user to select any shapes.
15+
16+
The following image shows how an add-in might bind to two shapes on a slide. Each shape has a binding ID created by the add-in: `star` and `pie`. Using the binding ID, the add-in can access the desired shape to update properties.
17+
18+
:::image type="content" source="../images/powerpoint-bind-shapes.png" alt-text="Binding to a star shape with the ID 'star' and binding to a pie chart with the ID 'pie'.":::
19+
20+
## Scenario: Use bindings to sync with a data source
21+
22+
A common scenario for using bindings is to keep shapes up to date with a data source. Often when creating a presentation, users copy and paste images from the data source into the presentation. Over time, to keep the images up to date, they will manually copy and paste the latest images from the data source. An add-in can help automate this process by retrieving up-to-date images from the data source on the user’s behalf. When a shape fill needs updating, the add-in uses the binding to find the correct shape and update the shape fill with the newer image.
23+
24+
In a general implementation, there are two components to consider for binding a shape in PowerPoint and updating it with a new image from a data source.
25+
26+
1. **The data source**. This is any source of data or asset library such as Microsoft SharePoint or Microsoft OneDrive.
27+
1. **The PowerPoint add-in**. The add-in gets data from the data source based on what the user needs. It converts the data to a Base64-encoded image. This is the only fill type the bound shape can accept. It inserts a shape upon the user’s request and binds it with a unique identifier. Then it fills the shape with the Base64 image based on the original data source. Shapes are updated upon the user’s request and the add-in uses the binding identifier to find the shape and update the image with the last saved Base64 image.
28+
29+
> [!NOTE]
30+
> You decide the implementation details of how to sync updates from the data source and how to get or create images. This article only describes how to use the Office JS APIs in your add-in to bind a shape and update it with latest images.
31+
32+
## Create a bound shape in PowerPoint
33+
34+
Use the `PowerPoint.BindingsCollection.add()` method for the presentation to create a binding which refers to a particular shape.
35+
36+
:::image type="content" source="../images/powerpoint-steps-to-bind-shape.png" alt-text="Add-in creates a Base64-encoded image from data source, then creates the shape from the image and adds a unique ID.":::
37+
38+
The following sample shows how to create a shape on the first selected slide.
39+
40+
```javascript
41+
await PowerPoint.run(async (context) => {
42+
const slides = context.presentation.getSelectedSlides();
43+
44+
// Insert new shape on first selected slide.
45+
const myShape = slides
46+
.getItemAt(0)
47+
.shapes.addGeometricShape(PowerPoint.GeometricShapeType.rectangle, {
48+
top: 100,
49+
left: 30,
50+
width: 200,
51+
height: 200
52+
});
53+
54+
// Fill shape with a Base64-encoded image.
55+
// Note: The image is typically created from a data source request.
56+
const productsImage = "...base64 image data...";
57+
myShape.fill.setImage(productsImage);
58+
});
59+
```
60+
61+
Call `BindingsCollection.add` to add the binding to the bindings collection in PowerPoint. The following sample shows how to add a new binding for a shape to the bindings collection.
62+
63+
```javascript
64+
// Create a binding ID to track the shape for later updates.
65+
const bindingId = "productChart";
66+
// Create binding by adding the new shape to the bindings collection.
67+
context.presentation.bindings.add(myShape, PowerPoint.BindingType.shape, bindingId);
68+
```
69+
70+
## Refresh a bound shape with updated data
71+
72+
After there's an update to the image data, refresh the shape image by finding it via the binding identifier. The following code sample shows how to find a bound shape with the identifier and fill it with an updated image. The image is updated by the add-in based on the data source request or provided by the data source directly.
73+
74+
```javascript
75+
async function updateBinding(bindingId, image) {
76+
await PowerPoint.run(async (context) => {
77+
try {
78+
// Get the shape based on binding ID.
79+
const myShape = context.presentation.bindings
80+
.getItem(bindingId)
81+
.getShape();
82+
83+
// Update the shape to latest image.
84+
myShape.fill.setImage(image);
85+
await context.sync();
86+
87+
} catch (err) {
88+
console.error(err);
89+
}
90+
});
91+
}
92+
```
93+
94+
## Delete a binding
95+
96+
The following sample shows how to delete a binding by deleting it from the bindings collection.
97+
98+
```javascript
99+
async function deleteBinding(bindingId) {
100+
await PowerPoint.run(async (context) => {
101+
context.presentation.bindings.getItemAt(bindingId).delete();
102+
await context.sync();
103+
});
104+
}
105+
```
106+
107+
## Load bindings
108+
109+
When a user opens a presentation and your add-in first loads, you can load all the bindings to continue working with them. The following code shows how to load all bindings in a presentation and display them in the console.
110+
111+
```javascript
112+
async function loadBindings() {
113+
await PowerPoint.run(async (context) => {
114+
try {
115+
let myBindings = context.presentation.bindings;
116+
myBindings.load("items");
117+
await context.sync();
118+
119+
// Log all binding IDs to console.
120+
if (myBindings.items.length > 0) {
121+
myBindings.items.forEach(async (binding) => {
122+
console.log(binding.id);
123+
});
124+
}
125+
} catch (err) {
126+
console.error(err);
127+
}
128+
});
129+
}
130+
```
131+
132+
## Error handling when a binding or shape is deleted
133+
134+
When a shape is deleted, its associated binding is also removed from the PowerPoint binding collection. Any object references you have to the binding, or shape, will return errors if you access any properties or methods on those objects. Be sure to handle potential error scenarios for a deleted shape if your add-in keeps Binding or Shape objects.
135+
136+
The following code shows one approach to error handling when a binding object references a deleted binding. Use a try/catch statement and then call a function to reload all binding and shape references when an error occurs.
137+
138+
```javascript
139+
async function getShapeFromBindingID(id) {
140+
await PowerPoint.run(async (context) => {
141+
try {
142+
const binding = context.presentation.bindings.getItemAt(id);
143+
const shape = binding.getShape();
144+
145+
await context.sync();
146+
return shape;
147+
} catch (err) {
148+
console.log(err);
149+
return undefined;
150+
}
151+
});
152+
}
153+
```
154+
155+
## See also
156+
157+
When maintaining freshness on shapes, you may also want to check the zOrder. See the [zOrderPosition](/javascript/api/powerpoint/powerpoint.shape?view=powerpoint-js-preview&preserve-view=true) property for more information.
158+
159+
- [Work with shapes using the PowerPoint JavaScript API](shapes.md)
160+
- [Bind to regions in a document or spreadsheet](../develop/bind-to-regions-in-a-document-or-spreadsheet.md)

0 commit comments

Comments
 (0)