When applying row-height rules to selected Word tables, invalid table or row references are skipped selections that need to be reported. A table index identifies a table by its zero-based position in the document, and a row index identifies a row by its zero-based position inside that table.
The setTableRowHeight primitive returns missing table and row indexes in its mutation result so that skipped selections are not mistaken for updated rows.[1] Because table lookup happens before row lookup, a missing table index is reported separately from a missing row index.
Below is a test scenario of setTableRowHeight: out-of-range table and row indexes are reported in the mutation result.
The scenario
Given a document with one table containing two rows,
When setTableRowHeight is called with table indexes [0, 2], row indexes [1, 5], valueTwips: 420, and rule: 'exact',
Then
result.missingTableIndexescontains2.result.missingRowIndexesequalsexpect.arrayContaining([expect.objectContaining({ tableIndex: 0, rowIndex: 5 })]).
The test fixture
The fixture builds a Word table and calls the row-height mutation with both valid and invalid selections.[2]
Below is the test fixture code.
test.openspec('setTableRowHeight reports missing indexes')('Scenario: setTableRowHeight reports missing indexes', async ({ when, then, attachPrettyJson }: AllureBddContext) => {
const doc = makeDoc(
'<w:tbl>' +
'<w:tr><w:tc><w:p><w:r><w:t>A1</w:t></w:r></w:p></w:tc></w:tr>' +
'<w:tr><w:tc><w:p><w:r><w:t>A2</w:t></w:r></w:p></w:tc></w:tr>' +
'</w:tbl>',
);
let result!: ReturnType<typeof setTableRowHeight>;
await when('setTableRowHeight is called with out-of-range indexes', async () => {
result = setTableRowHeight(doc, {
tableIndexes: [0, 2],
rowIndexes: [1, 5],
valueTwips: 420,
rule: 'exact',
});
await attachPrettyJson('Result', result);
});
await then('the result SHALL report missing indexes', () => {
expect(result.missingTableIndexes).toContain(2);
expect(result.missingRowIndexes).toEqual(
expect.arrayContaining([expect.objectContaining({ tableIndex: 0, rowIndex: 5 })]),
);
});
});
The expected result shape
The scenario asserts the diagnostic fields on the value returned by setTableRowHeight, because those fields show which requested selections could not be applied.
Below are the result assertions for this scenario.
expect(result.missingTableIndexes).toContain(2);
expect(result.missingRowIndexes).toEqual(
expect.arrayContaining([expect.objectContaining({ tableIndex: 0, rowIndex: 5 })]),
);
Below is a description of the expected fields:
missingTableIndexesis expected to contain2, because the fixture document contains only table index0.missingRowIndexesis expected to include{ tableIndex: 0, rowIndex: 5 }, because table0has row indexes0and1, so row index5is out of range.
A non-obvious detail
The implementation deduplicates and sorts requested table indexes before processing them. Row indexes are also deduplicated and sorted for each existing table, so missing row reports are tied to a resolved table index rather than to a missing table selection.