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
685 stars 95 forks source link

Parent contentControl is not found on the event DocumentSelectionChanged #2344

Closed benjamin-almarza closed 7 months ago

benjamin-almarza commented 2 years ago

Message from office-js bot: We’re closing this issue because it has been inactive for a long time. We’re doing this to keep the issues list manageable and useful for everyone. If this issue is still relevant for you, please create a new issue. Thank you for your understanding and continued feedback.

We add an event in the document that triggers when a contentControl is modified. Our issue is that in some documents (not all) the placeholder is null from the point of view of the addin.

Your Environment

Platform [PC desktop, Mac, iOS, Office on the web]: Office on the web Host [Excel, Word, PowerPoint, etc.]: Word Operating System: Windows 10 Pro Browser (if using Office on the web): Chrome is up to date Version 96.0.4664.110 (Official Build) (64-bit)

Expected behavior

On selecting inside a contentControl: we should be able to access its tag and id.

Current behavior

On selecting inside a contentControl: contentControl.isNullObject is true.

Steps to reproduce

Upload the file bug_file.docx to office.com

Run the following code in Office Word addin online

const onPlaceHolderSelected = async () => {
  return Word.run(async context => {
    const range = context.document.getSelection();
    context.load(range, "text");

    const contentControl = range.parentContentControlOrNullObject;
    context.load(contentControl, "text, tag, id");

    await context.sync();

    const tag = contentControl.tag;
    if (contentControl.isNullObject) {
      return Promise.reject();
    }

    return { id: contentControl.id, tag: tag };
  });
}

Office.onReady(info => {
  Word.run(async () => {
    return Office.context.document.addHandlerAsync(Office.EventType.DocumentSelectionChanged, () => {
      onPlaceHolderSelected().then( value => {
        console.log(value);
      })
      .catch(() => {
        console.error("not a contentControl")
      });
    })
  })
});

Click with the mouse in the word "placeholder"

Ideally, the line "console.log(value);" should be run.

Context

Our application relies heavily on contentControl without this piece it doesn't work.

jingxuan0109 commented 2 years ago

Hi, I can not repro this issue. I am just wondering whether you can provide more details.

image

benjamin-almarza commented 2 years ago

Hi: I figured out something weird.

If we do the following:

Then the issue happens as I described

But if we do the following:

Then it doesn't fails I can see that the document.xml inside the docx changes a little

I attached a brand new file that fails consistently when I upload it : test_file_2.docx

Please upload this and test


I attached the document.xml before and after: document_after.xml document_before.xml

The most significative change I can see is from this:

  <w:sdtContent>
                    <w:r>

to this:

  <w:sdtContent>
                    <w:r w:rsidRPr="6826A8E4" w:rsidR="73081A6C">
jingxuan0109 commented 2 years ago

I am just wondering how you insert a content control. It seems I cannot get any content controls for you document. But it can be get for new inserted content control.

benjamin-almarza commented 2 years ago

1.- How we created the document

We are creating programmatically the document using aspose

This is the snippet of the code

#create a new content control
sdt = Rjb::import('com.aspose.words.StructuredDocumentTag').new(aspose_doc, sdt_type_plain_text, markup_level_inline)
#lock it to prevent from accidental deletion in MS-Word
sdt.setLockContentControl(true)
#add tag for identification
sdt.setTag(tag_text)
#set text for context control, font etc.
sdt.appendChild(run_node(display_text, fontName, fontSize, fontColor))
sdt.isShowingPlaceholderText(false)

#replace the default text message
sdt.getRange.replace("Click here to enter text.", "", false, false)
doc_builder.insertNode(sdt)

2.- The contentControls we have. For every purpose, the contentControls we create work fine.

They look and behave as normal contentControls as you can see screenshot

Besides, one of our requirements is that we cannot control the documents received from our clients. If it is a validly created Docx with contentControls we should be able to interact with them.

3.- Document to test

I attached a document before and after downloading it from office.com. If you upload both to office.com and run the test I gave you, in one the contentControl "contract name" will work and in the other not. This may be useful to test because they are practically the same file.

jerryvaz commented 2 years ago

@RuoyingLiang Did you get a chance to look into this case? We are pretty stuck and blocked on this issue.

jingxuan0109 commented 2 years ago

I am just wondering whether you can try richtext content control.

chiz-ms commented 2 years ago

The issue is tracked in our backlog as ADO#5705306.

benjamin-almarza commented 2 years ago

The issue disappears when we change plain text content control to rich text. (Thanks, this would help us for the newer created documents, but we need a solution for the old one )