| page_type | sample | |||||||
|---|---|---|---|---|---|---|---|---|
| urlFragment | excel-content-add-in-hello-world | |||||||
| products |
|
|||||||
| languages |
|
|||||||
| extensions |
|
|||||||
| description | Create a Excel content add-in that gets the selected text then displays it. |
Learn how to build an Office content add-in that gets and displays the text selected from a Excel worksheet.
- Excel on Windows and in a browser.
| Version | Date | Comments |
|---|---|---|
| 1.0 | 02-04-2026 | Initial release |
| 1.1 | 06-19-2026 | Updated to use Excel JavaScript API |
- Microsoft 365 - You can get a free developer sandbox by joining the Microsoft 365 Developer Program.
When the user chooses the Get data from selection button, the getDataFromSelection() function is called. This function uses Workbook.getSelectedRange() to get the selected range and load its text. "Hello, world!" and the selected text are then displayed in the content add-in.
For more information, see Content add-ins.
// Reads data from current document selection and displays it.
async function getDataFromSelection() {
try {
await Excel.run(async (context) => {
const range = context.workbook.getSelectedRange();
range.load("text");
await context.sync();
// Join multi-cell selections into a single string.
const text = range.text.map((row) => row.join(", ")).join("; ");
document.getElementById("selected-data").textContent = 'Hello, world! The selected text is: ' + text;
});
} catch (error) {
document.getElementById("selected-data").textContent = 'Error getting selected text.';
console.error('Error:', error.message);
}
}An Office Add-in requires you to configure a web server to provide all the resources, such as HTML, image, and JavaScript files. The Hello World sample is configured so that the files are hosted directly from this GitHub repo, so all you need to do is build the manifest and package, and then sideload the package.
-
Clone or download this sample to a folder on your computer. Then in a command prompt, bash shell, or TERMINAL in Visual Studio Code, navigate to the root of the sample folder.
-
Run the command
npm install. -
Run the command
npm run build. -
Run the command
npm run start:prod.After a few seconds, desktop Excel opens, and after a few seconds more, the content add-in appears over the current worksheet with a Get data from selection button.
- If the content add-in doesn't appear, use the Add-ins button in the Home tab of the ribbon, then select the name of the content add-in, "Excel Content Add-in".
-
Choose the Get data from selection button to display "Hello, world!" and the selected text.
-
To try this out in Excel on the web:
- Open a workbook.
- Use the Add-ins button in the Home tab of the ribbon, then select the name of the content add-in, "Excel Content Add-in". If the add-in is absent, select the Manage link then open the add-in from there.
When you're finished working with the add-in, close Excel, and then in the window where you ran the three npm commands, run npm run stop:prod.
If you prefer to configure a web server and host the add-in's web files from your computer, use the following steps.
-
Clone or download this sample to a folder on your computer. Then in a command prompt, bash shell, or TERMINAL in Visual Studio Code, navigate to the root of the sample folder.
-
Run the command
npm install. -
Run the command
npm start.- If you've never developed an Office Add-in on this computer before or it has been more than 30 days since you last did, you'll be prompted to delete an old security cert or install a new one. Agree to both prompts.
- After a few seconds, a webpack dev-server window will open and your files will be hosted there on localhost:3000.
- When the server is successfully running, desktop Excel opens, and after a few seconds more, the content add-in appears over the current worksheet with a Get data from selection button.
- If the content add-in doesn't appear, use the Add-ins button in the Home tab of the ribbon, then select the name of the content add-in, "Excel Content Add-in".
-
Ensure that there's text in the worksheet, then select any range of cells containing text in the worksheet.
-
Choose the Get data from selection button to display "Hello, world!" and the selected text.
-
To try this out in Excel on the web:
- Open a workbook.
- Use the Add-ins button in the Home tab of the ribbon, then select the name of the content add-in, "Excel Content Add-in". If the add-in is absent, select the Manage link then open the add-in from there.
- Note: Depending on your machine's network or security settings, the add-in's web content might not be accessible.
When you're finished working with the add-in, close Excel, and then in the window where you ran the two npm commands, run npm stop.
- Did you experience any problems with the sample? Create an issue and we'll help you out.
- We'd love to get your feedback about this sample. Go to our Office samples survey to give feedback and suggest improvements.
- For general questions about developing Office Add-ins, go to Microsoft Q&A using the office-js-dev tag.
Copyright (c) Microsoft Corporation. All rights reserved.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
Note: The content.html file contains an image URL that tracks diagnostic data for this sample add-in. Please remove the image tag if you reuse this sample in your own code project.
