【问题标题】:Office.js get range adjacent to a content controlOffice.js 获取与内容控件相邻的范围
【发布时间】:2018-08-23 11:53:58
【问题描述】:

我有以下场景单词:

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

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

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

【问题讨论】:

  • 试试这个。如果可行,我将给出答案:使用 ContentControl.getRange() 获取控件的范围。然后使用 Range.getNextTextRange(或 Range.getNextTextRangeOrNullObject)获取控件后面的范围。然后读取范围的样式并将其重新应用到控件。

标签: javascript ms-word office-js word-contentcontrol


【解决方案1】:

我的假设是您希望文本紧接在内容控件之前。不幸的是,对象模型没有提供与getNextTextRange 等效的“向后”(getPreviousTextRange)。

可以,但是有点绕。以下是我的 Script Lab 示例代码:

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

最后一个单词和内容控件之间有空格的情况的代码:

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

【讨论】:

  • 是的,我想要抄送前的文字。我正在尝试代码,但有 1 个问题:lastWord 打印最后一个单词 + 形容词的第一个单词。抄送。它们之间没有空格,我应该明确地写出来。因此,我可以得到font,但font.color 是空的,因为该范围内有两种不同的颜色。在我的示例中,字体本身在段落和 CC 中都是相同的,所以如果我打印font.name,我会得到Calibri。有没有办法只获取 CC 之前的最后一个单词而不将其连接到 CC 的内容?
  • 是的,@dnmh,尽管这更加令人费解(并且需要一些思考)。我也编辑了我的答案以包含这种方法。请编辑您的问题以包含其他信息,以便我的回答对其他感兴趣的人有意义:-)
猜你喜欢
  • 2019-07-23
  • 2015-02-14
  • 2017-01-22
  • 2016-12-15
  • 2021-06-07
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多