Skip to content

Commit 58684e4

Browse files
authored
[Excel] (Shape) Add get active shape image snippet (#993)
* [Excel] (Shape) Add get active shape image snippet * Copilot reviewer feedback * Code review feedback * Update excel.xlsx, run yarn start
1 parent 8d00602 commit 58684e4

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed

playlists-prod/excel.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,15 @@
990990
group: Shape
991991
api_set:
992992
ExcelApi: '1.9'
993+
- id: excel-shape-get-active
994+
name: Get active shape image
995+
fileName: shape-get-active.yaml
996+
description: Get an image of the active shape in your workbook.
997+
rawUrl: >-
998+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-get-active.yaml
999+
group: Shape
1000+
api_set:
1001+
ExcelApi: '1.19'
9931002
- id: excel-table-add-rows-and-columns-to-a-table
9941003
name: Add rows and columns
9951004
fileName: add-rows-and-columns-to-a-table.yaml

playlists/excel.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,15 @@
990990
group: Shape
991991
api_set:
992992
ExcelApi: '1.9'
993+
- id: excel-shape-get-active
994+
name: Get active shape image
995+
fileName: shape-get-active.yaml
996+
description: Get an image of the active shape in your workbook.
997+
rawUrl: >-
998+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/44-shape/shape-get-active.yaml
999+
group: Shape
1000+
api_set:
1001+
ExcelApi: '1.19'
9931002
- id: excel-table-add-rows-and-columns-to-a-table
9941003
name: Add rows and columns
9951004
fileName: add-rows-and-columns-to-a-table.yaml
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
order: 7
2+
id: excel-shape-get-active
3+
name: Get active shape image
4+
description: Get an image of the active shape in your workbook.
5+
host: EXCEL
6+
api_set:
7+
ExcelApi: '1.19'
8+
script:
9+
content: |-
10+
document.getElementById("setup").addEventListener("click", () => tryCatch(setup));
11+
document.getElementById("add-shape-over-chart").addEventListener("click", () => tryCatch(addShapeOverChart));
12+
document.getElementById("group-chart-shapes").addEventListener("click", () => tryCatch(groupChartShapes));
13+
document.getElementById("get-active-shape").addEventListener("click", () => tryCatch(getActiveShape));
14+
15+
const sheetName = "Sample";
16+
17+
async function addShapeOverChart() {
18+
// This method adds a shape to use as a chart title.
19+
// It positions the shape over the chart on the worksheet.
20+
await Excel.run(async (context) => {
21+
const sheet = context.workbook.worksheets.getItem(sheetName);
22+
23+
// Create a rectangle shape for the chart title.
24+
const shape = sheet.shapes.addGeometricShape(Excel.GeometricShapeType.rectangle);
25+
shape.name = "ChartTitle";
26+
shape.height = 20;
27+
shape.width = 90;
28+
shape.textFrame.textRange.text = "Sales Quantity";
29+
shape.textFrame.textRange.font.size = 13;
30+
31+
// Get chart positioning information to place the shape correctly.
32+
const chart = sheet.charts.getItemAt(0);
33+
chart.load("left, top");
34+
chart.plotArea.load("width");
35+
await context.sync();
36+
37+
// Position the shape above the chart.
38+
shape.left = chart.left + chart.plotArea.width / 2.5;
39+
shape.top = chart.top + 10;
40+
41+
await context.sync();
42+
});
43+
}
44+
45+
async function groupChartShapes() {
46+
// This method groups the new chart title shape together with the existing chart.
47+
await Excel.run(async (context) => {
48+
const sheet = context.workbook.worksheets.getItem(sheetName);
49+
50+
// Get the shapes to group.
51+
const titleShape = sheet.shapes.getItem("ChartTitle");
52+
const chartShape = sheet.shapes.getItem("SalesChart");
53+
54+
// Create a group from the two shapes.
55+
const shapeGroup = sheet.shapes.addGroup([titleShape, chartShape]);
56+
shapeGroup.name = "GroupChart";
57+
58+
await context.sync();
59+
});
60+
}
61+
62+
async function getActiveShape() {
63+
// This method gets the active shape and displays it as an image in the task pane.
64+
await Excel.run(async (context) => {
65+
// Get the currently active shape, if any.
66+
const activeShape = context.workbook.getActiveShapeOrNullObject();
67+
68+
if (activeShape) {
69+
// Convert the active shape to an image.
70+
const shapeImage = activeShape.getAsImage(Excel.PictureFormat.png);
71+
await context.sync();
72+
73+
// Display the image in the task pane.
74+
const imageContainer = document.getElementById("image");
75+
imageContainer.innerHTML = ''; // Clear the container before adding a new image.
76+
const imageElement = document.createElement("img");
77+
imageElement.src = "data:image/png;base64," + shapeImage.value;
78+
imageContainer.appendChild(imageElement);
79+
} else {
80+
console.log("No active shape");
81+
}
82+
});
83+
}
84+
85+
/** Create sample data and a line chart. */
86+
async function setup() {
87+
await Excel.run(async (context) => {
88+
context.workbook.worksheets.getItemOrNullObject(sheetName).delete();
89+
const sheet = context.workbook.worksheets.add(sheetName);
90+
91+
// Add sample data to the worksheet.
92+
const dataRange = sheet.getRange("A1:C4");
93+
dataRange.values = sampleData;
94+
95+
// Create a line chart with markers.
96+
const chart = sheet.charts.add(Excel.ChartType.lineMarkers, dataRange);
97+
chart.name = "SalesChart";
98+
99+
sheet.activate();
100+
await context.sync();
101+
});
102+
}
103+
104+
/** Default helper for invoking an action and handling errors. */
105+
async function tryCatch(callback) {
106+
try {
107+
await callback();
108+
} catch (error) {
109+
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
110+
console.error(error);
111+
}
112+
}
113+
114+
/** Sample data for the chart. */
115+
const sampleData = [
116+
["Type", "Product A", "Product B"],
117+
["Q1", 15, 20],
118+
["Q2", 22, 15],
119+
["Q3", 33, 47]
120+
];
121+
language: typescript
122+
template:
123+
content: |-
124+
<section class="ms-Fabric ms-font-m">
125+
<p>This sample shows how to add a shape over a chart, group that new shape together with the existing chart, and then return an image of the chart (the active shape) to this task pane.</p>
126+
</section>
127+
<section class="ms-Fabric setup ms-font-m">
128+
<h3>Set up</h3>
129+
<button id="setup" class="ms-Button">
130+
<span class="ms-Button-label">Create a chart</span>
131+
</button>
132+
</section>
133+
<section class="ms-Fabric samples ms-font-m">
134+
<h3>Try it out</h3>
135+
<p>Add a shape to the workbook. This button adds a shape that covers the existing chart title with a new shape that contains text.</p>
136+
<button id="add-shape-over-chart" class="ms-Button">
137+
<span class="ms-Button-label">Add shape</span>
138+
</button>
139+
</section>
140+
<section class="ms-Fabric samples ms-font-m">
141+
<p>Group the new chart title together with the existing chart.</p>
142+
<button id="group-chart-shapes" class="ms-Button">
143+
<span class="ms-Button-label">Group shapes</span>
144+
</button>
145+
</section>
146+
<section class="ms-Fabric samples ms-font-m">
147+
<p>Get the active shape and display it as an image in this task pane. Make sure the chart is selected.</p>
148+
<button id="get-active-shape" class="ms-Button">
149+
<span class="ms-Button-label">Get active shape image</span>
150+
</button>
151+
</section>
152+
<section class="ms-Fabric ms-font-m">
153+
<p>Image of the active shape:</p>
154+
<div id="image"></div>
155+
</section>
156+
language: html
157+
style:
158+
content: |-
159+
section.samples {
160+
margin-top: 20px;
161+
}
162+
163+
section.samples .ms-Button, section.setup .ms-Button {
164+
display: block;
165+
margin-bottom: 5px;
166+
margin-left: 20px;
167+
min-width: 80px;
168+
}
169+
language: css
170+
libraries: |-
171+
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
172+
https://appsforoffice.microsoft.com/lib/1/hosted/office.d.ts
173+
174+
https://unpkg.com/[email protected]/dist/css/fabric.min.css
175+
https://unpkg.com/[email protected]/dist/css/fabric.components.min.css

snippet-extractor-metadata/excel.xlsx

56 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7596,6 +7596,34 @@
75967596

75977597
console.log("The active cell is " + activeCell.address);
75987598
});
7599+
'Excel.Workbook#getActiveShapeOrNullObject:member(1)':
7600+
- >-
7601+
// Link to full sample:
7602+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-get-active.yaml
7603+
7604+
7605+
// This method gets the active shape and displays it as an image in the task
7606+
pane.
7607+
7608+
await Excel.run(async (context) => {
7609+
// Get the currently active shape, if any.
7610+
const activeShape = context.workbook.getActiveShapeOrNullObject();
7611+
7612+
if (activeShape) {
7613+
// Convert the active shape to an image.
7614+
const shapeImage = activeShape.getAsImage(Excel.PictureFormat.png);
7615+
await context.sync();
7616+
7617+
// Display the image in the task pane.
7618+
const imageContainer = document.getElementById("image");
7619+
imageContainer.innerHTML = ''; // Clear the container before adding a new image.
7620+
const imageElement = document.createElement("img");
7621+
imageElement.src = "data:image/png;base64," + shapeImage.value;
7622+
imageContainer.appendChild(imageElement);
7623+
} else {
7624+
console.log("No active shape");
7625+
}
7626+
});
75997627
'Excel.Workbook#getSelectedRanges:member(1)':
76007628
- >-
76017629
// Link to full sample:

view-prod/excel.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"excel-shape-move-and-order": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-move-and-order.yaml",
103103
"excel-shape-groups": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-groups.yaml",
104104
"excel-shape-textboxes": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-textboxes.yaml",
105+
"excel-shape-get-active": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/44-shape/shape-get-active.yaml",
105106
"excel-table-add-rows-and-columns-to-a-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/46-table/add-rows-and-columns-to-a-table.yaml",
106107
"excel-table-convert-range-to-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/46-table/convert-range-to-table.yaml",
107108
"excel-table-create-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/46-table/create-table.yaml",

view/excel.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
"excel-shape-move-and-order": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/44-shape/shape-move-and-order.yaml",
103103
"excel-shape-groups": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/44-shape/shape-groups.yaml",
104104
"excel-shape-textboxes": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/44-shape/shape-textboxes.yaml",
105+
"excel-shape-get-active": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/44-shape/shape-get-active.yaml",
105106
"excel-table-add-rows-and-columns-to-a-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/46-table/add-rows-and-columns-to-a-table.yaml",
106107
"excel-table-convert-range-to-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/46-table/convert-range-to-table.yaml",
107108
"excel-table-create-table": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/46-table/create-table.yaml",

0 commit comments

Comments
 (0)