|
| 1 | +--- |
| 2 | +title: Custom enums for custom functions |
| 3 | +description: Create custom enums to use with your custom functions. |
| 4 | +ms.date: 06/24/2025 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Create custom enums for your custom functions |
| 9 | + |
| 10 | +:::image type="content" source="../images/custom-functions-custom-enum-autocomplete.png" alt-text="The members of a custom enum displaying in the Excel AutoComplete menu."::: |
| 11 | + |
| 12 | +Custom enums give users Excel AutoComplete options for your custom functions. The members in your enum show up in the formula bar as suggestions. The planets in the preceding screenshot are an example of a custom enum that provides a set list. This article describes how to create custom enums and use them as parameters in your custom functions. |
| 13 | + |
| 14 | +## Define the custom enum |
| 15 | + |
| 16 | +Define your enum with the JSDoc tag `@customenum`. The corresponding JSON properties are then autogenerated in your custom function metadata. For more information about JSDoc tags and custom functions, see [Basics of JSDoc tags](custom-functions-json-autogeneration.md#basics-of-jsdoc-tags). |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> Custom enums created with the `@customenum` JSDoc tag are only supported in TypeScript. They are not supported in JavaScript. To use custom enums with JavaScript functions, you must manually create your own JSON metadata. To learn more, see [Manually create JSON metadata](custom-functions-json.md). |
| 20 | +
|
| 21 | +The following code snippet shows how to define and use a simple custom enum as a parameter. |
| 22 | + |
| 23 | +```typescript |
| 24 | +/** |
| 25 | + * A custom enum with descriptions and tooltips. |
| 26 | + * @customenum {string} |
| 27 | + */ |
| 28 | +enum PLANETS { |
| 29 | + /** Mercury is the first planet from the sun. */ |
| 30 | + mercury = "Mercury", |
| 31 | + |
| 32 | + /** Venus is the second planet from the sun. */ |
| 33 | + venus = "Venus", |
| 34 | + |
| 35 | + /** Earth is the third planet from the sun. */ |
| 36 | + earth = "Earth", |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * A sample function that uses the custom enum as a parameter. |
| 41 | + * @customfunction |
| 42 | + */ |
| 43 | +function getPlanets(value: PLANETS): any { |
| 44 | + return value; |
| 45 | +} |
| 46 | +``` |
| 47 | + |
| 48 | +## Use a custom enum multiple times |
| 49 | + |
| 50 | +A custom enum can be reused in multiple functions, and it can be used as multiple parameters of a single function. A function can also have multiple enums as parameters at the same time. An enum parameter can be repeating or optional. |
| 51 | + |
| 52 | +The following code sample shows a `NUMBERS` enum and a custom function that uses the enum multiple times as an input value. |
| 53 | + |
| 54 | +```typescript |
| 55 | +/** |
| 56 | +* Enum of numbers with descriptions and tooltips. |
| 57 | +* @customenum {number} |
| 58 | +*/ |
| 59 | +enum NUMBERS { |
| 60 | + /** One */ |
| 61 | + One = 1, |
| 62 | + |
| 63 | + /** Two */ |
| 64 | + Two = 2, |
| 65 | + |
| 66 | + /** Three */ |
| 67 | + Three = 3, |
| 68 | + |
| 69 | + /** Four */ |
| 70 | + Four = 4, |
| 71 | + |
| 72 | + /** Five */ |
| 73 | + Five = 5 |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | +* Enter multiple numbers from the NUMBERS enum and get the sum. |
| 78 | +* @customfunction |
| 79 | +* @param input Enter enum numbers. |
| 80 | +* @returns |
| 81 | +*/ |
| 82 | +function addNumbers(input: NUMBERS[]): any { |
| 83 | + const sum = input.reduce((acc, num) => acc + num, 0); |
| 84 | + return "Sum: " + sum; |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +## Localize your custom enums |
| 89 | + |
| 90 | +Localizing custom enums is similar to [localizing custom functions](custom-functions-naming.md#localize-custom-functions). You must [manually create your JSON metadata](custom-functions-json.md) and then create a new JSON metadata file for each language. |
| 91 | + |
| 92 | +Note that only the `name` and `tooltip` properties within an enum should be localized to the target language. The `value` property should remain unchanged to avoid the need for handling multiple languages within your function body. |
| 93 | + |
| 94 | +The following JSON snippet shows the Chinese language localized `values` object for the planet Mercury. |
| 95 | + |
| 96 | +```json |
| 97 | +"enums": [ |
| 98 | + { |
| 99 | + "id": "PLANETS", |
| 100 | + "type": "string", |
| 101 | + "values": [ |
| 102 | + { |
| 103 | + "name": "水星", |
| 104 | + "value": "mercury", |
| 105 | + "tooltip": "水星是距离太阳最近的行星" |
| 106 | + } |
| 107 | + ] |
| 108 | + } |
| 109 | +], |
| 110 | +``` |
| 111 | + |
| 112 | +## Backwards compatibility |
| 113 | + |
| 114 | +Custom enums offer backwards compatibility. On older versions of Excel, parameters using a custom enum work as standard parameters without displaying in the Excel AutoComplete list. |
| 115 | + |
| 116 | +## See also |
| 117 | + |
| 118 | +- [Manually create JSON metadata for custom functions](custom-functions-json.md) |
| 119 | +- [Autogenerate JSON metadata for custom functions](custom-functions-json-autogeneration.md) |
0 commit comments