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

Word: Last paragraph in OOXML from table selection is outside of cell. #5017

Open TheBakwai1 opened 3 weeks ago

TheBakwai1 commented 3 weeks ago

I am using context.document.getSelection() to get a selection. From there I do a getOoxml(). I have noticed that when I select all the content in a single table cell, the final line / paragraph is copied into the Ooxml outside of the table.

Also, is it possible just to copy the content of a cell with the getOoxml and leave out the table structure?

Your Environment

Expected behavior

The final line / paragraph should be inside the table.

Current behavior

Steps to reproduce

  1. Create a table. One cell needs to have multiple lines image
  2. Select the content in the cell with multple lines including the final line image
  3. Run script in script lab
$("#run").on("click", () => tryCatch(run));

async function run() {
  await Word.run(async (context) => {
    const range = context.document.getSelection();
    const ooxml = range.getOoxml();
    await context.sync();
    console.log(ooxml.value);
  });
}

// Default helper for invoking an action and handling errors.
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);
  }
}
  1. Observe output Ooxml. Line with text "Four" is outside of the table.

image

Context

We want to be able to copy content based on the users selection, and it should copy as selected eg, all content is inside the table / cell

Ideally, selecting content in a single cell should only copy the content and not the table et al.

RuizhiSunMS commented 3 weeks ago

Hi @TheBakwai1, thx for your submit.

I did a quick try following your code and repro steps but things looked good at my side. Would you please share more about how you create the table or other information? image

TheBakwai1 commented 3 weeks ago

Hi @RuizhiSunMS ,

thanks for coming back to me. I just clicked Insert > Table > 3x3

image

And then entered One, , Two , Three and Four.

And then finally, just dragged a selection starting from just before the 'O' on the first line to just after the 'r' on the last line

I don't think there is anything else I'm doing.

Thanks, Steve

RuizhiSunMS commented 3 weeks ago

Weird. I still can't reproduce it. Looks what we have done are almost the same. I even added several more lines like the below. image But all lines got found in xml. image ooxml_lost_last_paragraph_not_repro_0.docx I uploaded my file, would you please share yours?

PS: I also tried with web, everything goes good. Would you please confirm getooxml() works fine on the web at your side?

TheBakwai1 commented 3 weeks ago

I can repo it with your document as well. I can't see all your xml, some of it is hidden by the popup. is the Four included in the tags?

This is the xml from the output from your document. Sorry, its a bit small, but the Four is outside of the table that has the three other paragraphs image

This is the document I used One.docx

RuizhiSunMS commented 3 weeks ago

@TheBakwai1, I think I get your point. You mean you can get the last line but the last line is out of cell node in xml structure. If my understanding is right, then I'll go on. It has been put on our backlog #9491181, the team will investigate and we will reply to you as soon as there is any progress. Thank you for your patience. Sorry that I thought you said you couldn't get last line.

TheBakwai1 commented 3 weeks ago

Hi @RuizhiSunMS, no worries :) I'm just glad it wasn't just me.

Thank you for your assistance.