Skip to content

Commit 1e6cac6

Browse files
authored
[Excel] (Custom Functions) Sample for using custom enum in custom function (#988)
* sample for using custom enum in custom function * resolve comments * resolve comment
1 parent 69d116c commit 1e6cac6

File tree

5 files changed

+111
-0
lines changed

5 files changed

+111
-0
lines changed

playlists-prod/excel.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@
287287
group: Custom Functions
288288
api_set:
289289
CustomFunctionsRuntime: 1.4
290+
- id: excel-custom-functions-custom-enum
291+
name: Function with custom enum parameters
292+
fileName: custom-enum.yaml
293+
description: >-
294+
Use custom enum as parameters in a custom function that searches for
295+
flights.
296+
rawUrl: >-
297+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-enum.yaml
298+
group: Custom Functions
299+
api_set:
300+
CustomFunctionsRuntime: '1.5'
290301
- id: excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts
291302
name: Using custom XML parts
292303
fileName: create-set-get-and-delete-custom-xml-parts.yaml

playlists/excel.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,17 @@
287287
group: Custom Functions
288288
api_set:
289289
CustomFunctionsRuntime: 1.4
290+
- id: excel-custom-functions-custom-enum
291+
name: Function with custom enum parameters
292+
fileName: custom-enum.yaml
293+
description: >-
294+
Use custom enum as parameters in a custom function that searches for
295+
flights.
296+
rawUrl: >-
297+
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-enum.yaml
298+
group: Custom Functions
299+
api_set:
300+
CustomFunctionsRuntime: '1.5'
290301
- id: excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts
291302
name: Using custom XML parts
292303
fileName: create-set-get-and-delete-custom-xml-parts.yaml
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
order: 7
2+
id: excel-custom-functions-custom-enum
3+
name: Function with custom enum parameters
4+
description: Use custom enum as parameters in a custom function that searches for flights.
5+
host: EXCEL
6+
api_set:
7+
CustomFunctionsRuntime: '1.5'
8+
script:
9+
content: |
10+
/**
11+
* A custom enum representing different airports.
12+
* @customenum {string}
13+
*/
14+
enum Airports {
15+
// Beijing is the capital of China.
16+
Beijing = "PEK",
17+
18+
// Shanghai is a major financial hub in China.
19+
Shanghai = "PVG",
20+
21+
// Seattle is known for its tech industry and the Space Needle.
22+
Seattle = "SEA",
23+
24+
// San Francisco is famous for the Golden Gate Bridge and tech startups.
25+
SanFrancisco = "SFO",
26+
27+
// Tokyo is the capital of Japan and known for its modern architecture and culture.
28+
Tokyo = "HND"
29+
}
30+
31+
/**
32+
* A custom enum representing the days of the week.
33+
* @customenum {number}
34+
*/
35+
enum DayOfWeek {
36+
Monday = 1,
37+
Tuesday = 2,
38+
Wednesday = 3,
39+
Thursday = 4,
40+
Friday = 5,
41+
Saturday = 6,
42+
Sunday = 7
43+
}
44+
45+
/**
46+
* A function that shows how to use custom enums to get a flight schedule.
47+
* @customfunction
48+
* @param {Airports} departure Where the flight departs.
49+
* @param {Airports} destination Where the flight arrives.
50+
* @param {DayOfWeek[]} day Days of the week when the flight is available.
51+
* @returns The available flight schedule.
52+
*/
53+
function fetchFlightSchedule(departure: Airports, destination: Airports, day: DayOfWeek[]): string[][] {
54+
const flights: string[][] = [];
55+
flights.push(["Flights from " + departure + " to " + destination, "", "", "", ""]);
56+
flights.push(["Day", "Flight Number", "Departure Time", "Arrival Time", "Price"]);
57+
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
58+
59+
day.forEach((d) => {
60+
const dayName = daysOfWeek[d - 1];
61+
const numberOfFlights = Math.floor(Math.random() * 3) + 1; // 1 to 3 flights
62+
63+
for (let i = 0; i < numberOfFlights; i++) {
64+
const flightNumber = `AA${Math.floor(Math.random() * 900) + 100}`;
65+
const departureTime = `${Math.floor(Math.random() * 12) + 1}:00 ${Math.random() > 0.5 ? "AM" : "PM"}`;
66+
const arrivalTime = `${Math.floor(Math.random() * 12) + 1}:00 ${Math.random() > 0.5 ? "AM" : "PM"}`;
67+
const price = `$${Math.floor(Math.random() * 500) + 100}`;
68+
69+
flights.push([dayName, flightNumber, departureTime, arrivalTime, price]);
70+
}
71+
});
72+
73+
return flights;
74+
}
75+
language: typescript
76+
libraries: |
77+
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
78+
@types/office-js
79+
80+
[email protected]/dist/css/fabric.min.css
81+
[email protected]/dist/css/fabric.components.min.css
82+
83+
[email protected]/client/core.min.js
84+
@types/core-js
85+
86+
87+

view-prod/excel.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"excel-custom-functions-web-call": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/web-call-function.yaml",
3131
"excel-custom-functions-errors": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-functions-errors.yaml",
3232
"excel-data-types-custom-functions": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/data-types-custom-functions.yaml",
33+
"excel-custom-functions-custom-enum": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-enum.yaml",
3334
"excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml",
3435
"excel-custom-xml-parts-test-xml-for-unique-namespace": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/18-custom-xml-parts/test-xml-for-unique-namespace.yaml",
3536
"excel-chart-chart-title-ts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/private-samples/excel/20-chart/chart-title-ts.yaml",

view/excel.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"excel-custom-functions-web-call": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/web-call-function.yaml",
3131
"excel-custom-functions-errors": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-functions-errors.yaml",
3232
"excel-data-types-custom-functions": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/data-types-custom-functions.yaml",
33+
"excel-custom-functions-custom-enum": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-enum.yaml",
3334
"excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/18-custom-xml-parts/create-set-get-and-delete-custom-xml-parts.yaml",
3435
"excel-custom-xml-parts-test-xml-for-unique-namespace": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/18-custom-xml-parts/test-xml-for-unique-namespace.yaml",
3536
"excel-chart-chart-title-ts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/private-samples/excel/20-chart/chart-title-ts.yaml",

0 commit comments

Comments
 (0)