|
| 1 | +--- |
| 2 | +page_type: sample |
| 3 | +products: |
| 4 | +- office-excel |
| 5 | +- office-365 |
| 6 | +languages: |
| 7 | +- typescript |
| 8 | +extensions: |
| 9 | + contentType: samples |
| 10 | + technologies: |
| 11 | + - Add-ins |
| 12 | + createdDate: 3/15/2020 1:25:00 PM |
| 13 | +description: "This sample shows how to share data across the ribbon, task pane, and custom functions." |
| 14 | +--- |
| 15 | + |
| 16 | +# (Preview) Share global data with a shared runtime |
| 17 | + |
| 18 | +## Summary |
| 19 | + |
| 20 | +This sample shows how to set up a basic project that uses the shared runtime. The shared runtime runs all parts of the Excel add-in (ribbon buttons, task pane, custom functions) in a single browser runtime. This makes it easy to shared data through local storage, or through global variables. |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +> **Note:** The features used in this sample are currently in preview and subject to change. They are not currently supported for use in production environments. To try the preview features, you'll need to [join Office Insider](https://insider.office.com/join). A good way to try out preview features is to sign up for an Office 365 subscription. If you don't already have an Office 365 subscription, get one by joining the [Office 365 Developer Program](https://developer.microsoft.com/office/dev-program). |
| 25 | +
|
| 26 | +## Features |
| 27 | + |
| 28 | +- Share data globally with ribbon buttons, the task pane, and custom functions. |
| 29 | +- To get started, use a provided manifest XML file to create a new project with a shared runtime. |
| 30 | + |
| 31 | +## Applies to |
| 32 | + |
| 33 | +- Excel on Windows, Mac, and in a browser. |
| 34 | + |
| 35 | +## Prerequisites |
| 36 | + |
| 37 | +To use this sample, you'll need to [join Office Insider](https://insider.office.com/join). |
| 38 | + |
| 39 | +Before running this sample, you need a recent version of [npm](https://www.npmjs.com/get-npm) and [Node.js](https://nodejs.org/en/) installed on your computer. To verify if you've already installed these tools, run the commands `node -v` and `npm -v` in your terminal. |
| 40 | + |
| 41 | +## Solution |
| 42 | + |
| 43 | +Solution | Author(s) |
| 44 | +---------|---------- |
| 45 | +Office Add-in share global data with a shared runtime | Microsoft |
| 46 | + |
| 47 | +## Version history |
| 48 | + |
| 49 | +Version | Date | Comments |
| 50 | +---------| -----| -------- |
| 51 | +1.0 | 3-15-2020 | Initial release |
| 52 | + |
| 53 | +## Disclaimer |
| 54 | + |
| 55 | +**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.** |
| 56 | + |
| 57 | +---------- |
| 58 | + |
| 59 | +## Scenario: Sharing key/value pairs |
| 60 | + |
| 61 | +This sample enables a user to store and retrieve key/value pairs by using the task pane or custom functions. The user can select which type of storage is used. They can choose to store key/value pairs in local storage, or choose to use a global variable. |
| 62 | + |
| 63 | +## Build and run the solution |
| 64 | + |
| 65 | +1. In the command prompt, run the command `npm install`. |
| 66 | +2. Run the command `npm run start`. This starts the node server, and opens Excel on the desktop. |
| 67 | + |
| 68 | +If you're running Excel on the web or Mac, see the following articles for instructions on how to sideload: |
| 69 | + |
| 70 | +- [Sideload Office Add-ins in Office on the web for testing](https://docs.microsoft.com/office/dev/add-ins/testing/sideload-office-add-ins-for-testing) |
| 71 | +- [Sideload Office Add-ins on iPad and Mac for testing](https://docs.microsoft.com/office/dev/add-ins/testing/sideload-an-office-add-in-on-ipad-and-mac) |
| 72 | + |
| 73 | +Once the add-in is loaded use the following steps to try out the functionality. |
| 74 | + |
| 75 | +1. On the `Home` tab, choose `Show TaskPane`. |
| 76 | +2. In the task pane, enter a key/value pair, and choose `Store key/value pair`. |
| 77 | + |
| 78 | +3. In any spreadsheet cell, enter the formula `=CONTOSO.GETVALUEFORKEYCF("1")`. Pass the value of the key you created from the task pane. |
| 79 | +4. In any spreadsheet cell, enter the formula `=CONTOSO.SETVALUEFORKEYCF("2","oranges")`. The formula should return the text `Stored key/value pair`. |
| 80 | +5. In the task pane, enter the key from the previous formula `2` and choose `Get value for key`. The task pane should display the value `oranges`. |
| 81 | + |
| 82 | +The task pane and custom function share data via a global variable in the shared runtime. You can switch the method of storage by choosing either the `Global variable` or `Local storage` radio buttons on the task pane. |
| 83 | + |
| 84 | +## Key parts of this sample |
| 85 | + |
| 86 | +The manifest.xml is configured to use the shared runtime by using the `Runtimes` element as follows: |
| 87 | + |
| 88 | +```xml |
| 89 | +<Runtimes> |
| 90 | + <Runtime resid="Shared.Url" lifetime="long" /> |
| 91 | +</Runtimes> |
| 92 | +``` |
| 93 | + |
| 94 | +In other parts of the manifest, you'll see that the custom functions and task pane are also configured to use the `Shared.Url` because they all run in the same runtime. `Shared.Url` points to `taskpane.html` which loads the shared runtime. |
| 95 | + |
| 96 | +Global state is tracked in a window object retrieved using a `getGlobal()` function. This is accessible to custom functions, the task pane, and the ribbon (because all the code is running in the same JavaScript runtime.) |
| 97 | + |
| 98 | +There are no commands.html or functions.html files. These are not necessary because their purpose is to load individual runtimes. These do not apply when using the shared runtime. |
| 99 | + |
| 100 | +## Security notes |
| 101 | + |
| 102 | +In the webpack.config.js file, a header is set to `"Access-Control-Allow-Origin": "*"`. This is only for development purposes. In production code, you should list the allowed domains and not leave this header open to all domains. |
| 103 | + |
| 104 | +You'll be prompted to install certificates for trusted access to https://localhost. The certificates are intended only for running and studying this code sample. Do not reuse them in your own code solutions or in production environments. |
| 105 | + |
| 106 | +You can install or uninstall the certificates by running the following commands in the project folder. |
| 107 | + |
| 108 | +``` |
| 109 | +npx office-addin-dev-certs install |
| 110 | +npx office-addin-dev-certs uninstall |
| 111 | +``` |
| 112 | + |
| 113 | +## Copyright |
| 114 | + |
| 115 | +Copyright (c) 2020 Microsoft Corporation. All rights reserved. |
| 116 | + |
| 117 | +This project has adopted the [Microsoft Open Source Code of Conduct ](https://opensource.microsoft.com/codeofconduct/). For more information, see the [Code of Conduct FAQ ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments. |
| 118 | + |
| 119 | +<img src="https://telemetry.sharepointpnp.com/officedev/samples/excel-shared-runtime-global-state" /> |
0 commit comments