Skip to content

Commit 569b8ee

Browse files
Office Add-in share global data with a shared runtime (#65)
* yo office creation of new sample * store and retrieve values working * custom function retrieves global state * added CF for set key/value pair * local storage working * task pane working * small fix * remove unneeded html runtime starters * added code comments * remove unneeded files * add readme steps and images * edit fix * Update Samples/excel-shared-runtime-global-state/README.md Co-Authored-By: Linda Lu Cannon <[email protected]> * Apply suggestions from code review edit feedback Co-Authored-By: Linda Lu Cannon <[email protected]> * Update Samples/excel-shared-runtime-global-state/README.md Co-Authored-By: Linda Lu Cannon <[email protected]> * Update README.md Co-authored-by: Linda Lu Cannon <[email protected]>
1 parent 1ab3822 commit 569b8ee

22 files changed

+10306
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": [
4+
"@typescript-eslint"
5+
],
6+
"parserOptions": {
7+
"ecmaVersion": 6,
8+
"sourceType": "module",
9+
"ecmaFeatures": {
10+
"jsx": true
11+
},
12+
"project": "./tsconfig.json"
13+
},
14+
"extends": [
15+
"eslint-config-office-addins"
16+
]
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
9+
# Diagnostic reports (https://nodejs.org/api/report.html)
10+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
11+
12+
# Runtime data
13+
pids
14+
*.pid
15+
*.seed
16+
*.pid.lock
17+
18+
# Directory for instrumented libs generated by jscoverage/JSCover
19+
lib-cov
20+
21+
# Coverage directory used by tools like istanbul
22+
coverage
23+
*.lcov
24+
25+
# nyc test coverage
26+
.nyc_output
27+
28+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
29+
.grunt
30+
31+
# Bower dependency directory (https://bower.io/)
32+
bower_components
33+
34+
# node-waf configuration
35+
.lock-wscript
36+
37+
# Compiled binary addons (https://nodejs.org/api/addons.html)
38+
build/Release
39+
40+
# Dependency directories
41+
node_modules/
42+
jspm_packages/
43+
44+
# Snowpack dependency directory (https://snowpack.dev/)
45+
web_modules/
46+
47+
# TypeScript cache
48+
*.tsbuildinfo
49+
50+
# Optional npm cache directory
51+
.npm
52+
53+
# Optional eslint cache
54+
.eslintcache
55+
56+
# Microbundle cache
57+
.rpt2_cache/
58+
.rts2_cache_cjs/
59+
.rts2_cache_es/
60+
.rts2_cache_umd/
61+
62+
# Optional REPL history
63+
.node_repl_history
64+
65+
# Output of 'npm pack'
66+
*.tgz
67+
68+
# Yarn Integrity file
69+
.yarn-integrity
70+
71+
# dotenv environment variables file
72+
.env
73+
.env.test
74+
75+
# parcel-bundler cache (https://parceljs.org/)
76+
.cache
77+
.parcel-cache
78+
79+
# Next.js build output
80+
.next
81+
82+
# Nuxt.js build / generate output
83+
.nuxt
84+
dist
85+
86+
# Gatsby files
87+
.cache/
88+
# Comment in the public line in if your project uses Gatsby and not Next.js
89+
# https://nextjs.org/blog/next-9-1#public-directory-support
90+
# public
91+
92+
# vuepress build output
93+
.vuepress/dist
94+
95+
# Serverless directories
96+
.serverless/
97+
98+
# FuseBox cache
99+
.fusebox/
100+
101+
# DynamoDB Local files
102+
.dynamodb/
103+
104+
# TernJS port file
105+
.tern-port
106+
107+
# Stores VSCode versions used for testing VSCode extensions
108+
.vscode-test
109+
110+
# yarn v2
111+
112+
.yarn/cache
113+
.yarn/unplugged
114+
.yarn/build-state.yml
115+
.pnp.*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Contribute to this code sample
2+
3+
Thank you for your interest in this sample! Your contributions and improvements will help the developer community.
4+
5+
## Ways to contribute
6+
7+
There are several ways you can contribute to this sample: providing better code comments, fixing open issues, and adding new features.
8+
9+
### Provide better code comments
10+
11+
Code comments make code samples even better by helping developers learn to use the code correctly in their own applications. If you spot a class, method, or section of code that you think could use better descriptions, then create a pull request with your code comments.
12+
13+
14+
In general we want our code comments to follow these guidelines:
15+
16+
- Any code that has associated documentation displayed in an IDE (such as IntelliSense, or JavaDocs) has code comments.
17+
- Classes, methods, parameters, and return values have clear descriptions.
18+
- Exceptions and errors are documented.
19+
- Remarks exist for anything special or notable about the code.
20+
- Sections of code that have complex algorithms have appropriate comments describing what they do.
21+
- Code added from Stack Overflow, or any other source, is clearly attributed.
22+
23+
### Fix open issues
24+
25+
Sometimes we get a lot of issues, and it can be hard to keep up. If you have a solution to an open issue that hasn't been addressed, fix the issue and then submit a pull request.
26+
27+
### Add a new feature
28+
29+
New features are great! Be sure to check with the repository admin first to be sure the feature will fit the intent of the sample. Start by opening an issue in the repository that proposes and describes the feature. The repository admin will respond and may ask for more information. If the admin agrees to the new feature, create the feature and submit a pull request.
30+
31+
## Contribution guidelines
32+
33+
We have some guidelines to help maintain a healthy repo and code for everyone.
34+
35+
### The Contribution License Agreement
36+
37+
For most contributions, you'll be asked to sign a Contribution License Agreement (CLA). This will happen when you submit a pull request. Microsoft will send a link to the CLA to sign via email. Once you sign the CLA, your pull request can proceed. Read the CLA carefully, because you may need to have your employer sign it.
38+
39+
### Code contribution checklist
40+
41+
Be sure to satisfy all of the requirements in the following list before submitting a pull request:
42+
43+
- Follow the code style that is appropriate for the platform and language in this repo. For example, Android code follows the style conventions found in the [Code Style for Contributors guide](https://source.android.com/source/code-style.html).
44+
- Test your code.
45+
- Test the UI thoroughly to be sure nothing has been broken by your change.
46+
- Keep the size of your code change reasonable. if the repository owner cannot review your code change in 4 hours or less, your pull request may not be reviewed and approved quickly.
47+
- Avoid unnecessary changes. The reviewer will check differences between your code and the original code. Whitespace changes are called out along with your code. Be sure your changes will help improve the content.
48+
49+
### Submit a pull request to the master branch
50+
51+
When you're finished with your work and are ready to have it merged into the master repository, follow these steps. Note: pull requests are typically reviewed within 10 business days. If your pull request is accepted you will be credited for your submission.
52+
53+
1. Submit your pull request against the master branch.
54+
2. Sign the CLA, if you haven't already done so.
55+
3. One of the repo admins will process your pull request, including performing a code review. If there are questions, discussions, or change requests in the pull request, be sure to respond.
56+
4. When the repo admins are satisfied, they will accept and merge the pull request.
57+
58+
Congratulations, you have successfully contributed to the sample!
59+
60+
## FAQ
61+
62+
### Where do I get a Contributor's License Agreement?
63+
64+
You will automatically be sent a notice that you need to sign the Contributor's License Agreement (CLA) if your pull request requires one.
65+
66+
As a community member, you must sign the CLA before you can contribute large submissions to this project. You only need complete and submit the CLA document once. Carefully review the document. You may be required to have your employer sign the document.
67+
68+
### What happens with my contributions?
69+
70+
When you submit your changes, via a pull request, our team will be notified and will review your pull request. You will receive notifications about your pull request from GitHub; you may also be notified by someone from our team if we need more information. We reserve the right to edit your submission for legal, style, clarity, or other issues.
71+
72+
### Who approves pull requests?
73+
74+
The admin of the repository approves pull requests.
75+
76+
### How soon will I get a response about my change request or issue?
77+
78+
We typically review pull requests and respond to issues within 10 business days.
79+
80+
## More resources
81+
82+
- To learn more about Markdown, see [Daring Fireball](http://daringfireball.net/).
83+
- To learn more about using Git and GitHub, check out the [GitHub Help section](http://help.github.com/).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Excel Custom Functions
2+
3+
MIT License
4+
5+
Copyright (c) Microsoft Corporation. All rights reserved.
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
![Screen shot of the add-in with ribbon buttons enabled and disabled](excel-shared-runtime-global.png)
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+
![Screen shot of both key and value input fields, and both store and get buttons.](task-pane-buttons.png)
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" />
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)