Hello Team,
I am using Aspose.Words for Node.js and I need to detect and replace a specific table inside a DOCX file.
My Requirement
I want to:
- Find a table
- by index
- OR by bookmark
- OR by matching specific text inside the table
- Remove/Clear the existing table
- Insert a new table in the exact same location
@AbhayKumar007
Steps
-
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' });
-
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 });
-
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"
-
Delete the existing table
await client.deleteTable({ name: 'input.docx', nodePath: insertPath });
-
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())
});
-
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,
Thank you for the detailed steps.
I have one more doubt:
Problem
In my document, the table is copy-pasted directly inside the DOCX (not inserted from HTML), so there is no bookmark, no unique HTML tag, and no special metadata.
Question
After deleting the table, how can I again find the correct index or position to insert a new table, since the original table no longer exists?
Because after deleteTable, the collection tables is reordered (e.g., table 3 becomes table 2).
@AbhayKumar007,
To assist you more effectively with your tasks, I am transferring your thread to the appropriate category. A member of Aspose.Words team will review your requirements and provide you with the necessary assistance soon.