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

RichApi.Error: GeneralException occurs when updating styles in HTML tables #4718

Open hnyrxs2 opened 1 month ago

hnyrxs2 commented 1 month ago

related to issue https://github.com/OfficeDev/office-js/issues/4463 just want to verify if the upcoming fixes this august would include the fixes for the html tables where style is being set. below is the gist used

$("#run").on("click", () => tryCatch(run));

async function run() {
  await Word.run(async (context) => {

    const defaultStyle: Word.Interfaces.TableUpdateData = {
      styleBuiltIn: 'TableGrid',
      font: {
        size: 11
      }
    };

    const testData = [
      ["Name", "Age", "Occupation"],
      ["Alice", "30", "Engineer"],
      ["Bob", "24", "Designer"],
      ["Charlie", "28", "Teacher"]
    ];

    const ccContainer = context.document.getSelection().insertContentControl();
    const table = document.createElement('table');
    const tableBody = document.createElement('tbody');
    testData.forEach(function (rowData) {
      const row = document.createElement('tr');
      rowData.forEach(function (cellData) {
        const cell = document.createElement('td');
        cell.appendChild(document.createTextNode(cellData));
        row.appendChild(cell);
      });
      tableBody.appendChild(row);
    });
    table.appendChild(tableBody);
    const htmlTable = ccContainer.insertHtml(table.outerHTML, Word.InsertLocation.start);
    htmlTable.set(defaultStyle);
    await context.sync();
  });
}

async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    console.error(error);
  }
}

Your Environment

Expected behavior

Upon rendering the HTML table, table style should be applied.

Current behavior

Plain HTML style is applied, no table style is visible. General Exception is logged on the console

Steps to reproduce

  1. Use the gist above on scriptlab
  2. run code
  3. error is encountered, expected style is not seen.

Context

This code gist is used to an add-in that enable users to create large tables of reports and should be customizable to their preferences.

Useful logs

console error on the scriptlab

[ERROR]: Error:
{
    "stack": "RichApi.Error: The argument you provided is not valid. Check the OfficeExtension.Error.debugInfo for more information.\n    at new n (https://appsforoffice.microsoft.com/lib/1/hosted/word-web-16.00.js:25:340405)\n    at o.processRequestExecutorResponseMessage (https://appsforoffice.microsoft.com/lib/1/hosted/word-web-16.00.js:25:404852)\n    at https://appsforoffice.microsoft.com/lib/1/hosted/word-web-16.00.js:25:402915",
    "message": "The argument you provided is not valid. Check the OfficeExtension.Error.debugInfo for more information.",
    "name": "RichApi.Error",
    "code": "InvalidArgument",
    "traceMessages": [],
    "innerError": null,
    "debugInfo": {
        "code": "InvalidArgument",
        "message": "The argument you provided is not valid. Check the OfficeExtension.Error.debugInfo for more information.",
        "errorLocation": "Range.styleBuiltIn",
        "statement": "insertHtml.styleBuiltIn = ...;",
        "surroundingStatements": [
            "...",
            "// Instantiate {selection}",
            "var insertContentControl = selection.insertContentControl();",
            "// Instantiate {insertContentControl}",
            "var insertHtml = insertContentControl.insertHtml(...);",
            "// Instantiate {insertHtml}",
            "// >>>>>",
            "insertHtml.styleBuiltIn = ...;",
            "// <<<<<",
            "var font = insertHtml.font;",
            "// Instantiate {font}",
            "font.size = ...;"
        ],
        "fullStatements": [
            "Please enable config.extendedErrorLogging to see full statements."
        ]
    },
    "httpStatusCode": 400
}
jipyua commented 1 month ago

hi @hnyrxs2 this is a different issue with case #4463. If you add a sentence htmlTable.select() before htmlTable.set(defaultStyle); you will notice that there is a separate empty paragraph before the table. So apply "TableGrid" style to such as range is not correct, since this style can only apply to a whole table. As why there is an empty paragraph before the table, this is a behavior defined by Word itself which you can try by adding an empty content control and insert a table into it from UI.

In order to work around this, I think you can try to get the table inside the content control by ccContainer.tables.getFirst().set(defaultStyle);

hnyrxs2 commented 1 month ago

Hello @jipyua thanks for the input. your workaround works for the gist i provided but we shall investigate further to the actual implementation on our add-in. But our implementation before works perfectly, is there any changes in the word behavior that caused this?

hnyrxs2 commented 1 month ago

Hello again @jipyua your work around works but it compromises the performance of the add in as well as the whole MS word itself. It its taking minutes if i were to insert the table that would be 2 more pages, then the word crashes.

jipyua commented 1 month ago

hi @hnyrxs2, thanks for trying the solution. From the above root cause analysis of your original issue, I don't think we have made changes of the behavior. Is it that you are trying to apply other non-table style to the range in the past? That way it will be always working.

About the new crash issue you mentioned, would you please share more information, such as your table code so that I can try to repro at my side and investigate what's happening?

4tti commented 1 month ago

Hello @jipyua, there was definitely behavior change. Before - like 3-4 months ago - our solution worked just fine, then we noticed that there was exactly same error as here in #4463 and that's why @hnyrxs2 raised it there and because there was not answer this thread was created.

Thanks a lot for your help!

You can test it yourself I guess with some old build, let' say half year ago or so. Edit: let us find time when it was working fine... Edit2: It was workin fine (based on our test cases) in May

jipyua commented 1 month ago

hi @4tti @hnyrxs2, thanks for providing further information, I tried the above code on a older build 16.0.17328.20514, but I still hit the same error. Could you please also have a try and explain more what's the behavior change you saw? Thanks.

hnyrxs2 commented 1 month ago

hello @jipyua the only behavioral change we saw was the GeneralException thrown when updating html table style. Our original implementation of this was almost 2 years ago, and just recently around May when we are doing regression testing we noticed the failing functions with html table implementations without any changes made from these past months / year.

jipyua commented 1 month ago

hi @hnyrxs2 , we will open an internal item to investigate this and see what's happening. At the same time, I suggest you use the work around to unblock you. Since there is only 1 table in the content contorl, the perf should be ok, if you see other unexpected cases, please let us know.

4tti commented 1 month ago

@jipyua The performance is not ok. The performance went significantly down:

but it compromises the performance of the add in as well as the whole MS word itself. It its taking minutes if i were to insert the table that would be 2 more pages, then the word crashes.

4tti commented 1 month ago

Any update please? @jipyua Do you need something from us?

jipyua commented 3 weeks ago

hi @4tti we have created an internal item to track this issue and if we have some investigation findings, we will share to you. thanks.

4tti commented 2 weeks ago

Thanks a lot @jipyua , once you have any update please let us know.

4tti commented 4 hours ago

@jipyua any update please?