[Example] How to replicate table rows
Posted: Sun Jan 25, 2015 11:44 am
The function below replicates rows of the current table in rve, Count times. New rows are added to the end of the table.
Heading rows are not replicated.
The function replicates the table structure, cell and row attributes, default text and paragraph styles in cells. It does not replicate cells contents.
This operation is undoable.
The function returns True on successful replication.
Possible reasons for a failure:
- the current item is not a table
- no non-heading rows in the table
- text protection or read-only mode
Example
Before:
After calling ReplicateRowsInCurrentTable(RichViewEdit1, 10):
Heading rows are not replicated.
The function replicates the table structure, cell and row attributes, default text and paragraph styles in cells. It does not replicate cells contents.
This operation is undoable.
Code: Select all
function ReplicateRowsInCurrentTable(rve: TCustomRichViewEdit; Count: Integer): Boolean;
var Table: TRVTableItemInfo;
StartRow, i, r, c, RowCount, Data, SourceRow, DestRow: Integer;
DestCell, SourceCell: TRVTableCellData;
begin
Result := rve.GetCurrentItemEx(TRVTableItemInfo, rve, TCustomRVItemInfo(Table));
if not Result then
exit;
if table.CanUseHeadingRowCount then
StartRow := Table.HeadingRowCount
else
StartRow := 0;
RowCount := Table.RowCount-StartRow;
Result := RowCount>0;
if not Result then
exit;
Result := rve.CanChange;
if not Result then
exit;
rve.BeginUndoGroup(rvutModifyItem);
rve.SetUndoGroupMode(True);
rve.BeginItemModify(Table.GetMyItemNo, Data);
try
table.InsertRows(table.RowCount, RowCount*Count, -1, False);
for i := 0 to Count-1 do
for r := 0 to RowCount-1 do begin
SourceRow := StartRow+r;
DestRow := StartRow+(i+1)*RowCount+r;
for c := 0 to Table.ColCount-1 do begin
SourceCell := Table.Cells[SourceRow,c];
if (SourceCell<>nil) then begin
if (SourceCell.ColSpan>1) or (SourceCell.RowSpan>1) then
Table.MergeCells(DestRow,c, SourceCell.ColSpan, SourceCell.RowSpan, False);
DestCell := Table.Cells[DestRow,c];
DestCell.AssignAttributesFrom(SourceCell, True, 1, 1);
DestCell.Clear;
DestCell.AddNL('', SourceCell.GetFirstTextStyleNo(True), SourceCell.GetFirstParaStyleNo);
end;
end;
table.Rows[DestRow].PageBreakBefore := table.Rows[SourceRow].PageBreakBefore;
table.Rows[DestRow].KeepTogether := table.Rows[SourceRow].KeepTogether;
table.Rows[DestRow].VAlign := table.Rows[SourceRow].VAlign;
end;
table.DeleteEmptyRows;
table.DeleteEmptyCols;
finally
rve.SetUndoGroupMode(False);
rve.EndItemModify(Table.GetMyItemNo, Data);
rve.Change;
end;
end;
Possible reasons for a failure:
- the current item is not a table
- no non-heading rows in the table
- text protection or read-only mode
Example
Before:
After calling ReplicateRowsInCurrentTable(RichViewEdit1, 10):