|
| 1 | +In this step of the tutorial, you'll programmatically test that your add-in supports the user's current version of Excel, add a table to a worksheet, populate the table with data, and format it. |
| 2 | + |
| 3 | +## Code the add-in |
| 4 | + |
| 5 | +1. Open the project in your code editor. |
| 6 | +2. Open the file index.html. |
| 7 | +3. Replace the `TODO1` with the following markup: |
| 8 | + |
| 9 | + ```html |
| 10 | + <button class="ms-Button" id="create-table">Create Table</button> |
| 11 | + ``` |
| 12 | + |
| 13 | +4. Open the app.js file. |
| 14 | +5. Replace the `TODO1` with the following code. This code determines whether the user's version of Excel supports a version of Excel.js that includes all the APIs that this series of tutorials will use. In a production add-in, use the body of the conditional block to hide or disable the UI that would call unsupported APIs. This will enable the user to still make use of the parts of the add-in that are supported by their version of Excel. |
| 15 | + |
| 16 | + ```js |
| 17 | + if (!Office.context.requirements.isSetSupported('ExcelApi', 1.7)) { |
| 18 | + console.log('Sorry. The tutorial add-in uses Excel.js APIs that are not available in your version of Office.'); |
| 19 | + } |
| 20 | + ``` |
| 21 | + |
| 22 | +6. Replace the `TODO2` with the following code: |
| 23 | + |
| 24 | + ```js |
| 25 | + $('#create-table').click(createTable); |
| 26 | + ``` |
| 27 | + |
| 28 | +7. Replace the `TODO3` with the following code. Note the following: |
| 29 | + - Your Excel.js business logic will be added to the function that is passed to `Excel.run`. This logic does not execute immediately. Instead, it is added to a queue of pending commands. |
| 30 | + - The `context.sync` method sends all queued commands to Excel for execution. |
| 31 | + - The `Excel.run` is followed by a `catch` block. This is a best practice that you should always follow. |
| 32 | + |
| 33 | + ```js |
| 34 | + function createTable() { |
| 35 | + Excel.run(function (context) { |
| 36 | + |
| 37 | + // TODO4: Queue table creation logic here. |
| 38 | + |
| 39 | + // TODO5: Queue commands to populate the table with data. |
| 40 | + |
| 41 | + // TODO6: Queue commands to format the table. |
| 42 | + |
| 43 | + return context.sync(); |
| 44 | + }) |
| 45 | + .catch(function (error) { |
| 46 | + console.log("Error: " + error); |
| 47 | + if (error instanceof OfficeExtension.Error) { |
| 48 | + console.log("Debug info: " + JSON.stringify(error.debugInfo)); |
| 49 | + } |
| 50 | + }); |
| 51 | + } |
| 52 | + ``` |
| 53 | + |
| 54 | +8. Replace `TODO4` with the following code. Note: |
| 55 | + - The code creates a table by using `add` method of a worksheet's table collection, which always exists even if it is empty. This is the standard way that Excel.js objects are created. There are no class constructor APIs, and you never use a `new` operator to create an Excel object. Instead, you add to a parent collection object. |
| 56 | + - The first parameter of the `add` method is the range of only the top row of the table, not the entire range the table will ultimately use. This is because when the add-in populates the data rows (in the next step), it will add new rows to the table instead of writing values to the cells of existing rows. This is a more common pattern because the number of rows that a table will have is often not known when the table is created. |
| 57 | + - Table names must be unique across the entire workbook, not just the worksheet. |
| 58 | + |
| 59 | + ```js |
| 60 | + const currentWorksheet = context.workbook.worksheets.getActiveWorksheet(); |
| 61 | + const expensesTable = currentWorksheet.tables.add("A1:D1", true /*hasHeaders*/); |
| 62 | + expensesTable.name = "ExpensesTable"; |
| 63 | + ``` |
| 64 | + |
| 65 | +9. Replace `TODO5` with the following code. Note: |
| 66 | + - The cell values of a range are set with an array of arrays. |
| 67 | + - New rows are created in a table by calling the `add` method of the table's row collection. You can add multiple rows in a single call of `add` by including multiple cell value arrays in the parent array that is passed as the second parameter. |
| 68 | + |
| 69 | + ```js |
| 70 | + expensesTable.getHeaderRowRange().values = |
| 71 | + [["Date", "Merchant", "Category", "Amount"]]; |
| 72 | + |
| 73 | + expensesTable.rows.add(null /*add at the end*/, [ |
| 74 | + ["1/1/2017", "The Phone Company", "Communications", "120"], |
| 75 | + ["1/2/2017", "Northwind Electric Cars", "Transportation", "142.33"], |
| 76 | + ["1/5/2017", "Best For You Organics Company", "Groceries", "27.9"], |
| 77 | + ["1/10/2017", "Coho Vineyard", "Restaurant", "33"], |
| 78 | + ["1/11/2017", "Bellows College", "Education", "350.1"], |
| 79 | + ["1/15/2017", "Trey Research", "Other", "135"], |
| 80 | + ["1/15/2017", "Best For You Organics Company", "Groceries", "97.88"] |
| 81 | + ]); |
| 82 | + ``` |
| 83 | + |
| 84 | +10. Replace `TODO6` with the following code. Note: |
| 85 | + - The code gets a reference to the **Amount** column by passing its zero-based index to the `getItemAt` method of the table's column collection. |
| 86 | + |
| 87 | + > [!NOTE] |
| 88 | + > Excel.js collection objects, such as `TableCollection`, `WorksheetCollection`, and `TableColumnCollection` have an `items` property that is an array of the child object types, such as `Table` or `Worksheet` or `TableColumn`; but a `*Collection` object is not itself an array. |
| 89 | + |
| 90 | + - The code then formats the range of the **Amount** column as Euros to the second decimal. |
| 91 | + - Finally, it ensures that the width of the columns and height of the rows is big enough to fit the longest (or tallest) data item. Notice that the code must get `Range` objects to format. `TableColumn` and `TableRow` objects do not have format properties. |
| 92 | + |
| 93 | + ```js |
| 94 | + expensesTable.columns.getItemAt(3).getRange().numberFormat = [['€#,##0.00']]; |
| 95 | + expensesTable.getRange().format.autofitColumns(); |
| 96 | + expensesTable.getRange().format.autofitRows(); |
| 97 | + ``` |
| 98 | + |
| 99 | +## Test the add-in |
| 100 | + |
| 101 | +1. Open a Git bash window, or Node.JS-enabled system prompt, and navigate to the **Start** folder of the project. |
| 102 | +2. Run the command `npm run build` to transpile your ES6 source code to an earlier version of JavaScript that is supported by Internet Explorer (which is used under-the-hood by Excel to run Excel add-ins). |
| 103 | +3. Run the command `npm start` to start a web server running on localhost. |
| 104 | +4. Sideload the add-in by using one of the following methods: |
| 105 | + - Windows: [Sideload Office Add-ins on Windows](../testing/create-a-network-shared-folder-catalog-for-task-pane-and-content-add-ins.md) |
| 106 | + - Excel Online: [Sideload Office Add-ins in Office Online](../testing/sideload-office-add-ins-for-testing.md#sideload-an-office-add-in-on-office-online) |
| 107 | + - iPad and Mac: [Sideload Office Add-ins on iPad and Mac](../testing/sideload-an-office-add-in-on-ipad-and-mac.md) |
| 108 | +5. On the **Home** menu, choose **Show Taskpane**. |
| 109 | +6. In the taskpane, choose **Create Table**. |
| 110 | + |
| 111 | +  |
0 commit comments