我正在使用 Office.js 开发一个 Word 插件,该插件利用了 ContentControl.insertText。根据文档,此方法由WordApi 要求集 1.1 提供,在 Office Online 中受支持。此外,ContentControls 的文档将 Office Online 列为受支持,并且在使用该特定方法时没有提及任何例外情况。
也就是说,当我在本机 Word 2016 中运行该加载项时,一切正常,但当我在 Word Online 环境中运行它时,它会失败并出现以下异常:
code:"NotAllowed"
errorLocation:"ContentControl.insertText"
message:"The action isn’t supported in Word Online. Check the OfficeExtension.Error.debugInfo for more information."
仅供参考,我是这样使用的:
control.insertText(heading.Text, 'Replace');
我还尝试运行文档中提供的示例,得到相同的结果:它在 Word 2016 中有效,但在 Word Online 中失败并出现上述异常。
有人能够在 Office Online 环境中成功将文本插入到 ContentControl 中吗?如果没有,有计划支持此功能吗?
任何意见或澄清将不胜感激!谢谢!
我是微软的一名工程师。在某些情况下,不允许 contentControl.insertText。
我认为你的情况的原因是heading.text中有多个段落,并且你尝试将heading.text插入内联内容控件中。
内联内容控制意味着内容仅控制段落的某些部分。
为了确定您的情况的根本原因,您能否分享一下文档和脚本代码。
我知道这是六年后的事了,但我今天面临着同样的问题, 但出于不同的原因。也许这对某人有帮助。
就我而言,我将 Content Control 设置为不可编辑。 在插入文本之前必须先使其可编辑:
Word.run(async (context) => {
const contentControl = context.document.contentControls.getById(1);
context.load(contentControl, ['id']);
await context.sync();
// enable editing
contentControl.cannotEdit = false;
await context.sync();
// edit the text
contentControl.insertText('Great', Word.InsertLocation.Replace);
await context.sync();
// disable editing again
contentControl.cannotEdit = true;
await context.sync();
});