首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Office.js获取与内容控件相邻的范围

Office.js获取与内容控件相邻的范围
EN

Stack Overflow用户
提问于 2018-08-23 11:53:58
回答 1查看 694关注 0票数 2

我有下面的情景词:

  • 带有一些内容控件的文本文档(也包括文本)
  • 内容控件总是与普通文本相邻(文本和内容控件之间没有空格)。
  • 内容控件在创建时具有与相邻文本相同的样式(通过编辑其font.colorfont.namefont.size等从插入内容控件的选择中读取)
  • 当内容控件被更新(它们的内容被更新)时,它们的样式被重置,我需要再次应用文档样式(不是问题,工作正常)。

问题是,当内容控件被更新时,当前的文档选择可以在任何地方,但我需要确保它们重新应用与它们相邻的文本相同的样式。

在Office.js中是否有一种方法可以获取与内容控件相邻的范围?这样,我就可以读取它的样式并将其应用到内容控件中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-23 17:27:22

我的假设是,您需要内容控件前面的文本。不幸的是,对象模型没有提供与getNextTextRange相同的“倒退”(getPreviousTextRange)。

这是可以做到的,但有点周密。以下是我的脚本实验室示例代码:

代码语言:javascript
运行
复制
async function
    getAdjacentRange() {
    await Word.run(async (context) => {

        var ccs = context.document.contentControls;
        ccs.load("items");
        await context.sync();
        console.log("Nr cc: " + ccs.items.length);
        let cc = ccs.items[0];

        //The starting point of the content control so that 
        //the range can be extended backwards          
        let ccRange = cc.getRange("Start");
        let ccParas = ccRange.paragraphs; cc.load("text,range,paragraphs");
        let ccPara = ccParas.getFirst();
        ccParas.load("items")

    //The content control must be in a paragraph: get that paragraph's
    //starting point so that the range can be extended in that direction
        let paraRange = ccPara.getRange("Start");
        let rng = paraRange.expandTo(ccRange);
   //Get the words in the range of the start of the paragraph to the 
   //start of the content control in order to get the last one before the content control
        let em = [" "];
        let words = rng.getTextRanges(em, true);
        words.load("items");
        await context.sync();
        let nrWords = words.items.length;

      //minus 2 to get second to last word since last word is directly adjacent to content control (no space)
        let lastWord = words.items[nrWords - 2];

        //Now get the content from the end of the second to last word to
        //the start of the content control, which will be the last word
        let word2BeforeCC = lastWord.getRange("End");
        let wordBeforeCC = word2BeforeCC.expandTo(ccRange);
        wordBeforeCC.load("text");
        await context.sync();
        console.log(cc.text + "/ Word before the content control: " + wordBeforeCC.text + " / nr Words: " + nrWords);
})
    }

当最后一个单词和内容控件之间有一个空格时,为这种情况编写代码:

代码语言:javascript
运行
复制
async function
    getAdjacentRange() {
    await Word.run(async (context) => {
        var ccs = context.document.contentControls;
        ccs.load("items");
        await context.sync();

        let cc = ccs.getFirstOrNullObject();

        //The starting point of the content control so that 
        //the range can be extended backwards
        let ccRange = cc.getRange("Start");

        //The content control must be in a paragraph, so get that paragraph's
        //starting point so that the range can be extended in that direction
        let ccParas = ccRange.paragraphs; cc.load("text,range,paragraphs");
        let ccPara = ccParas.getFirst();
        ccParas.load("items")
        let paraRange = ccPara.getRange("Start");
        let rng = paraRange.expandTo(ccRange);

        //Now it's possible to get the last word in the extended range
        let em = [" "];
        console.log(em.length.toString());
        let words = rng.getTextRanges(em, true);
        words.load("items");
        await context.sync();
        let nrWords = words.items.length;
        //returns a Range object, from which you can get the style info
        let lastWord = words.items[nrWords - 1];

        await context.sync();
        console.log(cc.text + "/" + "nr Words: " + nrWords + "last word: " + lastWord.text);
})
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51985240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档