1 | [![NPM Deployment Status](https://travis-ci.org/OfficeDev/office-js.svg?branch=release)](https://travis-ci.org/OfficeDev/office-js/builds)
|
2 |
|
3 | # Office JavaScript APIs
|
4 |
|
5 | The JavaScript API for Office enables you to create web applications that interact with the object models in Office host applications. Your application will reference the office.js library, which is a script loader. The office.js library loads the object models applicable to the Office application running the add-in.
|
6 |
|
7 | <br />
|
8 |
|
9 | ## About the NPM package
|
10 |
|
11 | The NPM package for Office.js is a copy of what gets published to the official "evergreen" Office.js CDN, at **<https://appsforoffice.microsoft.com/lib/1/hosted/office.js>**.
|
12 |
|
13 | The Office.js CDN contains all currently available Office.js APIs at any moment in time.
|
14 |
|
15 | Each Office.js NPM package contains only Office.js APIs available on the Office.js CDN when the NPM package version was created.
|
16 |
|
17 | ### Target scenarios
|
18 |
|
19 | The NPM package for Office.js is intended as a way to obtain an offline copy (non-CDN) of the Office.js files, which can then be statically serve from your own site instead of using the CDN.
|
20 |
|
21 | This NPM package is provided for the following scenarios:
|
22 |
|
23 | 1. Development of an Add-in behind a firewall, when accessing the Office.js CDN is not possible.
|
24 | 2. Offline access to the Office.js APIs to facilitate offline debugging.
|
25 |
|
26 | **Important**: Office Add-ins published to AppSource must use the CDN reference. Offline Office.js references are only appropriate for internal development, and debugging scenarios.
|
27 |
|
28 | ### Best practices
|
29 |
|
30 | Best practices for using the Office.js NPM package include:
|
31 |
|
32 | - Refreshing the NPM package periodically to access new APIs and bug fixes.
|
33 |
|
34 | - [Using the NPM package according to the instructions](#use-the-npm-package). Do not import the NPM package as commonly done with other NPM packages.
|
35 |
|
36 | - Do not use the NPM package in an Add-in intended for publication to [AppSource](https://appsource.microsoft.com/marketplace/apps?product=office). Add-ins that are published to AppSource must use the Office.js CDN.
|
37 |
|
38 | - [Using TypeScript definitions for Office.js](#intellisense-definitions).
|
39 |
|
40 | ### Support
|
41 |
|
42 | The Office.js CDN is the official supported source for Office Add-ins.
|
43 |
|
44 | For the NPM package sourced through this repository, only the latest version of the package is supported. No support and no patches will be provided for previous versions of the package. The frequency of the updates to this repository and related NPM package to match the CDN version is not guaranteed.
|
45 |
|
46 | Outlook add-ins do not support hosting Office.js offline due to network access requirements for dependencies like the Microsoft Ajax library.
|
47 |
|
48 | <br />
|
49 |
|
50 | ## Install the NPM package
|
51 |
|
52 | To install "office-js" locally via the NPM package, run the following command:
|
53 |
|
54 | > npm install @microsoft/office-js --save
|
55 |
|
56 | <br />
|
57 |
|
58 | Our policy requires that developers always reference the latest version of Office.js library. This is to ensure that essential product updates are served quickly to the Office Add-ins by always referencing the latest release of the library for a given version, such as Generally Available (GA) version. Given that the latest Office.js release is backward-compatible with prior releases, it is safe to update to the most recent release of the library when one is available. Hence, only the latest version of Office.js NPM package is made available for installation.
|
59 |
|
60 | ## Use the NPM package
|
61 |
|
62 | Installing the NPM package locally creates a set of static Office.js files in the `node_modules\@microsoft\office-js\dist` folder of the directory where you ran the `npm install` command. To use the NPM package, do the following:
|
63 |
|
64 | 1. Either manually or as part of a build script (e.g., `CopyWebpackPlugin` if you're using Webpack) have the files served from a destination of your choosing (e.g., from the `/assets/office-js/` directory of your web server).
|
65 |
|
66 | 2. Reference that location in a `<script>` tag within the HTML file in your add-in project.
|
67 |
|
68 | For example, if you add the contents of the `dist` folder to the `assets/office-js` directory of your project, then you'd add the following `<script>` tag to your HTML file:
|
69 |
|
70 | ```html
|
71 | <script src="/assets/office-js/office.js"></script>
|
72 | ```
|
73 |
|
74 | <br />
|
75 |
|
76 | ## IntelliSense definitions
|
77 |
|
78 | TypeScript definitions for Office.js are available.
|
79 |
|
80 | - latest **RELEASE** version of Office.js:
|
81 | - DefinitelyTyped: <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/office-js/index.d.ts>
|
82 | - @types: `@types/office-js`
|
83 | > npm install @types/office-js --save-dev
|
84 | - latest **BETA** version of Office.js:
|
85 | - DefinitelyTyped: <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/office-js-preview/index.d.ts>
|
86 | - @types: `@types/office-js-preview`.
|
87 | > npm install @types/office-js-preview --save-dev
|
88 | - **any** version:
|
89 | - Inside of the NPM package, under `dist/office.d.ts`
|
90 | - In this repo: [dist/office.d.ts](dist/office.d.ts)
|
91 |
|
92 | ### Use TypeScript definitions with the NPM package
|
93 |
|
94 | If you're using the Office.js NPM package for the [firewall scenario](#target-scenarios) and want a d.ts file that precisely matches the JS contents, use the d.ts file that is located within the `/dist/office.d.ts` folder of the NPM package. Achieve this by using a [triple-slash reference](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html).
|
95 |
|
96 | - **TIP**: If you create a `references.ts` file at the root of the project, simply point the reference to `office.d.ts` there.
|
97 |
|
98 | If you don't need the [firewall scenario](#target-scenarios), don't use the Office.js NPM package, obtain the TypeScript definitions by using `@types/office-js` and reference the Office.js CDN at <https://appsforoffice.microsoft.com/lib/1/hosted/office.js>.
|
99 |
|
100 | ### Enable IntelliSense in Visual Studio
|
101 |
|
102 | Visual Studio 2017+ can use these same TypeScript definitions, even for regular JavaScript. For JavaScript IntelliSense in earlier versions of Visual Studio, an `office-vsdoc.js` is available alongside the `office.js` file. As long as you have a `Scripts/_references.js` file in your VS project, and as long as you substitute the existing triple-slash reference (`/// <reference path="https://.../office.js" />`) with the new location (the `-vsdoc` part gets substituted automatically, so use it just as you would in a `<script src="">` reference), you should have the corresponding JavaScript IntelliSense.
|
103 |
|
104 | ## Access NPM files via a CDN
|
105 |
|
106 | In addition to downloading the files locally, they may be used via an external service like <https://unpkg.com>, which provides best-effort (no uptime guarantees) CDN hosting for npm packages. This is especially useful for trying out custom builds. To do so, simply change the script reference to:
|
107 |
|
108 | ```html
|
109 | <script src="https://unpkg.com/@microsoft/office-js/dist/office.js"></script>
|
110 | ```
|
111 |
|
112 | The different versions of the NPM package are listed on the top right in the dropdown list at <https://unpkg.com/@microsoft/office-js/>.
|
113 |
|
114 | To view the latest version numbers for each of the tags run the following command on the command-line:
|
115 |
|
116 | > npm view @microsoft/office-js dist-tags --json
|
117 |
|
118 | A specific version number, can be used with <https://unpkg.com> by appending `@<version-#>` right after `office-js`; e.g., `.../office-js@1.1.2-custom.0/dist/...`
|
119 |
|
120 | ```html
|
121 | <script src="https://unpkg.com/@microsoft/office-js@1.1.2-custom.0/dist/office.js"></script>
|
122 | ```
|
123 |
|
124 | <br />
|
125 |
|
126 | ## NPM Package Versions
|
127 |
|
128 | Office.js versioning is described in detail in <https://docs.microsoft.com/office/dev/add-ins/develop/referencing-the-javascript-api-for-office-library-from-its-cdn>. Importantly, there is a difference between what is in the JS files, versus what are the capabilities of a particular computer (i.e., older or slower-to-update versions of office).
|
129 |
|
130 | The NPM package and the repo branches assume the following structure.
|
131 |
|
132 | | GitHub branch name | NPM tag name | Description |
|
133 | | ------------------ |-------------- |-------------|
|
134 | | `release` | `latest` | Identical to a previous release on <https://appsforoffice.microsoft.com/lib/1/hosted/office.js> <br/><br/> The latest released publicly-available APIs. |
|
135 | | `beta` | `beta` | Identical to a previous release on <https://appsforoffice.microsoft.com/lib/beta/hosted/office.js> <br/><br/> Forthcoming APIs, not necessarily ready for public consumption that may change. Possibly available on [Insider Fast (and maybe Insider Slow) builds](https://products.office.com/office-insider). |
|
136 | | `custom` | `custom` | A custom release deployed for a specific need. Successive versions of this tag are not cumulative updates (e.e. `1.1.2-custom.1` may contain beta JavaScript, and `1.1.2-custom.2` may only contain the publicly-available release APIs with some tweaks) |
|
137 |
|
138 | ## Use a specific version with [Script Lab](https://aka.ms/script-lab)
|
139 |
|
140 | To use a version of the NPM package with [Script Lab](https://aka.ms/script-lab), substitute the CDN reference and the `@types/office-js` reference with the NPM package name and version. [Note: Script Lab uses <https://unpkg.com> for resolving the package names, so it's very similar guidance as above].
|
141 |
|
142 | For example, to use a `1.1.2-custom.0` version, use the following references:
|
143 |
|
144 | ```text
|
145 | @microsoft/office-js@1.1.2-custom.0/dist/office.js
|
146 | @microsoft/office-js@1.1.2-custom.0/dist/office.d.ts
|
147 | ```
|
148 |
|
149 | ![Use the NPM package with Script Lab](https://github.com/OfficeDev/office-js/blob/release/.github/images/script-lab-substitute-refs.png)
|
150 |
|
151 | ## Code of Conduct
|
152 |
|
153 | 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 [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments.
|
154 |
|
155 | ## More info
|
156 |
|
157 | For more information on Office Add-ins and the Office JavaScript APIs, see:
|
158 |
|
159 | - [Office Add-ins platform overview](https://docs.microsoft.com/office/dev/add-ins/overview/office-add-ins)
|
160 | - [JavaScript API for Office reference](https://docs.microsoft.com/javascript/api/overview/office?view=office-js)
|
161 |
|
162 | ## Join the Microsoft 365 Developer Program
|
163 |
|
164 | Get a free sandbox, tools, and other resources you need to build solutions for the Microsoft 365 platform.
|
165 |
|
166 | - [Free developer sandbox](https://developer.microsoft.com/microsoft-365/dev-program#Subscription) Get a free, renewable 90-day Microsoft 365 E5 developer subscription.
|
167 | - [Sample data packs](https://developer.microsoft.com/microsoft-365/dev-program#Sample) Automatically configure your sandbox by installing user data and content to help you build your solutions.
|
168 | - [Access to experts](https://developer.microsoft.com/microsoft-365/dev-program#Experts) Access community events to learn from Microsoft 365 experts.
|
169 | - [Personalized recommendations](https://developer.microsoft.com/microsoft-365/dev-program#Recommendations) Find developer resources quickly from your personalized dashboard.
|