Skip to content

Commit 7ad1a36

Browse files
authored
Merge pull request OfficeDev#560 from Rick-Kirkham/shapes
work with shapes in PPT
2 parents 8b24148 + bee4a01 commit 7ad1a36

File tree

7 files changed

+292
-0
lines changed

7 files changed

+292
-0
lines changed

playlists-prod/powerpoint.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
group: Preview APIs
5454
api_set:
5555
PowerPointApi: '1.3'
56+
- id: powerpoint-shapes
57+
name: 'Insert shape, line, and text box'
58+
fileName: shapes.yaml
59+
description: 'Inserts geometric shapes, lines, and text boxes to a slide.'
60+
rawUrl: >-
61+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/shapes.yaml
62+
group: Preview APIs
63+
api_set: PowerPointApi '1.3'
5664
- id: powerpoint-tags
5765
name: Work with tags
5866
fileName: tags.yaml

playlists/powerpoint.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@
5353
group: Preview APIs
5454
api_set:
5555
PowerPointApi: '1.3'
56+
- id: powerpoint-shapes
57+
name: 'Insert shape, line, and text box'
58+
fileName: shapes.yaml
59+
description: 'Inserts geometric shapes, lines, and text boxes to a slide.'
60+
rawUrl: >-
61+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/preview-apis/shapes.yaml
62+
group: Preview APIs
63+
api_set: PowerPointApi '1.3'
5664
- id: powerpoint-tags
5765
name: Work with tags
5866
fileName: tags.yaml
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
id: powerpoint-shapes
2+
name: 'Insert shape, line, and text box'
3+
description: 'Inserts geometric shapes, lines, and text boxes to a slide.'
4+
host: POWERPOINT
5+
api_set: PowerPointApi '1.3'
6+
script:
7+
content: |
8+
$("#create-hexagon").click(() => tryCatch(createHexagon));
9+
$("#shrink-hexagon").click(() => tryCatch(shrinkHexagon));
10+
$("#move-hexagon").click(() => tryCatch(moveHexagon));
11+
$("#create-line").click(() => tryCatch(createLine));
12+
$("#create-text-box").click(() => tryCatch(createTextBox));
13+
$("#create-shape-with-text").click(() => tryCatch(createShapeWithText));
14+
$("#remove-all").click(() => tryCatch(removeAll));
15+
16+
// This function gets the collection of shapes on the first slide,
17+
// and adds a hexagon shape to the collection, while specifying its
18+
// location and size. Then it names the shape.
19+
async function createHexagon() {
20+
await PowerPoint.run(async (context) => {
21+
var shapes = context.presentation.slides.getItemAt(0).shapes;
22+
var hexagon = shapes.addGeometricShape(PowerPoint.GeometricShapeType.hexagon,
23+
{
24+
left: 100,
25+
top: 100,
26+
height: 150,
27+
width: 150
28+
});
29+
hexagon.name = "Hexagon";
30+
31+
await context.sync();
32+
});
33+
}
34+
35+
// This function gets the collection of shapes on the first slide,
36+
// gets the first shape in the collection, and resets its size.
37+
async function shrinkHexagon() {
38+
await PowerPoint.run(async (context) => {
39+
var shapes = context.presentation.slides.getItemAt(0).shapes;
40+
var hexagon = shapes.getItemAt(0);
41+
hexagon.height = 50;
42+
hexagon.width = 50;
43+
44+
await context.sync();
45+
});
46+
}
47+
48+
// This function gets the collection of shapes on the first slide,
49+
// gets the first shape in the collection, and resets its location.
50+
async function moveHexagon() {
51+
await PowerPoint.run(async (context) => {
52+
var shapes = context.presentation.slides.getItemAt(0).shapes;
53+
var hexagon = shapes.getItemAt(0);
54+
hexagon.top = 50;
55+
hexagon.left = 150;
56+
57+
await context.sync();
58+
});
59+
}
60+
61+
// This function gets the collection of shapes on the first slide,
62+
// and adds a line to the collection, while specifying its
63+
// start and end points. Then it names the shape.
64+
async function createLine() {
65+
await PowerPoint.run(async (context) => {
66+
var shapes = context.presentation.slides.getItemAt(0).shapes;
67+
68+
// For a line, left and top are the coordinates of the start point,
69+
// while height and width are the coordinates of the end point.
70+
var line = shapes.addLine(PowerPoint.ConnectorType.straight,
71+
{
72+
left: 400,
73+
top: 200,
74+
height: 20,
75+
width: 150
76+
});
77+
line.name = "StraightLine";
78+
79+
await context.sync();
80+
});
81+
}
82+
83+
// This function gets the collection of shapes on the first slide,
84+
// and adds a text box to the collection, while specifying its text,
85+
// location, and size. Then it names the text box.
86+
async function createTextBox() {
87+
await PowerPoint.run(async (context) => {
88+
var shapes = context.presentation.slides.getItemAt(0).shapes;
89+
var textbox = shapes.addTextBox("Hello!",
90+
{
91+
left: 100,
92+
top: 300,
93+
height: 300,
94+
width: 450
95+
});
96+
textbox.name = "Textbox";
97+
98+
return context.sync();
99+
});
100+
}
101+
102+
// This function gets the collection of shapes on the first slide,
103+
// and adds a brace pair, {}, to the collection, while specifying its
104+
// location and size. Then it names the shape, sets its text and font
105+
// color, and centers it inside the braces.
106+
async function createShapeWithText() {
107+
await PowerPoint.run(async (context) => {
108+
var shapes = context.presentation.slides.getItemAt(0).shapes;
109+
var braces = shapes.addGeometricShape(PowerPoint.GeometricShapeType.bracePair, {
110+
left: 100,
111+
top: 400,
112+
height: 50,
113+
width: 150
114+
});
115+
braces.name = "Braces";
116+
braces.textFrame.textRange.text = "Shape text";
117+
braces.textFrame.textRange.font.color = "purple";
118+
braces.textFrame.verticalAlignment = PowerPoint.TextVerticalAlignment.middleCentered;
119+
120+
return context.sync();
121+
});
122+
}
123+
124+
// This function gets the collection of shapes on the first slide,
125+
// and then iterates through them, deleting each one.
126+
async function removeAll() {
127+
await PowerPoint.run(async (context) => {
128+
const slide = context.presentation.slides.getItemAt(0);
129+
const shapes = slide.shapes;
130+
131+
// Load all the shapes in the collection without loading their properties.
132+
shapes.load("items/$none");
133+
134+
await context.sync();
135+
136+
shapes.items.forEach((shape) => shape.delete());
137+
138+
await context.sync();
139+
});
140+
}
141+
142+
/** Default helper for invoking an action and handling errors. */
143+
async function tryCatch(callback) {
144+
try {
145+
await callback();
146+
} catch (error) {
147+
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
148+
console.error(error);
149+
}
150+
}
151+
language: typescript
152+
template:
153+
content: |-
154+
<section class="ms-font-m">
155+
<p>This sample shows how to create, resize, move, and delete shapes.</p>
156+
</section>
157+
158+
<section class="samples ms-font-m">
159+
<h3>Try it out</h3>
160+
<button id="create-hexagon" class="ms-Button">
161+
<span class="ms-Button-label">Create hexagon</span>
162+
</button>
163+
<p />
164+
<button id="shrink-hexagon" class="ms-Button">
165+
<span class="ms-Button-label">Shrink hexagon</span>
166+
</button>
167+
<p />
168+
<button id="move-hexagon" class="ms-Button">
169+
<span class="ms-Button-label">Move hexagon</span>
170+
</button>
171+
<p />
172+
<button id="create-line" class="ms-Button">
173+
<span class="ms-Button-label">Create line</span>
174+
</button>
175+
<p />
176+
177+
<button id="create-text-box" class="ms-Button">
178+
<span class="ms-Button-label">Create text box</span>
179+
</button>
180+
<p />
181+
182+
<button id="create-shape-with-text" class="ms-Button">
183+
<span class="ms-Button-label">Create shape with text</span>
184+
</button>
185+
<p />
186+
187+
<button id="remove-all" class="ms-Button">
188+
<span class="ms-Button-label">Remove all shapes</span>
189+
</button>
190+
<p />
191+
</section>
192+
language: html
193+
style:
194+
content: |-
195+
section.samples {
196+
margin-top: 20px;
197+
}
198+
199+
section.samples .ms-Button, section.setup .ms-Button {
200+
display: block;
201+
margin-bottom: 5px;
202+
margin-left: 20px;
203+
min-width: 80px;
204+
}
205+
language: css
206+
libraries: |
207+
https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
208+
@types/office-js-preview
209+
210+
[email protected]/dist/css/fabric.min.css
211+
[email protected]/dist/css/fabric.components.min.css
212+
213+
[email protected]/client/core.min.js
214+
@types/core-js
215+
216+
217+
183 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9942,6 +9942,63 @@
99429942
99439943
console.log(JSON.stringify(presentationTags));
99449944
});
9945+
'PowerPoint.ShapeCollection#addGeometricShape:member(1)':
9946+
- >-
9947+
// Link to full sample:
9948+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/shapes.yaml
9949+
9950+
await PowerPoint.run(async (context) => {
9951+
var shapes = context.presentation.slides.getItemAt(0).shapes;
9952+
var hexagon = shapes.addGeometricShape(PowerPoint.GeometricShapeType.hexagon,
9953+
{
9954+
left: 100,
9955+
top: 100,
9956+
height: 150,
9957+
width: 150
9958+
});
9959+
hexagon.name = "Hexagon";
9960+
9961+
await context.sync();
9962+
});
9963+
'PowerPoint.ShapeCollection#addLine:member(1)':
9964+
- >-
9965+
// Link to full sample:
9966+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/shapes.yaml
9967+
9968+
await PowerPoint.run(async (context) => {
9969+
var shapes = context.presentation.slides.getItemAt(0).shapes;
9970+
9971+
// For a line, left and top are the coordinates of the start point,
9972+
// while height and width are the coordinates of the end point.
9973+
var line = shapes.addLine(PowerPoint.ConnectorType.straight,
9974+
{
9975+
left: 400,
9976+
top: 200,
9977+
height: 20,
9978+
width: 150
9979+
});
9980+
line.name = "StraightLine";
9981+
9982+
await context.sync();
9983+
});
9984+
'PowerPoint.ShapeCollection#addTextBox:member(1)':
9985+
- >-
9986+
// Link to full sample:
9987+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/shapes.yaml
9988+
9989+
await PowerPoint.run(async (context) => {
9990+
var shapes = context.presentation.slides.getItemAt(0).shapes;
9991+
var textbox = shapes.addTextBox("Hello!",
9992+
{
9993+
left: 100,
9994+
top: 300,
9995+
height: 300,
9996+
width: 450
9997+
});
9998+
textbox.name = "Textbox";
9999+
10000+
return context.sync();
10001+
});
994510002
'Word.Body#search:member(1)':
994610003
- >-
994710004
// Link to full sample:

view-prod/powerpoint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"powerpoint-basics-insert-image": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/basics/insert-image.yaml",
66
"powerpoint-basics-insert-svg": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/basics/insert-svg.yaml",
77
"powerpoint-add-slides": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/add-slides.yaml",
8+
"powerpoint-shapes": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/shapes.yaml",
89
"powerpoint-tags": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/preview-apis/tags.yaml",
910
"powerpoint-scenarios-searches-wikipedia-api": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/scenarios/searches-wikipedia-api.yaml",
1011
"powerpoint-insert-slides": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/slide-management/insert-slides.yaml"

view/powerpoint.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"powerpoint-basics-insert-image": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/basics/insert-image.yaml",
66
"powerpoint-basics-insert-svg": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/basics/insert-svg.yaml",
77
"powerpoint-add-slides": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/preview-apis/add-slides.yaml",
8+
"powerpoint-shapes": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/preview-apis/shapes.yaml",
89
"powerpoint-tags": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/preview-apis/tags.yaml",
910
"powerpoint-scenarios-searches-wikipedia-api": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/scenarios/searches-wikipedia-api.yaml",
1011
"powerpoint-insert-slides": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/slide-management/insert-slides.yaml"

0 commit comments

Comments
 (0)