Skip to content

Commit 6554390

Browse files
[Excel] (ExcelApi 1.14) Add getPrecedents API to existing sample (OfficeDev#564)
* Rename direct-precedents.yaml to precedents.yaml, update snippet with ExcelApi 1.14 getPrecedents API * Update excel.xlsx with new precedents info * Run yarn start * Update samples/excel/42-range/precedents.yaml Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com> Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com>
1 parent 7ad1a36 commit 6554390

File tree

7 files changed

+92
-39
lines changed

7 files changed

+92
-39
lines changed

playlists-prod/excel.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,17 +644,17 @@
644644
group: Range
645645
api_set:
646646
ExcelApi: '1.1'
647-
- id: excel-direct-precedents
648-
name: Direct precedents
649-
fileName: direct-precedents.yaml
647+
- id: excel-precedents
648+
name: Precedents
649+
fileName: precedents.yaml
650650
description: >-
651651
This sample shows how to find and highlight the precedents of the currently
652652
selected cell. Precedents are cells referenced by the formula in a cell.
653653
rawUrl: >-
654-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/direct-precedents.yaml
654+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/precedents.yaml
655655
group: Range
656656
api_set:
657-
ExcelAPI: '1.12'
657+
ExcelApi: '1.14'
658658
- id: excel-range-style
659659
name: Style
660660
fileName: style.yaml

playlists/excel.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,17 +644,17 @@
644644
group: Range
645645
api_set:
646646
ExcelApi: '1.1'
647-
- id: excel-direct-precedents
648-
name: Direct precedents
649-
fileName: direct-precedents.yaml
647+
- id: excel-precedents
648+
name: Precedents
649+
fileName: precedents.yaml
650650
description: >-
651651
This sample shows how to find and highlight the precedents of the currently
652652
selected cell. Precedents are cells referenced by the formula in a cell.
653653
rawUrl: >-
654-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/direct-precedents.yaml
654+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/precedents.yaml
655655
group: Range
656656
api_set:
657-
ExcelAPI: '1.12'
657+
ExcelApi: '1.14'
658658
- id: excel-range-style
659659
name: Style
660660
fileName: style.yaml

samples/excel/42-range/direct-precedents.yaml renamed to samples/excel/42-range/precedents.yaml

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
order: 13
2-
id: excel-direct-precedents
3-
name: Direct precedents
2+
id: excel-precedents
3+
name: Precedents
44
description: This sample shows how to find and highlight the precedents of the currently selected cell. Precedents are cells referenced by the formula in a cell.
55
host: EXCEL
66
api_set:
7-
ExcelAPI: '1.12'
7+
ExcelApi: '1.14'
88
script:
99
content: |
1010
$("#setup").click(() => tryCatch(setup));
1111
$("#select-first-cell").click(() => tryCatch(selectFirstCell));
1212
$("#select-second-cell").click(() => tryCatch(selectSecondCell));
1313
$("#get-direct-precedents").click(() => tryCatch(getDirectPrecedents));
14+
$("#get-all-precedents").click(() => tryCatch(getAllPrecedents));
1415
$("#clear-highlighting").click(() => tryCatch(clearFormatting));
1516
16-
/** Select a cell with direct precedents on one worksheet. */
17+
/** Select a cell with precedents on one worksheet. */
1718
async function selectFirstCell() {
1819
await Excel.run(async (context) => {
1920
// Ensure correct worksheet is active.
@@ -27,7 +28,7 @@ script:
2728
});
2829
}
2930
30-
/** Select a cell with direct precedents on both worksheets. */
31+
/** Select a cell with precedents on both worksheets. */
3132
async function selectSecondCell() {
3233
await Excel.run(async (context) => {
3334
// Ensure correct worksheet is active.
@@ -44,6 +45,7 @@ script:
4445
async function getDirectPrecedents() {
4546
await Excel.run(async (context) => {
4647
// Precedents are cells referenced by the formula in a cell.
48+
// A "direct precedent" is a cell directly referenced by the selected formula.
4749
let range = context.workbook.getActiveCell();
4850
let directPrecedents = range.getDirectPrecedents();
4951
range.load("address");
@@ -52,7 +54,7 @@ script:
5254
5355
console.log(`Direct precedent cells of ${range.address}:`);
5456
55-
// Use the direct precedents API to loop through precedents of the active cell.
57+
// Use the direct precedents API to loop through precedents of the active cell.
5658
for (var i = 0; i < directPrecedents.areas.items.length; i++) {
5759
// Highlight and console the address of each precedent cell.
5860
directPrecedents.areas.items[i].format.fill.color = "Yellow";
@@ -62,6 +64,27 @@ script:
6264
});
6365
}
6466
67+
async function getAllPrecedents() {
68+
await Excel.run(async (context) => {
69+
// Precedents are cells referenced by the formula in a cell.
70+
let range = context.workbook.getActiveCell();
71+
let precedents = range.getPrecedents();
72+
range.load("address");
73+
precedents.areas.load("address");
74+
await context.sync();
75+
76+
console.log(`All precedent cells of ${range.address}:`);
77+
78+
// Use the precedents API to loop through precedents of the active cell.
79+
for (var i = 0; i < precedents.areas.items.length; i++) {
80+
// Highlight and console the address of each precedent cell.
81+
precedents.areas.items[i].format.fill.color = "Orange";
82+
console.log(` ${precedents.areas.items[i].address}`);
83+
}
84+
await context.sync();
85+
});
86+
}
87+
6588
/** Remove highlighting from precedent cells. */
6689
async function clearFormatting() {
6790
await Excel.run(async (context) => {
@@ -123,32 +146,37 @@ script:
123146
await context.sync();
124147
});
125148
}
126-
127-
/** Default helper for invoking an action and handling errors. */
128-
async function tryCatch(callback) {
129-
try {
130-
await callback();
131-
} catch (error) {
132-
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
133-
console.error(error);
134-
}
135-
}
136149
language: typescript
137150
template:
138151
content: |-
139152
<section class="ms-font-m">
140-
<p>This sample shows how to find and highlight the precedents of the currently selected cell. Precedents are cells referenced by the formula in a cell.</p>
153+
<p>This sample shows how to find and highlight the precedents of the currently selected cell. </p>
154+
<p>Precedents are cells referenced by the formula in a cell. A formula can also reference a cell that contains a formula, which results in a series of precedents. A "direct precedent" is a cell directly referenced by the selected formula. This sample shows how to return both the direct precedents and all of the precedents.</p>
141155
</section>
142156
<section class="setup ms-font-m">
143-
<h3>Set up</h3> <button id="setup" class="ms-Button"> <span class="ms-Button-label">Add sample data</span> </button>
157+
<h3>Set up</h3>
158+
<button id="setup" class="ms-Button">
159+
<span class="ms-Button-label">Add sample data</span>
160+
</button>
144161
</section>
145162
<section class="samples ms-font-m">
146163
<h3>Try it out</h3>
147-
<button id="select-first-cell" class="ms-Button"> <span class="ms-Button-label">Select a cell with direct precedents on this worksheet</span> </button>
148-
<button id="select-second-cell" class="ms-Button"> <span class="ms-Button-label">Select a cell with direct precedents across worksheets</span> </button>
149-
<button id="get-direct-precedents" class="ms-Button"> <span class="ms-Button-label">Get direct precedents</span> </button>
150-
<button id="clear-highlighting" class="ms-Button"> <span class="ms-Button-label">Clear highlighting</span> </button>
151-
</section>
164+
<button id="select-first-cell" class="ms-Button">
165+
<span class="ms-Button-label">Select a cell with precedents on this worksheet</span>
166+
</button>
167+
<button id="select-second-cell" class="ms-Button">
168+
<span class="ms-Button-label">Select a cell with precedents across worksheets</span>
169+
</button>
170+
<button id="get-direct-precedents" class="ms-Button">
171+
<span class="ms-Button-label">Get direct precedents</span>
172+
</button>
173+
<button id="get-all-precedents" class="ms-Button">
174+
<span class="ms-Button-label">Get all precedents</span>
175+
</button>
176+
<button id="clear-highlighting" class="ms-Button">
177+
<span class="ms-Button-label">Clear highlighting</span>
178+
</button>
179+
</section>
152180
language: html
153181
style:
154182
content: |
-76 Bytes
Binary file not shown.

snippet-extractor-output/snippets.yaml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3148,10 +3148,11 @@
31483148
'Excel.Range#getDirectPrecedents:member(1)':
31493149
- >-
31503150
// Link to full sample:
3151-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/direct-precedents.yaml
3151+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/precedents.yaml
31523152
31533153
await Excel.run(async (context) => {
31543154
// Precedents are cells referenced by the formula in a cell.
3155+
// A "direct precedent" is a cell directly referenced by the selected formula.
31553156
let range = context.workbook.getActiveCell();
31563157
let directPrecedents = range.getDirectPrecedents();
31573158
range.load("address");
@@ -3160,14 +3161,37 @@
31603161
31613162
console.log(`Direct precedent cells of ${range.address}:`);
31623163
3163-
// Use the direct precedents API to loop through precedents of the active cell.
3164+
// Use the direct precedents API to loop through precedents of the active cell.
31643165
for (var i = 0; i < directPrecedents.areas.items.length; i++) {
31653166
// Highlight and console the address of each precedent cell.
31663167
directPrecedents.areas.items[i].format.fill.color = "Yellow";
31673168
console.log(` ${directPrecedents.areas.items[i].address}`);
31683169
}
31693170
await context.sync();
31703171
});
3172+
'Excel.Range#getPrecedents:member(1)':
3173+
- >-
3174+
// Link to full sample:
3175+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/precedents.yaml
3176+
3177+
await Excel.run(async (context) => {
3178+
// Precedents are cells referenced by the formula in a cell.
3179+
let range = context.workbook.getActiveCell();
3180+
let precedents = range.getPrecedents();
3181+
range.load("address");
3182+
precedents.areas.load("address");
3183+
await context.sync();
3184+
3185+
console.log(`All precedent cells of ${range.address}:`);
3186+
3187+
// Use the precedents API to loop through precedents of the active cell.
3188+
for (var i = 0; i < precedents.areas.items.length; i++) {
3189+
// Highlight and console the address of each precedent cell.
3190+
precedents.areas.items[i].format.fill.color = "Orange";
3191+
console.log(` ${precedents.areas.items[i].address}`);
3192+
}
3193+
await context.sync();
3194+
});
31713195
'Excel.Range#getDirectDependents:member(1)':
31723196
- >-
31733197
// Link to full sample:
@@ -4953,10 +4977,11 @@
49534977
'Excel.WorkbookRangeAreas#areas:member':
49544978
- >-
49554979
// Link to full sample:
4956-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/direct-precedents.yaml
4980+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/precedents.yaml
49574981
49584982
await Excel.run(async (context) => {
49594983
// Precedents are cells referenced by the formula in a cell.
4984+
// A "direct precedent" is a cell directly referenced by the selected formula.
49604985
let range = context.workbook.getActiveCell();
49614986
let directPrecedents = range.getDirectPrecedents();
49624987
range.load("address");
@@ -4965,7 +4990,7 @@
49654990
49664991
console.log(`Direct precedent cells of ${range.address}:`);
49674992
4968-
// Use the direct precedents API to loop through precedents of the active cell.
4993+
// Use the direct precedents API to loop through precedents of the active cell.
49694994
for (var i = 0; i < directPrecedents.areas.items.length; i++) {
49704995
// Highlight and console the address of each precedent cell.
49714996
directPrecedents.areas.items[i].format.fill.color = "Yellow";

view-prod/excel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"excel-range-range-relationships": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/range-relationships.yaml",
6969
"excel-range-remove-duplicates": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/range-remove-duplicates.yaml",
7070
"excel-range-selected-range": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/selected-range.yaml",
71-
"excel-direct-precedents": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/direct-precedents.yaml",
71+
"excel-precedents": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/precedents.yaml",
7272
"excel-range-style": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/style.yaml",
7373
"excel-range-text-orientation": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/range-text-orientation.yaml",
7474
"excel-range-dynamic-arrays": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/42-range/dynamic-arrays.yaml",

view/excel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"excel-range-range-relationships": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/range-relationships.yaml",
6969
"excel-range-remove-duplicates": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/range-remove-duplicates.yaml",
7070
"excel-range-selected-range": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/selected-range.yaml",
71-
"excel-direct-precedents": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/direct-precedents.yaml",
71+
"excel-precedents": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/precedents.yaml",
7272
"excel-range-style": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/style.yaml",
7373
"excel-range-text-orientation": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/range-text-orientation.yaml",
7474
"excel-range-dynamic-arrays": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/42-range/dynamic-arrays.yaml",

0 commit comments

Comments
 (0)