Skip to content

Commit 511d15b

Browse files
[PowerPoint] (shapes) Add how to group and ungroup shapes (#5160)
1 parent 923c653 commit 511d15b

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

docs/powerpoint/shapes.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Work with shapes using the PowerPoint JavaScript API
33
description: Learn how to add, remove, and format shapes on PowerPoint slides.
4-
ms.date: 03/14/2023
4+
ms.date: 05/06/2025
55
ms.localizationpriority: medium
66
---
77

@@ -102,6 +102,75 @@ await PowerPoint.run(async (context) => {
102102
});
103103
```
104104

105+
## Group and ungroup shapes
106+
107+
In PowerPoint, you can group several shapes and treat them like a single shape. You can subsequently ungroup grouped shapes. To learn more about grouping objects in the PowerPoint UI, see [Group or ungroup shapes, pictures, or other objects](https://support.microsoft.com/office/a7374c35-20fe-4e0a-9637-7de7d844724b).
108+
109+
### Group shapes
110+
111+
To group shapes with the JavaScript API, use [ShapeCollection.addGroup](/javascript/api/powerpoint/powerpoint.shapecollection#powerpoint-powerpoint-shapecollection-addgroup-member(1)).
112+
113+
The following example code shows how to group existing shapes of type [GeometricShape](/javascript/api/powerpoint/powerpoint.shapetype) found on the current slide.
114+
115+
```typescript
116+
// Groups the geometric shapes on the current slide.
117+
await PowerPoint.run(async (context) => {
118+
// Get the shapes on the current slide.
119+
context.presentation.load("slides");
120+
const slide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
121+
slide.load("shapes");
122+
await context.sync();
123+
124+
const shapes: PowerPoint.ShapeCollection = slide.shapes;
125+
shapes.load("items/type,items/id");
126+
await context.sync();
127+
128+
// Group the geometric shapes.
129+
const shapesToGroup = shapes.items.filter((item) => item.type === PowerPoint.ShapeType.geometricShape);
130+
console.log(`Number of shapes to group: ${shapesToGroup.length}`);
131+
const group = shapes.addGroup(shapesToGroup);
132+
group.load("id");
133+
await context.sync();
134+
135+
console.log(`Grouped shapes. Group ID: ${group.id}`);
136+
});
137+
```
138+
139+
### Ungroup shapes
140+
141+
To ungroup shapes with the JavaScript API, get the [group](/javascript/api/powerpoint/powerpoint.shape#powerpoint-powerpoint-shape-group-member) property from the group's `Shape` object then call [ShapeGroup.ungroup](/javascript/api/powerpoint/powerpoint.shapegroup#powerpoint-powerpoint-shapegroup-ungroup-member(1)).
142+
143+
The following code shows how to ungroup the first shape group found on the current slide.
144+
145+
```typescript
146+
// Ungroups the first shape group on the current slide.
147+
await PowerPoint.run(async (context) => {
148+
// Get the shapes on the current slide.
149+
context.presentation.load("slides");
150+
const slide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
151+
slide.load("shapes");
152+
await context.sync();
153+
154+
const shapes: PowerPoint.ShapeCollection = slide.shapes;
155+
shapes.load("items/type,items/id");
156+
await context.sync();
157+
158+
// Ungroup the first grouped shapes.
159+
const shapeGroups = shapes.items.filter((item) => item.type === PowerPoint.ShapeType.group);
160+
if (shapeGroups.length == 0) {
161+
console.warn("No shape groups on the current slide so nothing to ungroup.");
162+
return;
163+
}
164+
165+
const firstGroupId = shapeGroups[0].id;
166+
const shapeGroupToUngroup = shapes.getItem(firstGroupId);
167+
shapeGroupToUngroup.group.ungroup();
168+
await context.sync();
169+
170+
console.log(`Ungrouped shapes with group ID: ${firstGroupId}`);
171+
});
172+
```
173+
105174
## Delete shapes
106175

107176
Shapes are removed from the slide with the `Shape` object's `delete` method.
@@ -123,3 +192,9 @@ await PowerPoint.run(async (context) => {
123192
await context.sync();
124193
});
125194
```
195+
196+
## See also
197+
198+
- [Work with tables using the PowerPoint JavaScript API](work-with-tables.md)
199+
- [Bind to shapes in a PowerPoint presentation](bind-shapes-in-presentation.md)
200+
- [Group or ungroup shapes, pictures, or other objects](https://support.microsoft.com/office/a7374c35-20fe-4e0a-9637-7de7d844724b)

docs/toc.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,15 +910,15 @@ items:
910910
- name: Use document themes in your PowerPoint add-ins
911911
href: powerpoint/use-document-themes-in-your-powerpoint-add-ins.md
912912
displayName: PowerPoint
913-
- name: Work with tables
914-
href: powerpoint/work-with-tables.md
915-
displayName: PowerPoint
916913
- name: Work with shapes
917914
href: powerpoint/shapes.md
918915
displayName: PowerPoint
919916
- name: Bind shapes to a data source
920917
href: powerpoint/bind-shapes-in-presentation.md
921918
displayName: PowerPoint
919+
- name: Work with tables
920+
href: powerpoint/work-with-tables.md
921+
displayName: PowerPoint
922922
- name: Project
923923
items:
924924
- name: Project add-ins documentation

0 commit comments

Comments
 (0)