ExecuteFunction不调用函数

时间:2019-04-26 15:32:03

标签: office-js

我已经使用yo office创建了一个Microsoft Office加载项React(TypeScript)项目。

我将manifest.xml更改为包含

<Control xsi:type="Button" id="Contoso.GrabSelectionButton">
  <Label resid="Contoso.GrabSelectionButton.Label" />
  <Supertip>
    <Title resid="Contoso.GrabSelectionButton.Label" />
    <Description resid="Contoso.GrabSelectionButton.Tooltip" />
  </Supertip>
  <Icon>
    <bt:Image size="16" resid="Contoso.tpicon_16x16" />
    <bt:Image size="32" resid="Contoso.tpicon_32x32" />
    <bt:Image size="80" resid="Contoso.tpicon_80x80" />
  </Icon>

  <Action xsi:type="ExecuteFunction">
    <FunctionName>grabSelection</FunctionName>
  </Action>
</Control>

<FunctionFile resid="Contoso.DesktopFunctionFile.Url" />的设置正确-我没有将其更改为默认模板。

我在function-file.ts中这样添加了方法

(() => {
  Office.initialize = () => { console.log("this gets logged..."); };

  function grabSelection(event){
    console.log("grabSelection invoked! - this does not get logged");

    event.completed();
  }

})();

我也尝试过这样

   (() => {
      Office.initialize = () => { console.log("this gets logged..."); };
    })();

export function grabSelection(event){
  console.log("grabSelection invoked! - this does not get logged");
  event.completed();
}

我可以看到它在运行时已编译并包含在内,但该函数未被调用...

为什么不调用该函数? webpack无法正确导出函数是否存在问题?

1 个答案:

答案 0 :(得分:0)

我设法通过将函数附加到全局名称空间来解决此问题:

(() => {
  Office.initialize = () => {
    console.log("function file loaded...!");
   };
  })();

function grabSelection(event: any): void {
    console.log("i got invoked!!!");
    event.completed();
}

const _global = (window /* browser */ || global /* node */) as any;
_global.grabSelection= grabSelection;