如何在word中获取`parentSectionOrNullObject`范围?使用 Office javascript Api

问题描述 投票:0回答:1

我正在使用这个,但是当我使用断点时,这会给出错误

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);
     }
 }

javascript office365 office-js add-in word-addins
1个回答
0
投票

您的

selection
变量是一个 Range 对象。 Range 没有parentSectionOrNullObject 属性。该属性只能在 Body 对象上找到。正如我在回答您的其他问题时所解释的:如何使用 Office javascript api 获取 Word.Section Range?,您需要首先获取 selection 的父级
body

© www.soinside.com 2019 - 2024. All rights reserved.