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
+
0 commit comments