在第一次插入 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/