OfficeVba/docx/TSDocxTable.tsf

222 lines
6.7 KiB
Plaintext

Type TSDocxTable = Class(TSVbaBase)
Uses TSDocxEnumerations;
public
Function Init(docx, table);
private
docx_;
table_; // TOfficeObj('TTable') 对象
range_;
public
Function ApplyStyleDirectFormatting(StyleName);
Function AutoFitBehavior(Behavior);
Function AutoFormat(Format, ApplyBorders, ApplyShading, ApplyFont, ApplyColor, ApplyHeadingRows, ApplyLastRow, ApplyFirstColumn, ApplyLastColumn, AutoFit);
Function Cell(Row, Column);
Function ConvertToText(Separator, NestedTables);
Function Delete();
Function Select();
Function Sort(ExcludeHeader, FieldNumber, SortFieldType, SortOrder, FieldNumber2, SortFieldType2, SortOrder2, FieldNumber3, SortFieldType3, SortOrder3, CaseSensitive, BidiSort, IgnoreThe, IgnoreKashida, IgnoreDiacritics, IgnoreHe, LanguageID);
Function SortAscending();
Function SortDescending();
Function Split(BeforeRow);
Function UpdateAutoFormat();
property AllowAutoFit read ReadAllowAutoFit write WriteAllowAutoFit;
property ApplyStyleColumnBands read ReadApplyStyleColumnBands write WriteApplyStyleColumnBands;
property ApplyStyleFirstColumn read ReadApplyStyleFirstColumn write WriteApplyStyleFirstColumn;
property ApplyStyleHeadingRows read ReadApplyStyleHeadingRows write WriteApplyStyleHeadingRows;
property ApplyStyleLastColumn read ReadApplyStyleLastColumn write WriteApplyStyleLastColumn;
property ApplyStyleLastRow read ReadApplyStyleLastRow write WriteApplyStyleLastRow;
property ApplyStyleRowBands read ReadApplyStyleRowBands write WriteApplyStyleRowBands;
property AutoFormatType read ReadAutoFormatType;
property Borders read ReadBorders;
property BottomPadding read ReadBottomPadding write WriteBottomPadding;
property Columns read ReadColumns;
property Descr read ReadDescr write WriteDescr;
property ID read ReadID write WriteID;
property LeftPadding read ReadLeftPadding write WriteLeftPadding;
property NestingLevel read ReadNestingLevel;
property PreferredWidth read ReadPreferredWidth write WritePreferredWidth;
property PreferredWidthType read ReadPreferredWidthType write WritePreferredWidthType;
property Range read ReadRange;
property RightPadding read ReadRightPadding write WriteRightPadding;
property Rows read ReadRows;
property Shading read ReadShading;
property Spacing read ReadSpacing write WriteSpacing;
property Style read ReadStyle write WriteStyle;
property TableDirection read ReadTableDirection write WriteTableDirection;
property Tables read ReadTables;
property Title read ReadTitle write WriteTitle;
property TopPadding read ReadTopPadding write WriteTopPadding;
property Uniform read ReadUniform;
Function ReadUniform();
Function WriteTopPadding();
Function ReadTopPadding();
Function WriteTitle();
Function ReadTitle();
Function ReadTables();
Function WriteTableDirection();
Function ReadTableDirection();
Function WriteStyle();
Function ReadStyle();
Function WriteSpacing();
Function ReadSpacing();
Function ReadShading();
Function ReadRows(index);
Function WriteRightPadding();
Function ReadRightPadding();
Function ReadRange();
Function WritePreferredWidthType(value);
Function ReadPreferredWidthType();
Function WritePreferredWidth(value);
Function ReadPreferredWidth();
Function ReadNestingLevel();
Function WriteLeftPadding();
Function ReadLeftPadding();
Function WriteID();
Function ReadID();
Function WriteDescr();
Function ReadDescr();
Function ReadColumns(index);
Function WriteBottomPadding();
Function ReadBottomPadding();
Function ReadBorders(index);
Function ReadAutoFormatType();
Function WriteApplyStyleRowBands();
Function ReadApplyStyleRowBands();
Function WriteApplyStyleLastRow();
Function ReadApplyStyleLastRow();
Function WriteApplyStyleLastColumn();
Function ReadApplyStyleLastColumn();
Function WriteApplyStyleHeadingRows();
Function ReadApplyStyleHeadingRows();
Function WriteApplyStyleFirstColumn();
Function ReadApplyStyleFirstColumn();
Function WriteApplyStyleColumnBands();
Function ReadApplyStyleColumnBands();
Function WriteAllowAutoFit();
Function ReadAllowAutoFit();
End;
// ============== 实现 ================= //
Function TSDocxTable.Init(docx, table);
Begin
docx_ := docx;
table_ := table;
range_ := nil;
End;
// function
Function TSDocxTable.ApplyStyleDirectFormatting(StyleName);
Begin
table_.TblPr.StyleId := docx_.StyleObject().GetStyle(StyleName).Value('StyleId');
table_.TblPr.Update();
End;
Function TSDocxTable.AutoFitBehavior(Behavior);
Begin
case Behavior of
TSDocxEnumerations.wdAutoFitContent():
begin
pr_width_type := "auto";
pr_width_value := 0;
width_value := 0;
width_type := "auto";
end
TSDocxEnumerations.wdAutoFitFixed():
begin
// 需要搞懂算法
pr_width_type := "dxa";
pr_width_value := 0;
end
TSDocxEnumerations.wdAutoFitWindow():
begin
pr_width_type := "pct";
pr_width_value := 5000;
width_type := "pct";
width_value := integer(5000 / table_.Cols());
end
End;
table_.TblPr.WidthType := pr_width_type;
table_.TblPr.Width := pr_width_value;
table_.TblPr.Update();
for r:=1 to table_.Rows() do
begin
for c:=1 to table_.Cols() do
begin
cel := table_.GetCell(r, c);
if ifnil(cel) then return nil;
tcpr_node := class(TSXml).GetNode(cel[0], "w:tcPr", "first");
tcpr := TOfficeObj("TwTcPr");
tcpr.InitRootNode(tcpr_node);
tcpr.Width := width_value;
tcpr.Type := width_type;
tcpr.Update();
end
end
End;
Function TSDocxTable.Cell(Row, Column);
Begin
cell_obj := new TSDocxCell(self.Application, self.Creator, self.Parent);
cell_obj.Init(docx_, table_, Row, Column);
return cell_obj;
End;
// property
Function TSDocxTable.ReadBorders(index);
Begin
borders_obj := new TSDocxBordersTable(self.Application, self.Creator, self.Parent);
borders_obj.Init(table_);
return ifnil(index) ? borders_obj : borders_obj[index];
End;
Function TSDocxTable.ReadColumns(index);
Begin
columns_obj := new TSDocxColumns(self.Application, self.Creator, self.Parent);
columns_obj.Init(table_);
return ifnil(index) ? columns_obj : columns_obj[index];
End;
Function TSDocxTable.ReadRows(index);
Begin
rows_obj := new TSDocxRows(self.Application, self.Creator, self.Parent);
rows_obj.Init(table_);
return ifnil(index) ? rows_obj : rows_obj[index];
End;
Function TSDocxTable.WritePreferredWidthType(value);
Begin
End;
Function TSDocxTable.ReadPreferredWidthType();
Begin
End;
Function TSDocxTable.WritePreferredWidth(value);
Begin
End;
Function TSDocxTable.ReadPreferredWidth();
Begin
End;
Function TSDocxTable.ReadRange();
Begin
if ifnil(range_) then
begin
pbegin := 0;
pend := 0;
arr := array();
root := docx_.Body().Root();
class(TSWdCommon).CalculateRange(root, root, table_.Root(), false, pbegin, pend, arr);
range_ := new TSDocxRange(self.Application, self.Creator, self);
range_.Init(pbegin, pend, arr);
end
return range_;
End;