How to Replace an Existing Table in DOCX (Find by Index/Bookmark and Insert New Table) Using Aspose.Words for Node.js?

Hello Team,

I am using Aspose.Words for Node.js and I need to detect and replace a specific table inside a DOCX file.

:heavy_check_mark: My Requirement

I want to:

  1. Find a table
  • by index
  • OR by bookmark
  • OR by matching specific text inside the table
  1. Remove/Clear the existing table
  2. Insert a new table in the exact same location

@AbhayKumar007

Steps

  1. Load the document

    const words = require('asposewordscloud');
    const client = new words.WordsApiClient({ clientId: 'YOUR_CLIENT_ID', clientSecret: 'YOUR_CLIENT_SECRET' });
    const doc = await client.getDocument({ name: 'input.docx' });
    
  2. Locate the target table

    By index

    const tables = await client.getTables({ name: 'input.docx' });
    const targetTable = tables.tables[tableIndex];   // zero‑based index
    

    By bookmark (bookmark that surrounds the table)

    const bm = await client.getBookmark({ name: 'input.docx', bookmarkName: 'MyTableBookmark' });
    const targetTable = await client.getTable({ name: 'input.docx', nodePath: bm.bookmarkStart.nodePath });
    

    By text inside the table (search the document, then pick the table that contains the run)

    const result = await client.search({ name: 'input.docx', text: 'UniqueCellText' });
    const tablePath = result.matches[0].nodePath.split('/').slice(0, -1).join('/'); // go up to the Table node
    const targetTable = await client.getTable({ name: 'input.docx', nodePath: tablePath });
    
  3. Remember the insertion point – the table’s node path is needed to place the new table in the same location.

    const insertPath = targetTable.table.nodePath;   // e.g. "sections/0/body/tables/2"
    
  4. Delete the existing table

    await client.deleteTable({ name: 'input.docx', nodePath: insertPath });
    
  5. Insert the new table at the same location

    const newTable = new words.models.TableInsert({
        rowsCount: 3,
        columnsCount: 4,
        // optional: set cell formatting, borders, etc.
    });
    
    // The `nodePath` points to the parent where the table will be inserted.
    // For a table, the parent is the body of the section that previously held it.
    const parentPath = insertPath.replace(/\/tables\/\d+$/, '/'); // strip the old table index
    await client.insertTable({
        name: 'input.docx',
        nodePath: parentPath,
        table: newTable,
        // `index` tells the API where inside the parent collection to place the new table.
        // Use the original table index to keep the order unchanged.
        index: parseInt(insertPath.split('/').pop())
    });
    
  6. Save (or download) the modified document

    await client.saveDocument({ name: 'input.docx', saveOptions: { saveFormat: 'docx' } });
    // or download the file:
    const updated = await client.downloadDocument({ name: 'input.docx' });
    

Key points

  • getTables / getTable and deleteTable are part of the Tables API.
  • The nodePath (e.g. sections/0/body/tables/2) uniquely identifies a table’s position.
  • By extracting the parent path and the original index you can delete the old table and insert a new one without shifting other content.
  • When using a bookmark, retrieve the bookmark’s start node path and walk up to the containing table.

If you run into any version‑specific issues (e.g., method names differ between 20.x and 21.x), let us know the exact Aspose.Words for Node.js version you are using and share a minimal DOCX sample that reproduces the problem. That will allow us to give a precise code adjustment.

Hello Team,

const newTable = new words.models.TableInsert({
rowsCount: 3,
columnsCount: 4,
// optional: set cell formatting, borders, etc.
});

// The nodePath points to the parent where the table will be inserted.
// For a table, the parent is the body of the section that previously held it.
const parentPath = insertPath.replace(//tables/\d+$/, ‘/’); // strip the old table index
await client.insertTable({
name: ‘input.docx’,
nodePath: parentPath,
table: newTable,
// index tells the API where inside the parent collection to place the new table.
// Use the original table index to keep the order unchanged.
index: parseInt(insertPath.split(‘/’).pop())
});

Please explain this code, and if possible, please provide some example code with a file.