Word 加载项概述Word add-ins overview

要创建解决方案来扩展 Word 功能?例如,涉及自动文档程序集的解决方案?或从其他数据源绑定到并访问 Word 文档中数据的解决方案?可以使用 Office 加载项平台,其中包含 Word JavaScript API 和适用于 Office 的 JavaScript API,可用于扩展在 Windows 桌面设备、Mac 或云中运行的 Word 客户端。Do you want to create a solution that extends the functionality of Word? For example, one that involves automated document assembly? Or a solution that binds to and accesses data in a Word document from other data sources? You can use the Office Add-ins platform, which includes the Word JavaScript API and the JavaScript API for Office, to extend Word clients running on a Windows desktop, on a Mac, or in the cloud.

Word 外接程序是 Office 外接程序平台上众多开发选项中的一项。外接程序命令可用于扩展 Word 用户界面并启动运行 JavaScript 并与 Word 文档中内容交互的任务窗格。在浏览器中可以运行的任何代码均可在 Word 外接程序中运行。与 Word 文档内容进行交互的外接程序可创建作用于 Word 对象的请求并同步对象状态。Word add-ins are one of the many development options that you have on the Office Add-ins platform. You can use add-in commands to extend the Word UI and launch task panes that run JavaScript that interacts with the content in a Word document. Any code that you can run in a browser can run in a Word add-in. Add-ins that interact with content in a Word document create requests to act on Word objects and synchronize object state.

备注

生成加载项时,如果计划将加载项发布到 AppSource,请务必遵循 AppSource 验证策略。例如,加载项必须适用于支持已定义方法的所有平台,才能通过验证(有关详细信息,请参阅第 4.12 部分以及 Office 加载项主机和可用性页面)。When you build your add-in, if you plan to publish your add-in to AppSource, make sure that you conform to the AppSource validation policies. For example, to pass validation, your add-in must work across all platforms that support the methods that you define (for more information, see section 4.12 and the Office Add-in host and availability page).

下图中的示例展示了在任务窗格中运行的 Word 加载项。The following figure shows an example of a Word add-in that runs in a task pane.

图 1:在 Word 的任务窗格中运行的加载项Figure 1. Add-in running in a task pane in Word

在 Word 的任务窗格中运行的外接程序

Word 外接程序 (1) 可以将请求发送到 Word 文档 (2) 可以使用 JavaScript 来访问段落对象和更新、删除或移动段落。例如,下面的代码演示如何将一个新句子附加到该段落。The Word add-in (1) can send requests to the Word document (2) and can use JavaScript to access the paragraph object and update, delete, or move the paragraph. For example, the following code shows how to append a new sentence to that paragraph.

Word.run(function (context) {
    var paragraphs = context.document.getSelection().paragraphs;
    paragraphs.load();
    return context.sync().then(function () {
        paragraphs.items[0].insertText(' New sentence in the paragraph.',
                                       Word.InsertLocation.end);
    }).then(context.sync);
});

若要托管 Word 加载项,可以使用任何 Web 服务器技术(如 ASP.NET、NodeJS 或 Python)。可以使用常用的客户端框架(Ember、Backbone、Angular、React),也可以坚持使用 VanillaJS 开发解决方案,并能使用 Azure 等服务验证和托管应用。You can use any web server technology to host your Word add-in, such as ASP.NET, NodeJS, or Python. Use your favorite client-side framework -- Ember, Backbone, Angular, React -- or stick with VanillaJS to develop your solution, and you can use services like Azure to authenticate and host your application.

通过 Word JavaScript API 可使应用程序访问 Word 文档中的对象和元数据。这些 API 可用于创建面向以下应用程序的外接程序:The Word JavaScript APIs give your application access to the objects and metadata found in a Word document. You can use these APIs to create add-ins that target:

  • Windows 版 Word 2013 或更高版本Word 2013 or later on Windows
  • Word 网页版Outlook on the web
  • Mac 版 Word 2016 或更高版本Word 2016 or later for Mac
  • iPad 版 WordWord on iPad

外接程序只需编写一次,即可跨多个平台在所有版本 Word 中运行。有关详细信息,请参阅 Office 外接程序主机和平台可用性Write your add-in once, and it will run in all versions of Word across multiple platforms. For details, see Office Add-in host and platform availability.

适用于 Word 的 JavaScript APIJavaScript APIs for Word

有两组 JavaScript API 可用于与 Word 文档中的对象和元数据进行交互。You can use two sets of JavaScript APIs to interact with the objects and metadata in a Word document. 第一组是在 Office 2013 中引入的通用 APIThe first is the Common API, which was introduced in Office 2013. 通用 API 中的许多对象可以在由两个或多个 Office 客户端托管的加载项中使用。Many of the objects in the Common API can be used in add-ins hosted by two or more Office clients. 此 API 广泛使用回调。This API uses callbacks extensively.

第二组是 Word JavaScript API。这是强类型对象模型,可用于创建面向 Mac 和 Windows 版的 Word 2016 的 Word 加载项。此对象模型使用承诺模式,并提供对特定于 Word 对象(如正文内容控件内联图片段落)的访问权限。Word JavaScript API 包括 TypeScript 定义和 vsdoc 文件,这样,你可以在 IDE 中获得代码提示。The second is the Word JavaScript API. This is a strongly-typed object model that you can use to create Word add-ins that target Word 2016 for Mac and Windows. This object model uses promises, and provides access to Word-specific objects like body, content controls, inline pictures, and paragraphs. The Word JavaScript API includes TypeScript definitions and vsdoc files so that you can get code hints in your IDE.

目前,所有 Word 客户端均支持共享的适用于 Office 的 JavaScript API,大多数客户端支持 Word JavaScript API。有关受支持的客户端的详细信息,请参阅 API 参考文档Currently, all Word clients support the shared JavaScript API for Office, and most clients support the Word JavaScript API. For details about supported clients, see the API reference documentation.

我们建议从 Word JavaScript API 开始,因为对象模型更易于使用。如果需要执行以下操作,请使用 Word JavaScript API:We recommend that you start with the Word JavaScript API because the object model is easier to use. Use the Word JavaScript API if you need to:

  • 访问 Word 文档中的对象。Access the objects in a Word document.

在需要执行以下操作时,使用共享的适用于 Office 的 JavaScript API:Use the shared JavaScript API for Office when you need to:

  • 面向 Word 2013。Target Word 2013.
  • 执行应用程序的初始操作。Perform initial actions for the application.
  • 检查支持的要求集。Check the supported requirement set.
  • 访问文档的元数据、设置和环境信息。Access metadata, settings, and environmental information for the document.
  • 绑定到文档中的部分并捕获事件。Bind to sections in a document and capture events.
  • 使用自定义 XML 部件。Use custom XML parts.
  • 打开一个对话框。Open a dialog box.

后续步骤Next steps

准备好创建首个 Word 加载项了吗?请参阅构建首个 Word 加载项。请使用加载项清单描述加载项的托管位置和显示方式,并定义权限和其他信息。Ready to create your first Word add-in? See Build your first Word add-in. You can also try our interactive Get started experience. Use the add-in manifest to describe where your add-in is hosted, how it is displayed, and define permissions and other information.

若要了解如何设计世界一流的 Word 外接程序来为用户打造具有吸引力的体验,请参阅设计指南最佳实践To learn more about how to design a world class Word add-in that creates a compelling experience for your users, see Design guidelines and Best practices.

开发加载项后,可以将它发布到网络共享、应用目录或 AppSource。After you develop your add-in, you can publish it to a network share, an app catalog, or AppSource.

另请参阅See also