Skip to content

Commit a2c2062

Browse files
[PowerPoint] (shapes) Updates to include 1.4 Shape enums (#1033)
* [PowerPoint] (shapes) Updates to include 1.4 Shape enums * Refine text ranges HTML * Update TextRange snippet to include ParagraphFormat and BulletFormat references
1 parent 086a93f commit a2c2062

File tree

7 files changed

+413
-90
lines changed

7 files changed

+413
-90
lines changed

playlists-prod/powerpoint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,4 @@
186186
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/powerpoint/text/get-set-textrange.yaml
187187
group: Text
188188
api_set:
189-
PowerPointApi: '1.5'
189+
PowerPointApi: '1.8'

playlists/powerpoint.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,4 @@
186186
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/powerpoint/text/get-set-textrange.yaml
187187
group: Text
188188
api_set:
189-
PowerPointApi: '1.5'
189+
PowerPointApi: '1.8'

samples/powerpoint/shapes/get-set-shapes.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ script:
5353
shape2.load("id");
5454
await context.sync();
5555
56-
console.log(`IDs: ${shape1.id}, ${shape2.id}`)
56+
console.log(`IDs: ${shape1.id}, ${shape2.id}`);
5757
slide1.setSelectedShapes([shape1.id, shape2.id]);
5858
await context.sync();
5959
});
@@ -64,9 +64,11 @@ script:
6464
await PowerPoint.run(async (context) => {
6565
const shapes: PowerPoint.ShapeScopedCollection = context.presentation.getSelectedShapes();
6666
const shapeCount = shapes.getCount();
67-
shapes.load("items");
67+
shapes.load("items/fill/type");
6868
await context.sync();
6969
shapes.items.map((shape) => {
70+
const shapeFillType = shape.fill.type as PowerPoint.ShapeFillType;
71+
console.log(`Shape ID ${shape.id} original fill type: ${shapeFillType}`);
7072
shape.fill.setSolidColor("red");
7173
});
7274
await context.sync();
@@ -115,9 +117,7 @@ script:
115117
}
116118
117119
function generateRandomHexColor() {
118-
return `#${Math.random()
119-
.toString(16)
120-
.substring(2, 8)}`;
120+
return `#${Math.random().toString(16).substring(2, 8)}`;
121121
}
122122
123123
async function createShapes() {
@@ -131,7 +131,7 @@ script:
131131
const minNewShapeHeight = 50;
132132
for (let i = 0; i < 20; i++) {
133133
const rectangle: PowerPoint.Shape = currentSlide.shapes.addGeometricShape(
134-
PowerPoint.GeometricShapeType.rectangle
134+
PowerPoint.GeometricShapeType.rectangle,
135135
);
136136
rectangle.height = getRandomBetween(minNewShapeWidth, maxNewShapeWidth);
137137
rectangle.width = getRandomBetween(minNewShapeHeight, maxNewShapeHeight);

samples/powerpoint/shapes/shapes.yaml

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ host: POWERPOINT
66
api_set:
77
PowerPointApi: '1.4'
88
script:
9-
content: |-
9+
content: |
1010
document.getElementById("create-hexagon").addEventListener("click", () => tryCatch(createHexagon));
1111
document.getElementById("shrink-hexagon").addEventListener("click", () => tryCatch(shrinkHexagon));
1212
document.getElementById("move-hexagon").addEventListener("click", () => tryCatch(moveHexagon));
@@ -25,7 +25,7 @@ script:
2525
left: 100,
2626
top: 100,
2727
height: 150,
28-
width: 150
28+
width: 150,
2929
};
3030
const hexagon: PowerPoint.Shape = shapes.addGeometricShape(PowerPoint.GeometricShapeType.hexagon, shapeOptions);
3131
hexagon.name = "Hexagon";
@@ -69,13 +69,12 @@ script:
6969
7070
// For a line, left and top are the coordinates of the start point,
7171
// while height and width are the coordinates of the end point.
72-
const line: PowerPoint.Shape = shapes.addLine(PowerPoint.ConnectorType.straight,
73-
{
74-
left: 400,
75-
top: 200,
76-
height: 20,
77-
width: 150
78-
});
72+
const line: PowerPoint.Shape = shapes.addLine(PowerPoint.ConnectorType.straight, {
73+
left: 400,
74+
top: 200,
75+
height: 20,
76+
width: 150,
77+
});
7978
line.name = "StraightLine";
8079
8180
await context.sync();
@@ -88,13 +87,12 @@ script:
8887
// location, and size. Then it names the text box.
8988
await PowerPoint.run(async (context) => {
9089
const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes;
91-
const textbox: PowerPoint.Shape = shapes.addTextBox("Hello!",
92-
{
93-
left: 100,
94-
top: 300,
95-
height: 300,
96-
width: 450
97-
});
90+
const textbox: PowerPoint.Shape = shapes.addTextBox("Hello!", {
91+
left: 100,
92+
top: 300,
93+
height: 300,
94+
width: 450,
95+
});
9896
textbox.name = "Textbox";
9997
10098
return context.sync();
@@ -109,15 +107,17 @@ script:
109107
await PowerPoint.run(async (context) => {
110108
const shapes: PowerPoint.ShapeCollection = context.presentation.slides.getItemAt(0).shapes;
111109
const braces: PowerPoint.Shape = shapes.addGeometricShape(PowerPoint.GeometricShapeType.bracePair, {
112-
left: 100,
113-
top: 400,
114-
height: 50,
115-
width: 150
116-
});
110+
left: 100,
111+
top: 400,
112+
height: 50,
113+
width: 150,
114+
});
117115
braces.name = "Braces";
118116
braces.textFrame.textRange.text = "Shape text";
119117
braces.textFrame.textRange.font.color = "purple";
118+
braces.textFrame.textRange.font.underline = PowerPoint.ShapeFontUnderlineStyle.heavy;
120119
braces.textFrame.verticalAlignment = PowerPoint.TextVerticalAlignment.middleCentered;
120+
braces.textFrame.autoSizeSetting = PowerPoint.ShapeAutoSize.autoSizeShapeToFitText;
121121
122122
return context.sync();
123123
});

samples/powerpoint/text/get-set-textrange.yaml

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ name: Work with text range selections
44
description: Get, set, load, and save text range selections.
55
host: POWERPOINT
66
api_set:
7-
PowerPointApi: '1.5'
7+
PowerPointApi: '1.8'
88
script:
9-
content: |-
9+
content: |
1010
document.getElementById("getSelectedTextRange").addEventListener("click", () => tryCatch(getSelectedTextRange));
1111
document.getElementById("setSelectedTextRange").addEventListener("click", () => tryCatch(setSelectedTextRange));
1212
document.getElementById("changeColor").addEventListener("click", () => tryCatch(changeColor));
1313
document.getElementById("saveTextSelection").addEventListener("click", () => tryCatch(saveTextSelection));
1414
document.getElementById("loadTextSelection").addEventListener("click", () => tryCatch(loadTextSelection));
15+
document
16+
.getElementById("getSelectedTextRangeProperties")
17+
.addEventListener("click", () => tryCatch(getSelectedTextRangeComplexProperties));
1518
1619
async function getSelectedTextRange() {
1720
// Gets the selected text range and prints data about the range on the task pane.
@@ -37,15 +40,15 @@ script:
3740
txtExplained = txtExplained.replace(/\v/g, "<font color=red>VV</font>");
3841
let finalTable = "";
3942
finalTable +=
40-
"<br><table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
43+
"<table border=1 cellpadding=3 cellspacing=0><tr><td bgcolor=#3333EE><font color=white>Index</font></td><td bgcolor=#3333EE><font color=white>Id</font></td></tr>";
4144
finalTable += "<tr><td>Raw</td><td>" + textRange.text + "</td></tr>";
4245
finalTable += "<tr><td>Html</td><td>" + txtHtml + "</td></tr>";
4346
finalTable += "<tr><td>Exp</td><td>" + txtExplained + "</td></tr>";
4447
finalTable += "<tr><td>Start</td><td>" + textRange.start + "</td></tr>";
4548
finalTable += "<tr><td>Length</td><td>" + textRange.length + "</td></tr>";
4649
finalTable += "</table>";
4750
const outputSpan = document.getElementById("outputSpan");
48-
outputSpan.innerHTML = ""
51+
outputSpan.innerHTML = "";
4952
outputSpan.innerHTML += finalTable;
5053
});
5154
}
@@ -132,12 +135,42 @@ script:
132135
await PowerPoint.run(async (context) => {
133136
const slide1: PowerPoint.Slide = context.presentation.slides.getItem(savedTextSlideSelection[0]);
134137
const shape1: PowerPoint.Shape = slide1.shapes.getItem(savedTextShapeSelection[0]);
135-
const textRange: PowerPoint.TextRange = shape1.textFrame.textRange.getSubstring(savedTextTextRangeStart, savedTextTextRangeLength);
138+
const textRange: PowerPoint.TextRange = shape1.textFrame.textRange.getSubstring(
139+
savedTextTextRangeStart,
140+
savedTextTextRangeLength,
141+
);
136142
textRange.setSelected();
137143
await context.sync();
138144
});
139145
}
140146
147+
async function getSelectedTextRangeComplexProperties() {
148+
// Gets navigational (complex) properties of the selected text range.
149+
await PowerPoint.run(async (context) => {
150+
const textRange: PowerPoint.TextRange = context.presentation.getSelectedTextRange();
151+
textRange.load("font,paragraphFormat/bulletFormat,paragraphFormat/horizontalAlignment");
152+
await context.sync();
153+
154+
console.log("Font properties of selected text range:");
155+
console.log(`\tallCaps: ${textRange.font.allCaps}`);
156+
console.log(`\tbold: ${textRange.font.bold}`);
157+
console.log(`\tcolor: ${textRange.font.color}`);
158+
console.log(`\tdoubleStrikethrough: ${textRange.font.doubleStrikethrough}`);
159+
console.log(`\titalic: ${textRange.font.italic}`);
160+
console.log(`\tname: ${textRange.font.name}`);
161+
console.log(`\tsize: ${textRange.font.size}`);
162+
console.log(`\tsmallCaps: ${textRange.font.smallCaps}`);
163+
console.log(`\tstrikethrough: ${textRange.font.strikethrough}`);
164+
console.log(`\tsubscript: ${textRange.font.subscript}`);
165+
console.log(`\tsuperscript: ${textRange.font.superscript}`);
166+
console.log(`\tunderline: ${textRange.font.underline}`);
167+
168+
console.log("Paragraph format properties of selected text range:");
169+
console.log(`\tbulletFormat.visible: ${textRange.paragraphFormat.bulletFormat.visible}`);
170+
console.log(`\thorizontalAlignment: ${textRange.paragraphFormat.horizontalAlignment}`);
171+
});
172+
}
173+
141174
/** Default helper for invoking an action and handling errors. */
142175
async function tryCatch(callback) {
143176
try {
@@ -151,16 +184,20 @@ script:
151184
template:
152185
content: |-
153186
<section class="ms-Fabric ms-font-m">
154-
<p>This sample shows how to get selected text, and how to select specific text.</p>
187+
<p>This sample shows how to get selected text, and how to select specific text.</p>
155188
</section>
156189
<section class="ms-Fabric samples ms-font-m">
157-
<h3>Try it out</h3>
158-
<button id="getSelectedTextRange" class="ms-Button"><span class="ms-Button-label">Get selected text range</span></button>
159-
<br><button id="setSelectedTextRange" class="ms-Button"><span class="ms-Button-label">Selects the first 10 characters of the selected shape.</span></button>
160-
<br><button id="changeColor" class="ms-Button"><span class="ms-Button-label">Change color of selected text</span></button>
161-
<br><button id="saveTextSelection" class="ms-Button"><span class="ms-Button-label">Save text selection</span></button>
162-
<br><button id="loadTextSelection" class="ms-Button"><span class="ms-Button-label">Load the saved selection</span></button>
163-
<span id="outputSpan"></span>
190+
<h3>Try it out</h3>
191+
<p>Before choosing the <b>Get selected text range</b> button, add text to a slide and select some text.</p>
192+
<button id="getSelectedTextRange" class="ms-Button"><span class="ms-Button-label">Get selected text range</span></button>
193+
<button id="setSelectedTextRange" class="ms-Button"><span class="ms-Button-label">Selects first 10 characters of selected shape</span></button>
194+
<button id="changeColor" class="ms-Button"><span class="ms-Button-label">Change color of selected text</span></button>
195+
<button id="saveTextSelection" class="ms-Button"><span class="ms-Button-label">Save text selection</span></button>
196+
<button id="loadTextSelection" class="ms-Button"><span class="ms-Button-label">Load the saved selection</span></button>
197+
<button id="getSelectedTextRangeProperties" class="ms-Button"><span class="ms-Button-label">Get selected text range properties</span></button>
198+
<h4>Output</h4>
199+
<span id="outputSpan"></span>
200+
<p>Output from choosing the <b>Get selected text range</b> button is displayed in this section.</p>
164201
</section>
165202
language: html
166203
style:
316 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)