Skip to content

[Excel] (Custom Functions) Sample for using custom enum in custom function #988

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions playlists-prod/excel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@
group: Custom Functions
api_set:
CustomFunctionsRuntime: 1.4
- id: excel-custom-functions-custom-enum-function
name: Function with custom enum parameters
fileName: custom-enum-function.yaml
description: Use custom enum as parameters in custom functions for searching flights.
rawUrl: >-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-enum-function.yaml
group: Custom Functions
api_set:
CustomFunctionsRuntime: '1.5'
- id: excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts
name: Using custom XML parts
fileName: create-set-get-and-delete-custom-xml-parts.yaml
Expand Down
9 changes: 9 additions & 0 deletions playlists/excel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,15 @@
group: Custom Functions
api_set:
CustomFunctionsRuntime: 1.4
- id: excel-custom-functions-custom-enum-function
name: Function with custom enum parameters
fileName: custom-enum-function.yaml
description: Use custom enum as parameters in custom functions for searching flights.
rawUrl: >-
https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-enum-function.yaml
group: Custom Functions
api_set:
CustomFunctionsRuntime: '1.5'
- id: excel-custom-xml-parts-create-set-get-and-delete-custom-xml-parts
name: Using custom XML parts
fileName: create-set-get-and-delete-custom-xml-parts.yaml
Expand Down
87 changes: 87 additions & 0 deletions samples/excel/16-custom-functions/custom-enum-function.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
order: 7
id: excel-custom-functions-custom-enum-function
name: Function with custom enum parameters
description: Use custom enum as parameters in custom functions for searching flights.
host: EXCEL
api_set:
CustomFunctionsRuntime: '1.5'
script:
content: |
/**
* Enum representing different airports.
* @customenum {string}
*/
enum AirPorts {
// Beijing is the capital of China.
Beijing = "PEK",

// Shanghai is a major financial hub in China.
Shanghai = "PVG",

// Seattle is known for its tech industry and the Space Needle.
Seattle = "SEA",

// San Francisco is famous for the Golden Gate Bridge and tech startups.
SanFrancisco = "SFO",

// Tokyo is the capital of Japan and known for its modern architecture and culture.
Tokyo = "HND"
}

/**
* Enum representing the days of the week.
* @customenum {number}
*/
enum DayOfWeek {
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6,
Sunday = 7
}

/**
* Function to get flight schedule.
* @customfunction
* @param {AirPorts} departure where the flight departs from
* @param {AirPorts} destination where the flight arrives
* @param {DayOfWeek[]} day days of the week when the flight is available
* @returns Available flight schedule
*/
function fetchFlightSchedule(departure: AirPorts, destination: AirPorts, day: DayOfWeek[]): string[][] {
const flights: string[][] = [];
flights.push(["Flights from " + departure + " to " + destination, "", "", "", ""]);
flights.push(["Day", "Flight Number", "Departure Time", "Arrival Time", "Price"]);
const daysOfWeek = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];

day.forEach((d) => {
const dayName = daysOfWeek[d - 1];
const numberOfFlights = Math.floor(Math.random() * 3) + 1; // 1 to 3 flights

for (let i = 0; i < numberOfFlights; i++) {
const flightNumber = `AA${Math.floor(Math.random() * 900) + 100}`;
const departureTime = `${Math.floor(Math.random() * 12) + 1}:00 ${Math.random() > 0.5 ? "AM" : "PM"}`;
const arrivalTime = `${Math.floor(Math.random() * 12) + 1}:00 ${Math.random() > 0.5 ? "AM" : "PM"}`;
const price = `$${Math.floor(Math.random() * 500) + 100}`;

flights.push([dayName, flightNumber, departureTime, arrivalTime, price]);
}
});

return flights;
}
language: typescript
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js

[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css

[email protected]/client/core.min.js
@types/core-js

[email protected]
@types/[email protected]
1 change: 1 addition & 0 deletions view-prod/excel.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"excel-custom-functions-web-call": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/web-call-function.yaml",
"excel-custom-functions-errors": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-functions-errors.yaml",
"excel-data-types-custom-functions": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/data-types-custom-functions.yaml",
"excel-custom-functions-custom-enum-function": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/samples/excel/16-custom-functions/custom-enum-function.yaml",
"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",
"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",
"excel-chart-chart-title-ts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/prod/private-samples/excel/20-chart/chart-title-ts.yaml",
Expand Down
1 change: 1 addition & 0 deletions view/excel.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"excel-custom-functions-web-call": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/web-call-function.yaml",
"excel-custom-functions-errors": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-functions-errors.yaml",
"excel-data-types-custom-functions": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/data-types-custom-functions.yaml",
"excel-custom-functions-custom-enum-function": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/samples/excel/16-custom-functions/custom-enum-function.yaml",
"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",
"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",
"excel-chart-chart-title-ts": "https://raw.githubusercontent.com/OfficeDev/office-js-snippets/main/private-samples/excel/20-chart/chart-title-ts.yaml",
Expand Down