fpspreadsheet: Add PageLayout record to Worksheet; implement reading and writing of page margins for xlsx.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4102 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2015-04-29 20:00:07 +00:00
parent 3a2b5f2aad
commit 4f520e0bf2
4 changed files with 87 additions and 0 deletions

View File

@@ -105,6 +105,7 @@ type
FDefaultColWidth: Single; // in "characters". Excel uses the width of char "0" in 1st font
FDefaultRowHeight: Single; // in "character heights", i.e. line count
FSortParams: TsSortParams; // Parameters of the current sorting operation
FPageLayout: TsPageLayout;
FOnChangeCell: TsCellEvent;
FOnChangeFont: TsCellEvent;
FOnCompareCells: TsCellCompareEvent;
@@ -467,6 +468,8 @@ type
{@@ The default row height is given in "line count" (height of the
default font }
property DefaultRowHeight: Single read FDefaultRowHeight write FDefaultRowHeight;
{@@ Page layout parameters for printing }
property PageLayout: TsPageLayout read FPageLayout write FPageLayout;
// These are properties to interface to TsWorksheetGrid
{@@ Parameters controlling visibility of grid lines and row/column headers,
@@ -1111,6 +1114,15 @@ begin
FMergedCells := TsMergedCells.Create;
FHyperlinks := TsHyperlinks.Create;
with FPageLayout do begin
LeftMargin := InToPts(0.7);
RightMargin := InToPts(0.7);
TopMargin := InToPts(0.78740157499999996);
BottomMargin := InToPts(0.78740157499999996);
HeaderDistance := InToPts(0.3);
FooterDistance := InToPts(0.3);
end;
FDefaultColWidth := 12;
FDefaultRowHeight := 1;

View File

@@ -668,6 +668,15 @@ type
cctError : (ErrorValue: TsErrorValue);
end;
TsPageLayout = record
LeftMargin: Double; // in Points
RightMargin: Double;
TopMargin: Double;
BottomMargin: Double;
HeaderDistance: Double;
FooterDistance: Double;
end;
function BuildFormatStringFromSection(const ASection: TsNumFormatSection): String;

View File

@@ -139,6 +139,7 @@ function PtsToTwips(AValue: Single): Integer;
function cmToPts(AValue: Double): Double;
function PtsToCm(AValue: Double): Double;
function InToPts(AValue: Double): Double;
function PtsToIn(AValue: Double): Double;
function mmToPts(AValue: Double): Double;
function PtsToMM(AValue: Double): Double;
function pxToPts(AValue, AScreenPixelsPerInch: Integer): Double;
@@ -1987,6 +1988,17 @@ begin
Result := AValue * 72;
end;
{@@ ----------------------------------------------------------------------------
Converts points to inches (72 pts = 1 inch)
@param AValue Length value in points
@return Value converted to inches
-------------------------------------------------------------------------------}
function PtsToIn(AValue: Double): Double;
begin
Result := AValue / 72;
end;
{@@ ----------------------------------------------------------------------------
Converts millimeters to points (72 pts = 1 inch)

View File

@@ -77,6 +77,7 @@ type
procedure ReadHyperlinks(ANode: TDOMNode);
procedure ReadMergedCells(ANode: TDOMNode; AWorksheet: TsWorksheet);
procedure ReadNumFormats(ANode: TDOMNode);
procedure ReadPageMargins(ANode: TDOMNode; AWorksheet: TsWorksheet);
procedure ReadPalette(ANode: TDOMNode);
procedure ReadRowHeight(ANode: TDOMNode; AWorksheet: TsWorksheet);
procedure ReadSharedStrings(ANode: TDOMNode);
@@ -129,6 +130,7 @@ type
procedure WriteMergedCells(AStream: TStream; AWorksheet: TsWorksheet);
procedure WriteNumFormatList(AStream: TStream);
procedure WritePalette(AStream: TStream);
procedure WritePageMargins(AStream: TStream; AWorksheet: TsWorksheet);
procedure WriteSheetData(AStream: TStream; AWorksheet: TsWorksheet);
procedure WriteSheetViews(AStream: TStream; AWorksheet: TsWorksheet);
procedure WriteStyleList(AStream: TStream; ANodeName: String);
@@ -1301,6 +1303,44 @@ begin
end;
end;
procedure TsSpreadOOXMLReader.ReadPageMargins(ANode: TDOMNode;
AWorksheet: TsWorksheet);
var
s: String;
layout: TsPageLayout;
begin
if ANode = nil then
exit;
layout := AWorksheet.PageLayout;
s := GetAttrValue(ANode, 'left');
if s <> '' then
layout.LeftMargin := HtmlLengthStrToPts(s);
s := GetAttrValue(ANode, 'right');
if s <> '' then
layout.RightMargin := HtmlLengthStrToPts(s);
s := GetAttrValue(ANode, 'top');
if s <> '' then
layout.TopMargin := HtmlLengthStrToPts(s);
s := GetAttrValue(ANode, 'bottom');
if s <> '' then
layout.BottomMargin := HtmlLengthStrToPts(s);
s := GetAttrValue(ANode, 'header');
if s <> '' then
layout.HeaderDistance := HtmlLengthStrToPts(s);
s := GetAttrValue(ANode, 'footer');
if s <> '' then
layout.FooterDistance := HtmlLengthStrToPts(s);
AWorksheet.PageLayout := layout;
end;
procedure TsSpreadOOXMLReader.ReadPalette(ANode: TDOMNode);
var
node, colornode: TDOMNode;
@@ -1669,6 +1709,7 @@ begin
ReadWorksheet(Doc.DocumentElement.FindNode('sheetData'), FWorksheet);
ReadMergedCells(Doc.DocumentElement.FindNode('mergeCells'), FWorksheet);
ReadHyperlinks(Doc.DocumentElement.FindNode('hyperlinks'));
ReadPageMargins(Doc.DocumentElement.FindNode('pageMargins'), FWorksheet);
FreeAndNil(Doc);
@@ -2262,6 +2303,18 @@ begin
'</colors>');
end;
procedure TsSpreadOOXMLWriter.WritePageMargins(AStream: TStream;
AWorksheet: TsWorksheet);
begin
with AWorksheet.PageLayout do
AppendToStream(AStream, Format(
'<pageMargins left="%g" right="%g" top="%g" bottom="%g" header="%g" footer="%g" />', [
PtsToIn(LeftMargin), PtsToIn(RightMargin), PtsToIn(TopMargin), PtsToIn(BottomMargin),
PtsToIn(HeaderDistance), PtsToIn(FooterDistance) ],
FPointSeparatorSettings
));
end;
procedure TsSpreadOOXMLWriter.WriteSheetData(AStream: TStream;
AWorksheet: TsWorksheet);
var
@@ -2889,6 +2942,7 @@ begin
WriteSheetData(FSSheets[FCurSheetNum], AWorksheet);
WriteHyperlinks(FSSheets[FCurSheetNum], AWorksheet);
WriteMergedCells(FSSheets[FCurSheetNum], AWorksheet);
WritePageMargins(FSSheets[FCurSheetNum], AWorksheet);
// Footer
if AWorksheet.Comments.Count > 0 then