【问题标题】:How to define Excel using microsoft office-js in Angular 6如何在 Angular 6 中使用 microsoft office-js 定义 Excel
【发布时间】:2018-11-22 06:30:45
【问题描述】:

我正在关注这个site来集成office js

这样声明:

import * as Excel from '@microsoft/office-js/excel' 它显示编译时错误:

找不到模块“@microsoft/office-js/excel”

这样声明:

declare var Excel: any

显示运行时错误:

ERROR ReferenceError: Excel 未定义

请建议如何声明它。我需要这样使用它:

Excel.run(session, (context){
   ...
});

【问题讨论】:

  • 一个可能暂时帮助您(直到您找到更好的解决方案)的技巧是声明“Excel”对象,以便规避类型检查。您可以通过添加带有 office.js 的脚本标记并在 javascript 文件中的导入语句下方添加此 declare const Excel: any; declare const Office: any; 来做到这一点。
  • 已经试过了。没用

标签: angular typescript office-js office-addins office-js-helpers


【解决方案1】:

安装office类型定义:

npm install --save @types/office-js

在 tsconfig.app.json 中添加:

"types": [
    "office-js"
]

在 main.ts 中,您将应用引导为:

    Office.initialize = function () {
    platformBrowserDynamic().bootstrapModule(AppModule);
};

在 index.html 中添加:

<script type="text/javascript" src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js"></script>

之后,您应该能够在您的 Angular 服务中编写类似这样的内容:

  getExtension(): string {
    if (Office.context.host === Office.HostType.Word) {
      return '.docx';
    } else if  (Office.context.host === Office.HostType.Excel) {
      return '.xlsx';
    } else if (Office.context.host === Office.HostType.PowerPoint) {
      return '.pptx';
    } else {
      return null;
    }
  }

在这里你可以找到一个 Angular office-js 项目的示例:

Angular office-js sample project

它是使用 Angular CLI 版本 6.0.8 构建的 Angular 6

【讨论】:

  • 我应该在哪个服务中添加getExtension()
  • 您将其添加到您的角度服务或角度组件中。
  • 这是一个错误:ERROR TypeError: Cannot read property 'host' of undefined
  • 对不起,我以为您已经安装了类型,请参阅改进的答案
  • 是的,不确定问题出在哪里,可能是办公室清单之类的。我创建了新的 Angular 示例项目 github.com/dgrudenic/angular-office-addin 你可以看看那里。
猜你喜欢
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2018-12-09
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 2023-01-18
  • 1970-01-01
相关资源
最近更新 更多