Cannot find name 'Office'

天涯浪子 提交于 2019-12-06 04:20:34

It looks like you still need to specify that the office-js types should be used. Open up src/tsconfig.app.json and add office-js to the types array, which should then look like this if it was previously an empty array:

"types": [
  "office-js"
]

Next, you need to run the command tsc -p tsconfig.json from within your project directory before attempting to build the project again.

The following works perfectly (note the package names)

npm install --save office-js @types/office-js

Then consume it as the global it declares.

Office.initialize(0);

Note that office-js is not a module, it is a global. Therefore we do not import from it. Rather the package becomes available ambiently.

If we wish to use import as a means to load office-js at runtime, we can add

import 'office-js';

In which case we are also not importing anything from it since there is nothing to import but we are stating a dependency on its execution, a side-effect of which is to create the global variable window.Office.

Note in your comments you mention an @microsoft/office-js package. At the time of this writing such a package exists but is completely empty, its entry point specifying a file that does not even exist, so I doubt that is the package you intend to use.

On an unrelated note, consider specifying your module format explicitly in your tsconfig.json file:

{
  "compilerOptions": {
    "module": "commonjs"
  }
}

It is a most important setting.

For now, Office.js is consumed not as an NPM package, but as a CDN reference. It's typings/d.ts do come from @types, but not its actual code.

I recommend you take a look at Script Lab, a recently-announced tool and also open-source showcase sample, that uses Angular 4, and which you can use for inspiration. Some of the underlying technology is also discussed in a podcast about the project.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!