Office加载项:插入ooxml后无法清除Word文档(Office Online,适用于桌面)

时间:2018-02-16 19:31:00

标签: javascript ms-word office-js

第一次插入后,尝试替换Word文档的内容。当我运行此代码时:

Word.run(async context => {
  let range = context.document.body.getRange();
  let myContentControl = range.insertContentControl();
  myContentControl.clear();
  myContentControl.insertOoxml(ooxml, Word.InsertLocation.replace);
  myContentControl.cannotEdit = false;
  myContentControl.cannotDelete = false;
  context.load(myContentControl, "id");
  await context.sync();
});

我收到此错误:

  

NotAllowed   Word Online不支持该操作。有关详细信息,请查看OfficeExtension.Error.debugInfo

如果我使用文本然后运行此代码,则插入正常。

这可能是我的ooxml吗?

1 个答案:

答案 0 :(得分:1)

如果没有看到OOXML或OfficeExtension.Error.debugInfo的值,就很难知道问题出在哪里。

那就是说,你在这里做了很多不必要的步骤:

Word.run(async context => {
  let range = context.document.body.getRange();

  // Why insert a content control?
  let myContentControl = range.insertContentControl();

  // Since you just inserted this, there nothing to clear
  myContentControl.clear();

  // Similar, since this is new (and cleared) why "replace"
  myContentControl.insertOoxml(ooxml, Word.InsertLocation.replace);

  // Already the default value
  myContentControl.cannotEdit = false;
  myContentControl.cannotDelete = false;

  // Why load this when you're not doing anything
  // with the value you're loaded
  context.load(myContentControl, "id");

  // Little known tip/trick:
  // You don't need to sync here, Word.run() will automatically 
  // process anything in the queue when it completes
  await context.sync();
});

在尝试诊断这是否是您的OOXML字符串之前,我首先要减少您所做的不必要的通话次数:

Word.run(async context => {
  let range = context.document.body.getRange();
  range.insertOoxml('Your OOXML', 'Replace');
});