:fix: handle rowSpan
by convert between the virtual column index and the root index
This commit is contained in:
@ -54,4 +54,44 @@ export class TableRow extends XmlComponent {
|
||||
// Offset because properties is also in root.
|
||||
this.root.splice(index + 1, 0, cell);
|
||||
}
|
||||
|
||||
public addCellToColumnIndex(cell: TableCell, columnIndex: number): void {
|
||||
const rootIndex = this.columnIndexToRootIndex(columnIndex, true);
|
||||
this.addCellToIndex(cell, rootIndex - 1);
|
||||
}
|
||||
|
||||
public rootIndexToColumnIndex(rootIndex: number): number {
|
||||
// convert the root index to the virtual column index
|
||||
if (rootIndex < 1 || rootIndex >= this.root.length) {
|
||||
throw new Error(`cell 'rootIndex' should between 1 to ${this.root.length - 1}`);
|
||||
}
|
||||
let colIdx = 0;
|
||||
// Offset because properties is also in root.
|
||||
for (let rootIdx = 1; rootIdx < rootIndex; rootIdx++) {
|
||||
const cell = this.root[rootIdx] as TableCell;
|
||||
colIdx += cell.options.columnSpan || 1;
|
||||
}
|
||||
return colIdx;
|
||||
}
|
||||
|
||||
public columnIndexToRootIndex(columnIndex: number, allowEndNewCell: boolean = false): number {
|
||||
// convert the virtual column index to the root index
|
||||
// `allowEndNewCell` for get index to inert new cell
|
||||
if (columnIndex < 0) {
|
||||
throw new Error(`cell 'columnIndex' should not less than zero`);
|
||||
}
|
||||
let colIdx = 0;
|
||||
// Offset because properties is also in root.
|
||||
let rootIdx = 1;
|
||||
const endRootIndex = allowEndNewCell ? this.root.length : this.root.length - 1;
|
||||
while (colIdx <= columnIndex) {
|
||||
if (rootIdx > endRootIndex) {
|
||||
throw new Error(`cell 'columnIndex' should not great than ${colIdx - 1}`);
|
||||
}
|
||||
const cell = this.root[rootIdx] as TableCell;
|
||||
rootIdx += 1;
|
||||
colIdx += (cell && cell.options.columnSpan) || 1;
|
||||
}
|
||||
return rootIdx - 1;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user