OfficeDev / office-js

A repo and NPM package for Office.js, corresponding to a copy of what gets published to the official "evergreen" Office.js CDN, at https://appsforoffice.microsoft.com/lib/1/hosted/office.js.
https://learn.microsoft.com/javascript/api/overview
Other
641 stars 92 forks source link

Word.API body.search() does not match paragraphs with footnote or endnote #4401

Closed mscc-sascha closed 5 days ago

mscc-sascha commented 3 weeks ago

Provide required information needed to triage your issue

If a paragraph in MS Word contains a footnote or endnote and I try to search for the entire paragraph with context.document.body.search(), the result is empty.

Your Environment

Expected behavior

The entire paragraph should be matched when I search for the text.

Current behavior

No results available if I search for a part of the paragraph when there is a footnote or endnote in between.

Steps to reproduce

  1. Create a word document and insert a sample paragraph (e.g. Video provides a powerful way to help you prove your point.)
  2. Insert a footnote to any word in the sentence
  3. Search for the entire paragraph using OfficeJS context.document.body.search("Video provides a powerful 1way to help you prove your point.")
  4. Search result will be empty

Link to live example(s)

Snippets for Script Lab

Script

$("#basic-search").on("click", () => tryCatch(basicSearch));

async function basicSearch() {
  // Does a basic text search and highlights matches in the document.
  await Word.run(async (context) => {
    const match = context.document.body.search("Video provides a powerful 1way to help you prove your point.");

    match.load("items");

    await context.sync();

    const startRange = match.items[0];
    startRange.select();

    await context.sync();
  });
}

async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}

HTML

<section class="ms-font-m">
    Sample
</section>

<section class="samples ms-font-m">
    <button id="basic-search" class="ms-Button">
    <span class="ms-Button-label">Search entire paragraph</span>
  </button>
</section>

CSS

section.samples {
    margin-top: 20px;
}

section.samples .ms-Button, section.setup .ms-Button {
    display: block;
    margin-bottom: 5px;
    margin-left: 20px;
    min-width: 80px;
}

Libraries

https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js

office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

core-js@2.4.1/client/core.min.js
@types/core-js

jquery@3.1.1
@types/jquery@3.3.1
microsoft-github-policy-service[bot] commented 3 weeks ago

Thank you for letting us know about this issue. We will take a look shortly. Thanks.

xiruatms commented 1 week ago

@mscc-sascha , thanks for reporting this.

I assuming you're trying to search some words with marker for footnote/endnote as following: image

Is that correct?

As the screenshot shows, this is not supported with Word on Win32 or Online.

mscc-sascha commented 6 days ago

@xiruatms Thanks for verifying! You are right, the default search in MS Word does not match the parts neither. We will implement a work around then.

xiruatms commented 5 days ago

Thanks for your confirmation. Close this ticket as agreed.