TypeScript版本的Office Addins文件不起作用 [英] Office Addins file in its TypeScript version doesn't work

查看:198
本文介绍了TypeScript版本的Office Addins文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用在 https://github.com中找到的模板/OfficeDev/Excel-Add-In-TS-Start

基本上,它是Visual Studio生成的默认Excel Office Addins模板,但已修改为还支持TypeScript.

Basically, it is the default Excel Office Addins template that Visual Studio generates, but modified to also support TypeScript.

根目录中的文件Home.ts已经具有其TypeScript版本.

The file Home.ts in the root is already in its TypeScript version.

还有一个名为FunctionFile.js的文件(在Functions文件夹中),我想在那里写自己的函数,但是要用TypeScript. 因此,我创建了ExecuteFunction类型的功能区控件,该控件指向名为myTest的函数.

There is also a file called FunctionFile.js (inside Functions folder), and I wanted to write my own functions there, but in TypeScript. So, I created a ribbon control of ExecuteFunction type pointing to a function called myTest.

FunctionFile.js内部,我编写了下面的代码,当我单击addin命令时,它可以按预期工作:

Inside FunctionFile.js, I wrote the code below, and it worked as expected when I clicked in the addin command:

(function () {
    Office.initialize = function (reason) {
    };
})();

function myTest() {
    Excel.run(function (ctx) {
        ctx.workbook.worksheets.getActiveWorksheet().getRange("A1").values = [["I was clicked!"]];

        return ctx.sync();
    }).catch(errorHandler);
}

function errorHandler(error) {
    console.log(error.message);
}

到目前为止,太好了.但是,正如我提到的那样,我希望所有内容都可以在TypeScript中工作,因此我尝试将其重命名为FunctionFile.ts并将代码替换为其TypeScript版本:

So far, so good. But, as I mentioned, I want everything to work in TypeScript, so I tried renaming it to FunctionFile.ts and replaced the code by its TypeScript version:

(() => {
    Office.initialize = () => {
    }
})();

function myTest(): void {
    Excel.run(async (ctx) => {
        ctx.workbook.worksheets.getActiveWorksheet().getRange("A1").values = [["I as clicked!"]];

        await ctx.sync();
    }).catch(errorHandler);
}

function errorHandler(error): void {
    console.log(error);
}

当我尝试运行时,函数myTest被调用,但是当它到达(我认为)Excel.run方法时,我得到了这个错误:

When I tried to run, the function myTest is called, but I got this error when it reaches (I think) Excel.run method:

HTML1300: Ocorreu navegação.
FunctionFile.html
ReferenceError: 'Promise' is not defined (I translated this)
{
[functions]: ,
__proto__: { },
description: "'Promise' não está definido",
message: "'Promise' não está definido",
name: "ReferenceError",
number: -2146823279,
stack: "ReferenceError: 'Promise' não está definido
at Anonymous function (https://localhost:44356/Functions/FunctionFile.js:2:5)
at Anonymous function (https://localhost:44356/Functions/FunctionFile.js:42:32)
at Anonymous function (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.js:21:205600)
at yi (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.js:21:236067)
at st (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.js:21:236154)
at d (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.js:21:235974)
at c (https://appsforoffice.microsoft.com/lib/1/hosted/excel-win32-16.01.js:21:234560)"
}

实际上,该错误发生在下面的JavaScript层中(太混乱而无法分析):

Actually, the error happens in the JavaScript layer below (that it is too messed up to analyze):

return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
})

为什么当我将Home.js转换为Home.ts时,它可以工作,但是FunctionFile.js转换为FunctionFile.ts时却没有发生?我做错什么了吗?

Why when I convert Home.js to Home.ts it works, but the same doesn't happen with FunctionFile.js to FunctionFile.ts? Did I do something wrong?

推荐答案

使用async/await关键字时,TypeScript希望您提供Promise实现.

When you use the async/await keywords, TypeScript expects you to provide a Promise implementation.

摘自《使用Office.js构建Office外接程序》一书( https://leanpub.com/buildingofficeaddins /)[免责声明,我是上述图书的作者]:

From the book "Building Office Add-ins using Office.js" (https://leanpub.com/buildingofficeaddins/) [Disclaimer, I am the author of said book]:

在您的情况下(以及我将在书中添加说明的内容),async关键字有效地为您创建了Promise,因此上述指导仍然适用.在脚本实验室中,我们默认包含core-js库(选项#3),该库提供了Promise polyfill :

In your case (and something that I'll add a clarification for in the book), the async keyword is effectively creating a Promise for you, so the guidance above still applies. In Script Lab, we include the library core-js by default (option #3), which provides a Promise polyfill:

如果您不希望引入额外的库,则还可以依靠Office.js(只要您使用的是ExcelApi 1.2+或WordApi 1.2+)就包括Promise库这一事实.因此,如果需要,您需要做的就是在Office.initialized = function() { ... }内添加以下一线,这将用Office.js提供的一个将全局window.Promise填充:

If you prefer not to bring in an extra library, you can also rely on the fact that Office.js (as long as you're using ExcelApi 1.2+ or WordApi 1.2+) includes a Promise library. So all you need to do, if you want, is add the following one-liner inside of Office.initialized = function() { ... }, which will polyfill the global window.Promise with the Office.js-provided-one:

window.Promise = OfficeExtension.Promise;

希望这会有所帮助!

这篇关于TypeScript版本的Office Addins文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆