UNPKG

10.9 kBMarkdownView Raw
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
5The 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 that are applicable to the Office application that is running the add-in.
6
7<br />
8
9## About the NPM package
10
11The 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
13While the Office.js CDN contains all currently available Office.js APIs at any moment in time, each version of the NPM package for Office.js contains only the Office.js APIs that were available at the point in time when that version of the NPM package was created.
14
15### Target scenarios
16
17The NPM package for Office.js is intended as a way for you to obtain an offline copy (non-CDN) of the Office.js files, which you can then statically serve from your own site instead of using the CDN. This NPM package is primarily provided to address the following scenarios.
18
191. If you are developing an add-in behind a firewall, where accessing the Office.js CDN is not possible.
202. If you need offline access to the Office.js APIs (for example, to facilitate offline debugging).
21
22**Important**: If you plan to publish your Office Add-in from AppSource, you must use the CDN reference. Offline Office.js references are only appropriate for internal, development, and debugging scenarios.
23
24### Best practices
25
26Best practices for using the Office.js NPM package include:
27
28- Refresh your NPM package periodically (to gain access to new APIs and/or bug fixes that may not have been available in your current version of the package).
29
30- Use the NPM package according to the instructions in [Using the NPM package](#using-the-npm-package); do not try to import the NPM package as you might commonly do with other NPM packages.
31
32- Do not use the NPM package in an add-in that you submit 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.
33
34- Use TypeScript definitions for Office.js as described in [IntelliSense definitions](#intellisense-definitions).
35
36### Support
37
38The Office.js CDN is the official, supported source for Office Add-ins. For the NPM package sourced through this repository, only the latest version of the package is supported. We'll not support nor apply any patches to previous versions of the package. In addition, the frequency of the updates to this repository and related NPM package to match the CDN version is not guaranteed.
39
40Also, Outlook add-ins do not support hosting Office.js offline due to a network access requirement for dependencies like the Microsoft Ajax library.
41
42<br />
43
44## Installing the NPM package
45
46To install "office-js" locally via the NPM package, run the following command:
47
48 npm install @microsoft/office-js --save
49
50<br />
51
52Our policy requires that developers always reference the latest version of Office.js library. This is done 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.
53
54## Using the NPM package
55
56Installing 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:
57
581. 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).
59
602. Reference that location in a `<script>` tag within the HTML file in your add-in project.
61
62For 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:
63
64 <script src="/assets/office-js/office.js"></script>
65
66<br />
67
68## IntelliSense definitions
69
70TypeScript definitions for Office.js are available.
71
72* For latest **RELEASE** version of Office.js:
73 * DefinitelyTyped: <https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/office-js/index.d.ts>
74 * @types: `@types/office-js`. (Acquire as `npm install @types/office-js --save-dev`)
75
76* For **any version** (including **RELEASE**, but also including **BETA**, etc.):
77 * Inside of the NPM package, under `dist/office.d.ts`
78 * In this repo: [dist/office.d.ts](dist/office.d.ts)
79
80### Using TypeScript definitions with the NPM package
81
821. If you are 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. You can achieve this by using a [triple-slash reference](https://www.typescriptlang.org/docs/handbook/triple-slash-directives.html).
83
84 - **Tip**: If you create a `references.ts` file at the root of the project, you can simply point the reference to `office.d.ts` there.
85
862. If you are using the Office.js NPM package for **beta**, follow the guidance outlined in the preceding point (#1), but make sure to update often.
87
88If neither of these points applies to your scenario, you can just 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> -- in which case, you don't need to use the Office.js NPM package.
89
90### Enabling IntelliSense in Visual Studio
91
92Visual 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.
93
94<br />
95
96## Accessing the NPM files via a CDN
97
98In addition to downloading the files locally, you can also use them 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 alpha or beta builds. To do so, simply change the script reference to:
99
100 <script src="https://unpkg.com/@microsoft/office-js/dist/office.js"></script>
101
102You can see the different versions of the NPM package listed in the dropdown on the top right at <https://unpkg.com/@microsoft/office-js/>. This provides the alpha and beta versions as well.
103
104To view the latest version numbers for each of the tags, you can also run the following command on the command-line:
105
106 npm view @microsoft/office-js dist-tags --json
107
108When you have a version number, can use it as follows with <https://unpkg.com>: (appending `@<version-#>` right after `office-js`; e.g., `.../office-js@1.1.2-alpha.0/dist/...`
109
110 <script src="https://unpkg.com/@microsoft/office-js@1.1.2-alpha.0/dist/office.js"></script>
111
112
113<br />
114
115## Production vs. Beta vs. Private versions
116
117Office.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 large 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).
118
119The NPM package and the repo branches assume the following structure.
120
121| GitHub branch name | NPM tag name | Description |
122| ------------------ |--------------|-------------|
123| `release` | `release` (and also `latest`, [a default NPM tag](https://docs.npmjs.com/getting-started/using-tags)) | The latest of the released publicly-available APIs. <br/> This should be identical with what is currently on <https://appsforoffice.microsoft.com/lib/1/hosted/office.js> |
124| `beta` | `beta` | Forthcoming APIs, not necessarily ready for public consumption yet (and may still change...), but likely available on [Insider Fast (and maybe Insider Slow) builds](https://products.office.com/office-insider). <br/> This should be identical to what is currently on <https://appsforoffice.microsoft.com/lib/beta/hosted/office.js> |
125| `release-next` | `release-next` | A forthcoming update the the "release" branch (typically a couple weeks ahead of "release") |
126| `beta-next` | `beta-next` | A forthcoming update the the "beta" branch (typically a couple weeks ahead of "beta") |
127| `private` | `private` | Any flavor of a release, but deployed for a very specific need (e.g., try out something experimental) or for a specific partner. Unlike the other tags, successive versions of this tag are not necessarily cumulative updates; it is possible to have a `1.1.2-private.1` that has the beta JS, and then a `1.1.2-private.2` that only contains the publicly-available release APIs (with maybe some tweaks) |
128
129
130<br />
131
132## Using a Private or Beta version with [Script Lab](https://aka.ms/script-lab)
133
134To 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].
135
136For example, to use a `1.1.2-beta-next.0` version, use the following references:
137
138 @microsoft/office-js@1.1.2-beta-next.0/dist/office.js
139 @microsoft/office-js@1.1.2-beta-next.0/dist/office.d.ts
140
141
142![Using the NPM package with Script Lab](https://github.com/OfficeDev/office-js/blob/release/.github/images/script-lab-substitute-refs.png)
143
144<br />
145
146## More info
147
148For more information on Office Add-ins and the Office JavaScript APIs, see:
149
150- [Office Add-ins platform overview](https://docs.microsoft.com/office/dev/add-ins/overview/office-add-ins)
151- [JavaScript API for Office reference](https://docs.microsoft.com/javascript/api/overview/office?view=office-js)
152- [Learn about the Microsoft 365 Developer Program](https://developer.microsoft.com/microsoft-365/dev-program)