簡體   English   中英

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];

                        });
                }
            });
        });

這是我到目前為止所做的。 但我無法找到在/嵌套內容控件中添加內容控件的方法。 你的幫助對我很有幫助。

在此處輸入圖像描述

我測試並在內容控件中調用insertContentControl應該可以工作:

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

結果截圖

請看看這是否適合你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM