问题描述
背景
我在我的 Word 文档中插入了一堆内容控件。每个内容控件都是一些文本,例如“你好世界”。
我想做什么
当用户将光标置于内容控件内时,我想访问加载项中内容控件的详细信息。
我的尝试
我在启动时运行它。
Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged,() => {
Word.run( async (context) => {
const range = context.document.getSelection();
console.log({range});
const contentControls = range.contentControls;
contentControls.load("items");
await context.sync();
console.log('contentControls.items',contentControls.items)
})
})
问题
如果用户在内容控件中弹出他们的光标,则不会报告“项目”。但是,如果用户突出显示整个内容控件,则会正确报告“项目”。
问题
有没有办法检测用户是否在内容控件内,而无需他们选择整个内容?
解决方法
尝试使用 Range.parentContentControl(或 parentContentControlOrNullObject)获取对包含内容控件的引用。
例如
const parentContentControl = range.parentContentControlOrNullObject;
await context.sync();
if (parentContentControl.isNullObject) {
console.log("The cursor is not in a content control")
}
else {
console.log('parentContentControl',parentContentControl);
}