Word JS: get a Range object based on start and end indices

Background

I’m writing an add-on to help visually highlight certain text in court opinions, legal memos, and law review articles. As an example:

The question is whether section 404(c) of the FSA bars Jackson’s 2021 Motion. The answer is yes, because Jackson’s 2018 Motion received a “complete review…on the merits.” Section 404(c) states that “[n]o court shall entertain a motion made under [Section 404] to reduce a sentence…if a previous motion made under this section to reduce the sentence was…denied after a complete review of the motion on the merits.” Section 404(c) thus unambiguously “establishes that a defendant can file only one motion for resentencing.” United States v. Smith, 982 F.3d 106, 112 (2d Cir. 2020) (quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019)); see also United States v. Mannie, 971 F.3d 1145, 1152 (10th Cir. 2020) (stating that section 404(c) “essentially ensure[s] that offenders only get one bite of the apple”); United States v. Maxwell, 800 Fed. Appx. 373, 376 (6th Cir. 2020) (“The First Step Act gives defendants only one bite at the apple”).

In this text, at the most basic level, I want to highlight quotes in one color:

  • “complete review…on the merits.”
  • “[n]o court shall entertain a motion made under [Section 404] to reduce a sentence…if a previous motion made under this section to reduce the sentence was…denied after a complete review of the motion on the merits.”
  • “establishes that a defendant can file only one motion for resentencing.”

And parentheticals in another color:

  • (stating that section 404(c) “essentially ensure[s] that offenders only get one bite of the apple”)
  • (“The First Step Act gives defendants only one bite at the apple”)

I also want to highlight all start and end parentheses and all semicolons, provided that they’re not nested within a parenthetical, as in, e.g., “(quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019)).”

Desired approach + problem

To me, the most natural solution is, for each paragraph, to get the start and end index of a section that I want to highlight, then to create a Range object based off of that start/end indices, and then to modify the range accordingly. E.g., consider the following text: “Word is a great program. See Word v. Word Perfect, 55 F.3d 55 (19th Cir. 2054) (concluding “Word is superior”).” For this text, one set of start/end indices of interest would be [54, 84], which corresponds to “(concluding “Word is superior”).”

Because some of the text I’d like to highlight is likely to repeat—words, phrases, punctuation marks—it won’t do to just search for the word, because that will pick out a number of false positives. E.g., searching for “)” will pick out the closing parentheses on “(c),” which I don’t want to highlight, and the inner closing parentheses on “(quoting United States v. Jackson, 945 F.3d 315, 321 (5th Cir. 2019)),” which I also don’t want to highlight.

Unfortunately, there seems to be no way to get a Range object by start and end index.
I’ve searched around the Word JS documentation and on Stack Overflow, but to no avail. All similar questions are from several years ago and conclude that it’s not possible to select ranges by start and end index, but because several years have passed, it seems possible that things have changed.

That is, my question is: is it possible—and if so, how—to get a Range object by the start and end index of text in the paragraph, as described above?

Some questions I’ve looked at:

  • Word js: how to get the Range object from start and end index
  • Getting a range’s surrounding text in Office-js
  • Word JS APIs: extending a Range
  • OfficeJs/WordJs select text range by index and bind click event
  • How to get a range from start & end index with Office.js while developing MS Word Add-in

The code I have in place right now, which works for searching for words rather than by indices, is as follows:

async function run() {
  Word.run(async (context) => {
    const color = Office.context.document.settings.get("parentheticalColor");
    const paragraphs = context.document.body.paragraphs;
    paragraphs.load("items");
    await context.sync();

    for (let i = 0; i < paragraphs.items.length; i++) {
      const paragraph = paragraphs.items[i];

      const text = paragraph.text;
      const response = await textAPI(text);
      let matches = response.parentheticals;
      await context.sync();

      // matches is currently, e.g, ['of the FSA', '"establishes that a', '.']
      // ideally it would be, e.g., [[5, 14], [19, 37], [56, 56]]
      // then you could do something like paragraph.getRange(start, end)
      
      for (let match of matches) {
        const ranges = paragraph.search(match, { matchCase: true });
        ranges.load("items");
        await context.sync();

        for (let k = 0; k < ranges.items.length; k++) {
          ranges.items[k].font.color = color;
        }
      }
    }

    await context.sync();
  }).catch(function (error) {
    console.log("Error: " + JSON.stringify(error));
  });
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật