Office.js绑定在相对平台(Office Online在线与Office Desktop)中未识别



我们正在为Excel和Word开发JavaScript Office加载项。我们的用户将在线使用Office Desktop和Office。

当用户在加载项中创建新记录时,我们会对当前焦点的范围创建一个绑定。绑定正在创建并看起来还不错,但是如果用户在Office桌面中创建绑定,然后在Office在线打开文档,则无法识别绑定。反之亦然,如果在Office Online在Office创建绑定,则不以后被桌面识别。

有更好的方法吗?

在Excel中的示例请参阅下面的代码:

addNote() {
  try {
    Excel.run((ctx) => {
      const selectedRange = ctx.workbook.getSelectedRange();
      selectedRange.load('address');
      return ctx.sync().then(() => {
        const currentAddress = selectedRange.address;
        this.setState({ currentAddress });
        const bindingName = `SymphonyBinding_${newGuid()}`;
        const myBindings = Office.context.document.bindings;
        this.setState({ bindingName });
        myBindings.addFromNamedItemAsync(currentAddress, 'matrix', { id: bindingName },
          (result) => {
            if (result.status === 'succeeded') {
              this.setState({ bindingName: result.value.id });
              meow.raise({ name: 'create-new-note', context: { tags: [result.value.id] } });
            } else {
              this.setState({ bindingName: 'could not bind' });
            }
          });
      });
    });
  } catch (error) {
    handleError('Office Add-In', error, error.message);
  }
}

这是识别绑定的代码:

showNoteRequested({ context }) {
  const { note } = context;
  note.tags.forEach((tag) => {
    if (tag.name.indexOf('SymphonyBinding_') !== -1) {
      this.setState({ bindingName: tag.name }, this.selectRange);
    }
  });
}
selectRange() {
  const { bindingName } = this.state;
  try {
    Excel.run((ctx) => {
      const foundBinding = ctx.workbook.bindings.getItem(bindingName);
      const myRange = foundBinding.getRange();
      myRange.select();
      myRange.load('address');
      return ctx.sync().then(() => {
        const currentAddress = myRange.address;
        this.setState({ currentAddress });
      });
    });
  } catch (error) {
    handleError('Office Add-In', error, error.message);
  }
}

以下绑定的创建代码等于您在AddNode中的内容吗?当文件加载在excel Online Build中16.0.9229.5030中时,对我来说似乎很好。

function run() {
    return Excel.run(function (ctx) {
        const selectedRange = ctx.workbook.getSelectedRange();
        selectedRange.load('address');
        return ctx.sync().then(() => {
            const currentAddress = selectedRange.address;
            const bindingName = 'SymphonyBinding_{493332C3-F66B-41BF-B37E-8E28D045E2F0}';
            const myBindings = Office.context.document.bindings;
            console.log("binding name" + bindingName);
            myBindings.addFromNamedItemAsync(currentAddress, 'matrix', { id: bindingName },
                (result) => {
                    console.log("binding created");
                });
        });
    });
}

最新更新