我正在使用这个,但是当我使用断点时,这会给出错误
TypeError: Cannot read properties of undefined (reading 'isNullObject')
,那么这会给出selection.parentSectionOrNullObject
未定义。
async function getSectionRange() {
try {
await Word.run(async (context) => {
const selection = context.document.getSelection();
selection.load('parentSectionOrNullObject');
await context.sync();
if (!selection.parentSectionOrNullObject.isNullObject) {
const parentSection = selection.parentSectionOrNullObject;
const sectionRange = parentSection.getRange("Whole");
context.load(sectionRange, 'text');
await context.sync();
console.log('Section Range Text:', sectionRange.text);
} else {
console.log('The selection is not within a section.');
}
});
} catch (error) {
console.error('Error: ', error);
}
}
您的
selection
变量是一个 Range 对象。 Range 没有parentSectionOrNullObject 属性。该属性只能在 Body 对象上找到。正如我在回答您的其他问题时所解释的:如何使用 Office javascript api 获取 Word.Section Range?,您需要首先获取 selection
的父级 body。