You've already forked lazarus-ccr
fpspreadsheet: Add method to copy a worksheet into another workbook.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4032 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -612,6 +612,8 @@ type
|
|||||||
{ Worksheet list handling methods }
|
{ Worksheet list handling methods }
|
||||||
function AddWorksheet(AName: string;
|
function AddWorksheet(AName: string;
|
||||||
ReplaceDuplicateName: Boolean = false): TsWorksheet;
|
ReplaceDuplicateName: Boolean = false): TsWorksheet;
|
||||||
|
function CopyWorksheetFrom(AWorksheet: TsWorksheet;
|
||||||
|
ReplaceDuplicateName: Boolean = false): TsWorksheet;
|
||||||
function GetFirstWorksheet: TsWorksheet;
|
function GetFirstWorksheet: TsWorksheet;
|
||||||
function GetWorksheetByIndex(AIndex: Integer): TsWorksheet;
|
function GetWorksheetByIndex(AIndex: Integer): TsWorksheet;
|
||||||
function GetWorksheetByName(AName: String): TsWorksheet;
|
function GetWorksheetByName(AName: String): TsWorksheet;
|
||||||
@ -905,10 +907,24 @@ end;
|
|||||||
@param AToCell Cell to which the format is to be copied
|
@param AToCell Cell to which the format is to be copied
|
||||||
-------------------------------------------------------------------------------}
|
-------------------------------------------------------------------------------}
|
||||||
procedure CopyCellFormat(AFromCell, AToCell: PCell);
|
procedure CopyCellFormat(AFromCell, AToCell: PCell);
|
||||||
|
var
|
||||||
|
sourceSheet, destSheet: TsWorksheet;
|
||||||
|
fmt: TsCellFormat;
|
||||||
|
font: TsFont;
|
||||||
begin
|
begin
|
||||||
Assert(AFromCell <> nil);
|
Assert(AFromCell <> nil);
|
||||||
Assert(AToCell <> nil);
|
Assert(AToCell <> nil);
|
||||||
AToCell^.FormatIndex := AFromCell^.FormatIndex;
|
sourceSheet := TsWorksheet(AFromCell.Worksheet);
|
||||||
|
destSheet := TsWorksheet(AToCell.Worksheet);
|
||||||
|
if (sourceSheet=nil) or (destSheet=nil) or (sourceSheet.Workbook = destSheet.Workbook) then
|
||||||
|
AToCell^.FormatIndex := AFromCell^.FormatIndex
|
||||||
|
else
|
||||||
|
begin
|
||||||
|
fmt := sourceSheet.ReadCellFormat(AFromCell);
|
||||||
|
font := sourceSheet.ReadCellFont(AFromCell);
|
||||||
|
fmt.FontIndex := destSheet.WriteFont(AToCell, font.FontName, font.Size, font.Style, font.Color);
|
||||||
|
destSheet.WriteCellFormat(AToCell, fmt);
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{@@ ----------------------------------------------------------------------------
|
{@@ ----------------------------------------------------------------------------
|
||||||
@ -1086,7 +1102,6 @@ end;
|
|||||||
-------------------------------------------------------------------------------}
|
-------------------------------------------------------------------------------}
|
||||||
destructor TsWorksheet.Destroy;
|
destructor TsWorksheet.Destroy;
|
||||||
begin
|
begin
|
||||||
// RemoveAllCells;
|
|
||||||
RemoveAllRows;
|
RemoveAllRows;
|
||||||
RemoveAllCols;
|
RemoveAllCols;
|
||||||
|
|
||||||
@ -1669,11 +1684,15 @@ begin
|
|||||||
// Fix row and column indexes overwritten
|
// Fix row and column indexes overwritten
|
||||||
AToCell^.Row := toRow;
|
AToCell^.Row := toRow;
|
||||||
AToCell^.Col := toCol;
|
AToCell^.Col := toCol;
|
||||||
|
AToCell^.Worksheet := self;
|
||||||
|
|
||||||
// Fix relative references in formulas
|
// Fix relative references in formulas
|
||||||
// This also fires the OnChange event.
|
// This also fires the OnChange event.
|
||||||
CopyFormula(AFromCell, AToCell);
|
CopyFormula(AFromCell, AToCell);
|
||||||
|
|
||||||
|
// Copy cell format
|
||||||
|
CopyFormat(AFromCell, AToCell);
|
||||||
|
|
||||||
// Merged?
|
// Merged?
|
||||||
if IsMergeBase(AFromCell) then
|
if IsMergeBase(AFromCell) then
|
||||||
begin
|
begin
|
||||||
@ -1706,11 +1725,16 @@ end;
|
|||||||
-------------------------------------------------------------------------------}
|
-------------------------------------------------------------------------------}
|
||||||
procedure TsWorksheet.CopyCell(AFromRow, AFromCol, AToRow, AToCol: Cardinal;
|
procedure TsWorksheet.CopyCell(AFromRow, AFromCol, AToRow, AToCol: Cardinal;
|
||||||
AFromWorksheet: TsWorksheet = nil);
|
AFromWorksheet: TsWorksheet = nil);
|
||||||
|
var
|
||||||
|
srcCell, destCell: PCell;
|
||||||
begin
|
begin
|
||||||
if AFromWorksheet = nil then
|
if AFromWorksheet = nil then
|
||||||
AFromWorksheet := self;
|
AFromWorksheet := self;
|
||||||
|
|
||||||
CopyCell(AFromWorksheet.FindCell(AFromRow, AFromCol), GetCell(AToRow, AToCol));
|
srcCell := AFromWorksheet.FindCell(AFromRow, AFromCol);
|
||||||
|
destCell := GetCell(AToRow, AToCol);
|
||||||
|
|
||||||
|
CopyCell(srcCell, destCell);
|
||||||
|
|
||||||
ChangedCell(AToRow, AToCol);
|
ChangedCell(AToRow, AToCol);
|
||||||
ChangedFont(AToRow, AToCol);
|
ChangedFont(AToRow, AToCol);
|
||||||
@ -6677,6 +6701,45 @@ begin
|
|||||||
FOnAddWorksheet(self, Result);
|
FOnAddWorksheet(self, Result);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
{@@ ----------------------------------------------------------------------------
|
||||||
|
Copies a worksheet (even from an external workbook) and adds it to the
|
||||||
|
current workbook
|
||||||
|
|
||||||
|
@param AWorksheet Worksheet to be copied. Can be in a different
|
||||||
|
workbook.
|
||||||
|
@param ReplaceDuplicateName The copied worksheet gets the name of the original.
|
||||||
|
If ReplaceDuplicateName is true and this sheet
|
||||||
|
name already exists then a number is added to
|
||||||
|
the sheet name to make it unique.
|
||||||
|
@return The instance of the newly created worksheet
|
||||||
|
@see TsWorksheet
|
||||||
|
-------------------------------------------------------------------------------}
|
||||||
|
function TsWorkbook.CopyWorksheetFrom(AWorksheet: TsWorksheet;
|
||||||
|
ReplaceDuplicateName: boolean = false): TsWorksheet;
|
||||||
|
var
|
||||||
|
r, c: Cardinal;
|
||||||
|
cell: PCell;
|
||||||
|
begin
|
||||||
|
Result := nil;
|
||||||
|
if (AWorksheet = nil) then
|
||||||
|
exit;
|
||||||
|
|
||||||
|
Result := AddWorksheet(AWorksheet.Name, ReplaceDuplicateName);
|
||||||
|
inc(FLockCount);
|
||||||
|
try
|
||||||
|
for cell in AWorksheet.Cells do
|
||||||
|
begin
|
||||||
|
r := cell^.Row;
|
||||||
|
c := cell^.Col;
|
||||||
|
Result.CopyCell(r, c, r, c, AWorksheet);
|
||||||
|
end;
|
||||||
|
finally
|
||||||
|
dec(FLockCount);
|
||||||
|
end;
|
||||||
|
|
||||||
|
Result.ChangedCell(r, c);
|
||||||
|
end;
|
||||||
|
|
||||||
{@@ ----------------------------------------------------------------------------
|
{@@ ----------------------------------------------------------------------------
|
||||||
Quick helper routine which returns the first worksheet
|
Quick helper routine which returns the first worksheet
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user