You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/create-addin-commands.md
+55-49Lines changed: 55 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,7 @@
1
1
---
2
2
title: Create add-in commands with the add-in only manifest
3
3
description: Configure an add-in only manifest to define add-in commands for Excel, Outlook, PowerPoint, and Word. Use add-in commands to create UI elements, add buttons or menus, and perform actions.
4
-
ms.date: 09/05/2024
4
+
ms.date: 02/28/2025
5
5
ms.localizationpriority: medium
6
6
---
7
7
@@ -37,15 +37,15 @@ The following example shows the **\<VersionOverrides\>** element and its child e
@@ -69,7 +69,7 @@ The following example shows the **\<Hosts\>**, **\<Host\>**, and **\<DesktopForm
69
69
<Hostxsi:type="Workbook">
70
70
<DesktopFormFactor>
71
71
72
-
<!--information about FunctionFile and ExtensionPoint -->
72
+
<!--Information about FunctionFile and ExtensionPoint.-->
73
73
74
74
</DesktopFormFactor>
75
75
</Host>
@@ -82,21 +82,21 @@ The following example shows the **\<Hosts\>**, **\<Host\>**, and **\<DesktopForm
82
82
83
83
### Step 3: Add the FunctionFile element
84
84
85
-
The [**\<FunctionFile\>** element](/javascript/api/manifest/functionfile) specifies a file that contains JavaScript code to run when an add-in command uses the **ExecuteFunction** action. The **\<FunctionFile\>** element's **resid** attribute is set to a HTML file that includes all the JavaScript files your add-in commands require. You can't link directly to a JavaScript file. You can only link to an HTML file. The file name is specified as a [**\<Url\>** element](/javascript/api/manifest/url) in the [**\<Resources\>** element](/javascript/api/manifest/resources).
85
+
The [**\<FunctionFile\>** element](/javascript/api/manifest/functionfile) specifies a file that contains JavaScript or TypeScript code to run when an add-in command uses the **ExecuteFunction** action. The **\<FunctionFile\>** element's **resid** attribute is set to a HTML file that includes all the JavaScript or TypeScript files your add-in commands require. You can't link directly to a JavaScript or TypeScript file. You can only link to an HTML file. The file name is specified as a [**\<Url\>** element](/javascript/api/manifest/url) in the [**\<Resources\>** element](/javascript/api/manifest/resources).
86
86
87
87
> [!NOTE]
88
-
> The Yo Office projects use [webpack](https://webpack.js.org/concepts/) to avoid manually adding the JavaScript to the HTML.
88
+
> The Yo Office projects use [webpack](https://webpack.js.org/concepts/) to avoid manually adding the JavaScript or TypeScript to the HTML.
89
89
90
90
The following is an example of the **\<FunctionFile\>** element.
91
91
92
92
```xml
93
93
<DesktopFormFactor>
94
94
<FunctionFileresid="Commands.Url" />
95
95
<ExtensionPointxsi:type="PrimaryCommandSurface">
96
-
<!--information about this extension point -->
96
+
<!--Information about this extension point.-->
97
97
</ExtensionPoint>
98
98
99
-
<!-- You can define more than one ExtensionPoint element as needed -->
99
+
<!-- You can define more than one ExtensionPoint element as needed.-->
100
100
</DesktopFormFactor>
101
101
```
102
102
@@ -119,7 +119,7 @@ The following examples show how to use the **\<ExtensionPoint\>** element with *
119
119
```xml
120
120
<ExtensionPointxsi:type="PrimaryCommandSurface">
121
121
<CustomTabid="Contoso Tab">
122
-
<!-- If you want to use a default tab that comes with Office, remove the above CustomTab element, and then uncomment the following OfficeTab element -->
122
+
<!-- If you want to use a default tab that comes with Office, remove the above CustomTab element, and then uncomment the following OfficeTab element.-->
123
123
<!-- <OfficeTab id="TabData"> -->
124
124
<Labelresid="residLabel4" />
125
125
<Groupid="Group1Id12">
@@ -131,32 +131,33 @@ The following examples show how to use the **\<ExtensionPoint\>** element with *
131
131
</Icon>
132
132
<Controlxsi:type="Button"id="Button1Id1">
133
133
134
-
<!--information about the control -->
134
+
<!--Information about the control.-->
135
135
</Control>
136
-
<!--other controls, as needed -->
136
+
<!--Other controls, as needed.-->
137
137
</Group>
138
138
</CustomTab>
139
139
</ExtensionPoint>
140
140
<ExtensionPointxsi:type="ContextMenu">
141
141
<OfficeMenuid="ContextMenuCell">
142
142
<Controlxsi:type="Menu"id="ContextMenu2">
143
-
<!--information about the control -->
143
+
<!--Information about the control.-->
144
144
</Control>
145
-
<!--other controls, as needed -->
145
+
<!--Other controls, as needed.-->
146
146
</OfficeMenu>
147
147
</ExtensionPoint>
148
148
```
149
149
150
150
### Step 5: Add Control elements
151
151
152
-
The [**\<Control\>** element](/javascript/api/manifest/control) defines the usable surface of command (button, menu, etc) and the action associated with it.
152
+
The [**\<Control\>** element](/javascript/api/manifest/control) defines the usable surface of command, such as a button or menu, and the action associated with it.
153
153
154
154
#### Button controls
155
155
156
-
A [button control](/javascript/api/manifest/control-button) performs a single action when the user selects it. It can either execute a JavaScript function or show a task pane. The following example shows how to define two buttons. The first button runs a JavaScript function without showing a UI, and the second button shows a task pane. In the **\<Control\>** element:
156
+
A [button control](/javascript/api/manifest/control-button) performs a single action when the user selects it. It can either run a JavaScript or TypeScript function or show a task pane. The following example shows how to define two buttons. The first button runs a JavaScript function without showing a UI, and the second button shows a task pane. In the **\<Control\>** element:
157
157
158
158
- The **type** attribute is required, and must be set to **Button**.
159
159
- The **id** attribute of the **\<Control\>** element is a string with a maximum of 125 characters.
160
+
- The **xsi:type** attribute of the child [**\<Action\>** element](/javascript/api/manifest/action) must be set to **ExecuteFunction** to run a function or **ShowTaskpane** to display a task pane.
160
161
161
162
```xml
162
163
<!-- Define a control that calls a JavaScript function. -->
@@ -194,46 +195,14 @@ A [button control](/javascript/api/manifest/control-button) performs a single ac
194
195
</Control>
195
196
```
196
197
197
-
The following code shows an example function used by **\<FunctionName\>**. Note the call to [`event.completed`](/javascript/api/office/office.addincommands.event#office-office-addincommands-event-completed-member(1)). This signals that you've successfully handled the event. When a function is called multiple times, such as multiple clicks on the same add-in command, all events are automatically queued. The first event runs automatically, while the other events remain on the queue. When your function calls `event.completed`, the next queued call to that function runs. You must implement `event.completed`, otherwise your function won't run.
198
-
199
-
```js
200
-
// Initialize the Office Add-in.
201
-
Office.onReady(() => {
202
-
// If needed, Office.js is ready to be called
203
-
});
204
-
205
-
// The command function.
206
-
asyncfunctionhighlightSelection(event) {
207
-
208
-
// Implement your custom code here. The following code is a simple Excel example.
209
-
try {
210
-
awaitExcel.run(async (context) => {
211
-
constrange=context.workbook.getSelectedRange();
212
-
range.format.fill.color="yellow";
213
-
awaitcontext.sync();
214
-
});
215
-
} catch (error) {
216
-
// Note: In a production add-in, notify the user through your add-in's UI.
217
-
console.error(error);
218
-
}
219
-
220
-
// Calling event.completed is required. event.completed lets the platform know that processing has completed.
221
-
event.completed();
222
-
}
223
-
224
-
// You must register the function with the following line.
A [menu control](/javascript/api/manifest/control-menu) can be used with either **PrimaryCommandSurface** or **ContextMenu**, and defines:
232
201
233
202
- A root-level menu item.
234
203
- A list of submenu items.
235
204
236
-
When used with **PrimaryCommandSurface**, the root menu item displays as a button on the ribbon. When the button is selected, the submenu displays as a drop-down list. When used with **ContextMenu**, a menu item with a submenu is inserted on the context menu. In both cases, individual submenu items can either execute a JavaScript function or show a task pane. Only one level of submenus is supported at this time.
205
+
When used with **PrimaryCommandSurface**, the root menu item displays as a button on the ribbon. When the button is selected, the submenu displays as a drop-down list. When used with **ContextMenu**, a menu item with a submenu is inserted on the context menu. In both cases, individual submenu items can either run a JavaScript or TypeScript function or show a task pane. Only one level of submenus is supported at this time.
237
206
238
207
The following example shows how to define a menu item with two submenu items. The first submenu item shows a task pane, and the second submenu item runs a JavaScript function. In the **\<Control\>** element:
239
208
@@ -288,6 +257,43 @@ The following example shows how to define a menu item with two submenu items. Th
288
257
</Control>
289
258
```
290
259
260
+
#### Sample code for function commands
261
+
262
+
The following code shows a function that's invoked by a button or menu item control whose **\<Action\>** element's **xsi:type** is set to **ExecuteFunction**. Note the following about the code.
263
+
264
+
- The [Office.actions.associate](/javascript/api/office/office.actions#office-office-actions-associate-member(1)) call tells Office which function to run when a button or menu item is selected. The value passed to its **actionId** parameter must match the value specified in the [**\<FunctionName\>** element](/javascript/api/manifest/action#functionname) of the manifest. You must have an `Office.actions.associate` call for every function command defined in the manifest.
265
+
- The [event.completed](/javascript/api/office/office.addincommands.event#office-office-addincommands-event-completed-member(1)) call signals that you've successfully handled the event. When a function is called multiple times, such as multiple clicks on the same add-in command, all events are automatically queued. The first event runs automatically, while the other events remain on the queue. When your function calls `event.completed`, the next queued call to that function runs. You must implement `event.completed`, otherwise your function won't run.
266
+
267
+
```js
268
+
// Initialize the Office Add-in.
269
+
Office.onReady(() => {
270
+
// If needed, Office.js is ready to be called.
271
+
});
272
+
273
+
// The command function.
274
+
asyncfunctionhighlightSelection(event) {
275
+
276
+
// Implement your custom code here. The following code is a simple Excel example.
277
+
try {
278
+
awaitExcel.run(async (context) => {
279
+
constrange=context.workbook.getSelectedRange();
280
+
range.format.fill.color="yellow";
281
+
awaitcontext.sync();
282
+
});
283
+
} catch (error) {
284
+
// Note: In a production add-in, notify the user through your add-in's UI.
285
+
console.error(error);
286
+
}
287
+
288
+
// Calling event.completed is required. The event.completed call lets the platform know that processing has completed.
289
+
event.completed();
290
+
}
291
+
292
+
// This maps the function to the action ID specified in the manifest.
The [**\<Resources\>** element](/javascript/api/manifest/resources) contains resources used by the different child elements of the **\<VersionOverrides\>** element. Resources include icons, strings, and URLs. An element in the manifest can use a resource by referencing the **id** of the resource. Using the **id** helps organize the manifest, especially when there are different versions of the resource for different locales. An **id** has a maximum of 32 characters.
0 commit comments