Skip to content

Commit db5af4a

Browse files
[PowerPoint] (shapes) Tweak group-ungroup samples (#5163)
1 parent 5937344 commit db5af4a

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

docs/powerpoint/shapes.md

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,25 @@ In PowerPoint, you can group several shapes and treat them like a single shape.
110110

111111
To group shapes with the JavaScript API, use [ShapeCollection.addGroup](/javascript/api/powerpoint/powerpoint.shapecollection#powerpoint-powerpoint-shapecollection-addgroup-member(1)).
112112

113-
The following example code shows how to group existing shapes of type [GeometricShape](/javascript/api/powerpoint/powerpoint.shapetype) found on the current slide.
113+
The following code sample shows how to group existing shapes of type [GeometricShape](/javascript/api/powerpoint/powerpoint.shapetype) found on the current slide.
114114

115115
```typescript
116116
// Groups the geometric shapes on the current slide.
117117
await PowerPoint.run(async (context) => {
118118
// Get the shapes on the current slide.
119119
context.presentation.load("slides");
120-
const slide: PowerPoint.Slide = context.presentation.getSelectedSlides().getItemAt(0);
121-
slide.load("shapes");
120+
const slide = context.presentation.getSelectedSlides().getItemAt(0);
121+
slide.load("shapes/items/type,shapes/items/id");
122122
await context.sync();
123123

124-
const shapes: PowerPoint.ShapeCollection = slide.shapes;
125-
shapes.load("items/type,items/id");
126-
await context.sync();
124+
const shapes = slide.shapes;
125+
const shapesToGroup = shapes.items.filter((item) => item.type === PowerPoint.ShapeType.geometricShape);
126+
if (shapesToGroup.length === 0) {
127+
console.warn("No shapes on the current slide, so nothing to group.");
128+
return;
129+
}
127130

128131
// Group the geometric shapes.
129-
const shapesToGroup = shapes.items.filter((item) => item.type === PowerPoint.ShapeType.geometricShape);
130132
console.log(`Number of shapes to group: ${shapesToGroup.length}`);
131133
const group = shapes.addGroup(shapesToGroup);
132134
group.load("id");
@@ -140,28 +142,25 @@ await PowerPoint.run(async (context) => {
140142

141143
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)).
142144

143-
The following code shows how to ungroup the first shape group found on the current slide.
145+
The following code sample shows how to ungroup the first shape group found on the current slide.
144146

145-
```typescript
147+
```js
146148
// Ungroups the first shape group on the current slide.
147149
await PowerPoint.run(async (context) => {
148150
// Get the shapes on the current slide.
149151
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");
152+
const slide = context.presentation.getSelectedSlides().getItemAt(0);
153+
slide.load("shapes/items/type,shapes/items/id");
156154
await context.sync();
157155

158-
// Ungroup the first grouped shapes.
156+
const shapes = slide.shapes;
159157
const shapeGroups = shapes.items.filter((item) => item.type === PowerPoint.ShapeType.group);
160158
if (shapeGroups.length == 0) {
161-
console.warn("No shape groups on the current slide so nothing to ungroup.");
159+
console.warn("No shape groups on the current slide, so nothing to ungroup.");
162160
return;
163161
}
164162

163+
// Ungroup the first grouped shapes.
165164
const firstGroupId = shapeGroups[0].id;
166165
const shapeGroupToUngroup = shapes.getItem(firstGroupId);
167166
shapeGroupToUngroup.group.ungroup();

0 commit comments

Comments
 (0)