Published: 2013/11/15
This topic shows you how to use Visual Studio to create a Hello World app for Office and then extend it to read, write, and bind to the document.
Applies to: apps for Office | Office 2013 | Office 365 | Excel Web App | Word 2013 | Excel 2013 | PowerPoint 2013
Install the following components before you get started to First task - Visual Studio
- Microsoft Visual Studio 2013
-or-
Visual Studio 2012 and the Microsoft Office Developer Tools for Visual Studio 2012.
To download Visual Studio 2012, see "Tools" on the Download page.
To download Visual Studio 2013, see the Visual Studio Downloads page - Word 2013
- Excel 2013
We'll start by creating and running a Hello World task pane app in Excel. Then we'll extend the app to perform the following tasks:
- Write data to the current selection in the worksheet.
- Read data from the current selection in the worksheet and display it in the app UI.
- Create a binding to the current selection in the worksheet.
- Read the data in the binding and display it in the app's UI.
- Add an event handler to read and display data whenever data in the binding is changed.
- Run the task pane app in Word.
- Run the app as a content app in Excel.
To get started, create an Apps for Office project in Visual Studio.
To create a project in Visual Studio
- Start Visual Studio.
- On the File menu, choose New Project.
The New Project dialog box opens. - In the list of project types under Visual C# or Visual Basic, expand Office/SharePoint, choose Apps, and then choose App for Office 2013.
- Name the project HelloWorld, and then choose OK.
The Create App for Office dialog box opens. By default, the option Task pane app in is selected along with the check boxes for Excel, Word, PowerPoint, and Project. That's what we want, so leave that option selected, and then choose Finish.
Visual Studio creates the project, and its files appear in Solution Explorer.
The default HelloWorld.html and HelloWorld.js pages of the project open in the Source view of the Visual Web Developer designer.
To design the appearance of the app, you add HTML to the default page of the project. To design the functionality and programming logic for your app, you can add JavaScript code directly in the HTML page, or to the default JavaScript file (HelloWorld.js).
In the following procedures, we'll extend your Hello World app to access data in the worksheet.
Now let's add an event handler that will read and display the data in the binding whenever it is changed.
In the next section we'll modify the app project so that you can run and test it in Word.
To create a Hello World app (First Task)
- In the designer, choose the Source tab if the editor isn't already in HTML Source view.
- Delete the tags inside the opening and closing <body> tags, and then type <div>Hello World!</div> inside the opening and closing body tags.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=Edge"/> <title>HelloWorld</title> <link rel="stylesheet" type="text/css" href="../Content/Office.css" /> <link rel="stylesheet" type="text/css" href="../Content/App.css" /> <script src="../Scripts/jquery-1.6.2.js"></script> <script src="../Scripts/Office/MicrosoftAjax.js"></script> <script src="../Scripts/Office/Office.js"></script> <!-- Add your JavaScript to the following file --> <script src="../Scripts/HelloWorld.js"></script> </head> <body> <div>Hello World!</div> </body> </html>
- On the Debug menu, choose Start Debugging or press the F5 key.
Excel opens a blank workbook and app appears in the task pane.
- Close the workbook file.
Debugging stops and focus returns to Visual Studio.
To write data to the worksheet
- Replace <div>Hello World!</div> inside the opening and closing <body> tags of the HelloWorld.html page with the following HTML.
<button onclick="writeData()"> Write Data </button><br/> <button onclick="readData()"> Read Selected Data </button><br/> <button onclick="bindData()"> Bind Selected Data </button><br/> <button onclick="readBoundData()"> Read Bound Data </button><br/> <button onclick="addEvent()"> Add Event </button><br/> <span>Results: </span><div id="results"></div>
- Open the HelloWorld.js file to display the default JavaScript file for the app. If it's not already open, you can find it in Solution Explorer under Scripts > Office.
- Add the following functions to the HelloWorld.js file.
function writeData() { Office.context.document.setSelectedDataAsync("Hello World!", function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } }); } function writeToPage(text) { document.getElementById('results').innerText = text; }
Note Make sure not to delete or overwrite the Office.initialize event handler function (although you can replace the code within it). The Office.initialize event handler must be in place for your app to initialize correctly at runtime.
The writeToPage(text) function is a helper function for writing text back to the results div on the app HTML page. The writeToPage(text) function is also used to display data and messages in the code examples in the following procedures. - On the Debug menu, choose Start Debugging or press the F5 key.
- Choose the Write Data button to write "Hello World!" to the current cell, but don't close the workbook or stop debugging yet.
- Switch back to the code editor, and replace "Hello World!" in the call to the setSelectedDataAsync method with [["red"],["green"],["blue"]] like this:
function writeData() { Office.context.document.setSelectedDataAsync([["red"],["green"],["blue"]], function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } }); }
[["red", "rojo"],["green", "verde"],["blue", "azul"]]
You can create a single row of three cells like this:
[["red","green","blue"]] - Choose Ctrl+S to save this change to the code.
- Now switch back to the workbook, open the shortcut menu (right-click) for the app task pane, and then choose Reload.
This reloads the HTML page with the updated JavaScript code. - Move the selection to a new cell, and then choose the Write Data button.
This writes the array containing red, green, and blue to a single column of three cells.
- Close the workbook to stop debugging.
To read data from the worksheet
- In Solution Explorer, open the HelloWorld.js file.
- Add the following code to the HelloWorld.js file below the functions you added in the previous procedure.
function readData() { Office.context.document.getSelectedDataAsync("matrix", function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } else{ writeToPage('Selected data: ' + asyncResult.value); } }); }
- On the Debug menu, choose Start Debugging or press the F5 key.
- Choose the Write Data button, leave the three cells that have red, green, and blue in them selected, and then choose the Read Selected Data button.
This reads the data from the three cells as a matrix data structure, and then writes those values to the app page.
- Close the workbook to stop debugging.
To create a binding for selected data and read the bound data
- In Solution Explorer, open the HelloWorld.js file.
- Add the following code to the HelloWorld.js file below the function you added in the previous procedure.
function bindData() { Office.context.document.bindings.addFromSelectionAsync("matrix", { id: 'myBinding' }, function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } else { writeToPage('Added binding with type: ' + asyncResult.value.type + ' and id: ' + asyncResult.value.id); } }); }
- Add the following code to the HelloWorld.js file below the bindData () function.
function readBoundData() { Office.select("bindings#myBinding").getDataAsync({ coercionType: "matrix" }, function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } else { writeToPage('Selected data: ' + asyncResult.value); } }); }
- On the Debug menu, choose Start Debugging or press the F5 key.
- Choose the Write Data button, leave three cells that have red, green, and blue in them selected, and then choose the Bind Selected Data button.
This creates a matrix binding that is associated with the three selected cells that have the id myBinding.
- Move the selection off of the three cells, and then choose the Read Bound Data button.
This will read the data from the binding created in the previous procedure, and then write those values to the app page. If you didn't change the values, red, green, and blue will be displayed in the app. - Change one or more values in the three cells, press the Enter key after each change, and then choose Read Bound Data again.
This will read the changed data and display it in the app.
- Close the workbook to stop debugging.
To add an event handler
- In Solution Explorer, open the HelloWorld.js file.
- Add the following code to HelloWorld.js file below the functions you added in the previous procedure.
function addEvent() { Office.select("bindings#myBinding").addHandlerAsync("bindingDataChanged", myHandler, function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } else { writeToPage('Added event handler'); } }); } function myHandler(eventArgs) { eventArgs.binding.getDataAsync({ coerciontype: "matrix" }, function (asyncResult) { if (asyncResult.status === "failed") { writeToPage('Error: ' + asyncResult.error.message); } else { writeToPage('Bound data: ' + asyncResult.value); } }); }
- On the Debug menu, choose Start Debugging or press the F5 key.
- Choose the Write Data button, leave three cells that have red, green, and blue in them selected, and then choose the Bind Selected Data button.
- Choose the Add Event button.
This retrieves the binding with id of myBinding, and then adds the myHandler function as the handler for the DataChanged event. - Change one or more values in the three bound cells, and press the Enter key after each change.
This will read the changed data and display it in the app task pane.
- Close Excel to stop debugging.
You can perform the following steps to modify this app project so that it will run and debug in Word 2013.
- Change the Start Action property of the project to start Word when debugging.
- Run and debug in Word.
To change the Start Action property in the Debugging property page of the project
- In Solution Explorer, choose the project name (HelloWorld).
The Project Properties property page for the project appears in the pane below the Solution Explorer. - Under App, in the Start Action list, choose Microsoft Word.
To debug the app in Word
- On the menu bar, choose Debug, Start Debugging.
Word 2013 opens with the HelloWorld app in the task pane. - Click the Write Data, Read Selected Data, Bind Selected Data, Read Bound Data, and Add Event buttons to perform the same actions as when working in Excel.
Note In Word, the event handler won't run to display the bound data until you move the cursor outside of the table inserted by the Write Data button.
You can perform the following steps to modify this app project so that it will run as a content app in Excel.
- Modify the manifest file to set the xsi:type attribute to "ContentApp" in the OfficeApp element.
- Modify the manifest file to set values for the RequestedWidth and RequestedHeight elements.
- Modify the manifest file to remove the "Presentation", "Project", and "Document"Capability elements from the Capabilities element.
- Change the Start Action property of the project to start in Excel.
To modify the manifest file
- In Solution Explorer, open the HelloWorld.xml file.
- In the opening OfficeApp tag, change the value of the xsi:type attribute to "ContentApp".
<OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ContentApp">
Note In this release of Office, content apps can run only in Excel 2013 and Excel Web App. After you change xsi:type to "ContentApp" in the manifest, the app will run only in Excel 2013 or Excel Web App. - Add the following RequestedWidth and RequestedHeight elements in the manifest within the <DefaultSettings> tags.
- Remove the "Presentation", "Project", and "Document"Capability elements from the Capabilities element, so that only the "Workbook" Capability element remains.
- Save these changes to the HelloWorld.xml file.
To change the Start Action property in the Debugging property page of the project
- In Solution Explorer, choose the project name (HelloWorld).
The Project Properties property page for the project appears in the pane below the Solution Explorer. - Under App, in the Start Action list, choose Microsoft Excel.
To debug the app in Excel
- On the menu bar, choose Debug, Start Debugging.
Excel 2013 opens with the HelloWorld app running as a content app in the worksheet. - Click the Write Data, Read Selected Data, Bind Selected Data, Read Bound Data, and Add Event buttons to perform the same actions as before.
And here, your first task using Visual Basic...
This article demonstrated how to create a Hello World task pane app for Excel, and then add some UI and JavaScript to read, write, and bind to data in the worksheet. We also added a simple event handler to the binding to re-read its data whenever it is changed. We then changed the Start Action to run the same code as a task pane app in Word. Finally, we modified the manifest to run the app as a content app in an Excel worksheet.
To learn more about developing apps for Office, see the following sections of the documentation.
This article demonstrated how to create a Hello World task pane app for Excel, and then add some UI and JavaScript to read, write, and bind to data in the worksheet. We also added a simple event handler to the binding to re-read its data whenever it is changed. We then changed the Start Action to run the same code as a task pane app in Word. Finally, we modified the manifest to run the app as a content app in an Excel worksheet.
To learn more about developing apps for Office, see the following sections of the documentation.
- For information about design guidelines, see Design apps for Office.
- For information about tools and development concepts, see Develop apps for Office.
- For information about deploying and publishing, see Publish apps for Office.
Tip To deploy and publish an app from Visual Studio, see How to: Publish an app for Office. To publish an app without using Visual Studio, you can deploy your app's HTML page and .js files on a web server, and then upload your app's manifest file to a network share catalog or app catalog on SharePoint. But, before uploading the manifest file, replace the ~remoteAppUrl token in the DefaultValue attribute of SourceLocation tag to specify the full URL of your app's default HTML page on the web server where it is hosted. - For API and manifest schema reference information, see Apps for Office API and schema references.
No comments:
Post a Comment