Office.js API - 在word文件中添加嵌套内容控件

Office.js API - Add nested content control in word file

我正在为 Office Word 开发一个插件,我想添加允许用户以嵌套内容控件的形式按层次顺序添加内容的功能。

        let wordContentControl:any;
        Word.run(async context => {
           
            const range = context.document.getSelection();
            range.clear();
            wordContentControl = range.insertContentControl();
            wordContentControl.tag = "NameTest";
            wordContentControl.insertText("Sample", 'End');
            wordContentControl.cannotEdit = false;
            wordContentControl.appearance = 'BoundingBox';

            wordContentControl.font.color = 'red';
            await context.sync()


        }).catch((error) => {
            console.log(error);
        });
        Word.run(async context => {
            var contentControlsWithTag = context.document.contentControls.getByTag('NameTest');
            // Queue a command to load the tag property for all of content controls.
            context.load(contentControlsWithTag, 'tag');

            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                if (contentControlsWithTag.items.length === 0) {
                    console.log('No content control found.');
                }
                else {
                    return context.sync()
                        .then(function () {
                           
                            context.sync();
                            var ccs = context.document.contentControls;
                            
                            ccs.load("NameTest");
                            
                            console.log("Nr cc: " + ccs.items.length);
                            let cc = ccs.items[0];

                        });
                }
            });
        });

这就是我到目前为止所做的。但我找不到添加内容控件 within/nested 内容控件的方法。 你的帮助对我很有帮助。

我测试过,在内容控件中调用 insertContentControl 应该有效:

var contentControl = context.document.getSelection().insertText('outer\n', Word.InsertLocation.start).insertContentControl();
contentControl.insertText('inner', Word.InsertLocation.end).insertContentControl();

请看看这是否适合你。