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
657 stars 93 forks source link

Template Refreshes After Document Generation #4644

Open punyakrishna opened 3 days ago

punyakrishna commented 3 days ago

Description: After generating a document, the template refreshes unexpectedly. This behavior occurs without any API failures.

Steps to Reproduce:

  1. Use the MS Office API to generate a document based on the template.
  2. Delete the existing table after generating the new table.
  3. Observe the behavior after the document is generated.

Expected Behavior: The template should remain unchanged after the document is generated.

Actual Behavior: The template refreshes unexpectedly after the document is generated. If the line that deletes the existing table is commented out, the document contains both the generated and created tables, and the template does not refresh.

Additional Information:

Environment:

Notes:

Code:

  await Word.run(async (context: Word.RequestContext) => {
    const documentTables: Word.TableCollection = context.document.body.tables.load();
    await context.sync();
    for (let index = 0; index < tableData.length; index++) {
      const existingTable: Word.Table = documentTables.items[index];
      const updatedTable: string[][] = tableData[index];

      if (updatedTable.length > 1 && updatedTable[0].length < updatedTable[1].length) {
        for (let k = updatedTable[0].length; k < updatedTable[1].length; k++) {
          updatedTable[0].push("");
        }
      }

      // Load the table rows
      existingTable.load("rows/items");
      await context.sync();

      const insertedTable = existingTable.insertTable(
        updatedTable.length,
        updatedTable[0].length,
        Word.InsertLocation.after,
        updatedTable
      );

      insertedTable.styleBuiltIn = Word.BuiltInStyleName.gridTable4;
      insertedTable.alignment = "Left";

      await context.sync();

      if (documentTables.items.length > index && existingTable) {
        existingTable.delete();
        await context.sync();
      }
    }
  });
microsoft-github-policy-service[bot] commented 3 days ago

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

shanshanzheng-dev commented 3 days ago

Hi @punyakrishna Thanks for reporting this issue. In order to repro your problem better, could you provide the whole code if possible? Thanks.