javascript - Office 加载项 : Cannot clean Word document after inserting ooxml (Office Online, 适用于桌面)

标签 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 The action isn’t supported in Word Online. Check the OfficeExtension.Error.debugInfo for more information.

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

这会是我的 ooxml 吗?

最佳答案

没有看到 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');
});

关于javascript - Office 加载项 : Cannot clean Word document after inserting ooxml (Office Online, 适用于桌面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48833629/

相关文章:

javascript - jQuery 导航菜单插件推荐

javascript - 使用随机背景颜色更新多个类

javascript - 如何定位在 php 中没有任何 ID 或标记的 <span> 元素的 innerHTML?

excel - 在 excel 中从另一个插件启动一个插件

office365 - Office-ui-fabric-core 某些图标未显示

javascript - 如何通过三元运算符使用多个条件

php - 从 MS Word 粘贴时 PHP 和撇号的编码问题

vba - 调用应用程序时对象错误

html - 从 Word 复制到文本区域,保持格式

javascript - Excel-JS - 重新计算不起作用