📄 uxlsworkbookglobals.pas
字号:
unit UXlsWorkbookGlobals;
{$IFDEF LINUX}{$INCLUDE ../FLXCOMPILER.INC}{$ELSE}{$INCLUDE ..\FLXCOMPILER.INC}{$ENDIF}
interface
uses Classes, SysUtils, UXlsBaseRecords, UXlsBaseRecordLists, UXlsOtherRecords, UXlsChart,
UXlsSST, XlsMessages, UXlsSections, UXlsReferences, USheetNameList, UXlsEscher,
UXlsFormula, UEscherRecords, UXlsPalette, UXlsXF, UFlxMessages;
type
TBoundSheetList = class
private
FSheetNames: TSheetNameList; //Cache with all the sheet names to speed up searching
FBoundSheets: TBoundSheetRecordList;
public
property BoundSheets: TBoundSheetRecordList read FBoundSheets;
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure Add(const aRecord: TBoundSheetRecord);
procedure SaveToStream(const DataStream: TStream);
procedure SaveRangeToStream(const DataStream: TStream; const SheetIndex: integer);
function TotalSize:int64;
function TotalRangeSize(const SheetIndex: integer): int64;
procedure InsertSheet(const BeforeSheet: integer; const OptionFlags: word; const SheetName: WideString);
procedure DeleteSheet(const SheetIndex: integer);
end;
TWorkbookGlobals = class( TBaseSection)
private
FSST: TSST;
FReferences: TReferences;
FBoundSheets: TBoundSheetList;
FMiscRecords: TBaseRecordList;
FNames : TNameRecordList;
FDrawingGroup: TDrawingGroup;
FWindow1: TWindow1Record;
F1904: T1904Record;
FBookBool: TBookBoolRecord;
FPrecision: TPrecisionRecord;
FXF: TXFRecordList;
FFonts: TFontRecordList;
FFormats: TFormatRecordList;
FPaletteCache: TPaletteRecord;
FPaletteIndex: integer;
FHasMacro: boolean;
FCodeName: WideString;
function GetSheetCount: integer;
function GetSheetName(const index: integer): Widestring;
procedure SetSheetName(const index: integer; const Value: Widestring);
function GetSheetVisible(const index: integer): TXlsSheetVisible;
procedure SetSheetVisible(const index: integer; const Value: TXlsSheetVisible);
function GetSheetOptionFlags(const index: integer): word;
function GetActivesheet: integer;
procedure SetActiveSheet(const Value: integer);
function GetColorPalette(Index: integer): LongWord;
procedure SetColorPalette(Index: integer; const Value: LongWord);
function GetIs1904: boolean;
function GetPrecisionAsDisplayed: boolean;
function GetSaveExternalLinkValues: boolean;
procedure SetIs1904(const Value: boolean);
procedure SetPrecisionAsDisplayed(const Value: boolean);
procedure SetSaveExternalLinkValues(const Value: boolean);
public
property SST: TSST read FSST;
property SheetName[const index: integer]: Widestring read GetSheetName write SetSheetName;
procedure SetFirstSheetVisible(const index: integer);
property SheetVisible[const index: integer]: TXlsSheetVisible read GetSheetVisible write SetSheetVisible;
property SheetCount: integer read GetSheetCount;
property SheetOptionFlags[const index: integer]: word read GetSheetOptionFlags;
procedure SheetSetOffset(const index: integer; const Offset: LongWord);
property ActiveSheet: integer read GetActivesheet write SetActiveSheet;
property DrawingGroup: TDrawingGroup read FDrawingGroup;
property References: TReferences read FReferences;
property Names: TNameRecordList read FNames;
property HasMacro: boolean read FHasMacro;
property CodeName: widestring read FCodeName;
constructor Create;
destructor Destroy; override;
function TotalSize:int64; override;
function TotalRangeSize(const SheetIndex: integer; const CellRange: TXlsCellRange): int64; override;
procedure Clear; override;
procedure LoadFromStream( const DataStream: TStream; const First: TBOFRecord; const SST: TSST); override;
procedure SaveToStream(const DataStream: TStream);override;
procedure SaveRangeToStream(const DataStream: TStream; const SheetIndex: integer; const CellRange: TXlsCellRange);override;
procedure InsertAndCopyRowsAndCols(const FirstRow, LastRow, DestRow, aRowCount, FirstCol, LastCol, DestCol, aColCount: integer; const SheetInfo: TSheetInfo);
procedure DeleteRowsAndCols(const aRow, aRowCount, aCol, aColCount: word;const SheetInfo: TSheetInfo);
procedure DeleteSheets(const SheetIndex, SheetCount: integer);
procedure InsertSheets(const CopyFrom: integer; BeforeSheet: integer; const OptionFlags: word; const Name: WideString; const SheetCount: byte);
property ColorPalette[Index: integer]: LongWord read GetColorPalette write SetColorPalette;
property XF:TXFRecordList read FXF;
property Fonts:TFontRecordList read FFonts;
property Formats:TFormatRecordList read FFormats;
property Is1904: boolean read GetIs1904 write SetIs1904;
property PrecisionAsDisplayed: boolean read GetPrecisionAsDisplayed write SetPrecisionAsDisplayed;
property SaveExternalLinkValues: boolean read GetSaveExternalLinkValues write SetSaveExternalLinkValues;
procedure DeleteCountry;
class function IsValidRangeName(const Name: WideString; var OptionFlags: Integer): Boolean;
procedure CheckInternalNames(const OptionFlags: integer);
procedure AddName(var Range: TXlsNamedRange; const CellList: pointer);
end;
implementation
{ TBoundSheetList }
procedure TBoundSheetList.Add(const aRecord: TBoundSheetRecord);
begin
FSheetNames.Add(aRecord.SheetName);
FBoundSheets.Add(aRecord); //Last
end;
procedure TBoundSheetList.Clear;
begin
if FSheetNames<>nil then FSheetNames.Clear;
if FBoundSheets<>nil then FBoundSheets.Clear;
end;
procedure TBoundSheetList.DeleteSheet(const SheetIndex: integer);
begin
FSheetNames.DeleteSheet(FBoundSheets.SheetName[SheetIndex]);
FBoundSheets.Delete(SheetIndex);
end;
constructor TBoundSheetList.Create;
begin
inherited;
FSheetNames:= TSheetNameList.Create;
FBoundSheets:= TBoundSheetRecordList.Create;
end;
destructor TBoundSheetList.Destroy;
begin
FreeAndNil(FSheetNames);
FreeAndNil(FBoundSheets);
inherited;
end;
procedure TBoundSheetList.InsertSheet(const BeforeSheet: integer;
const OptionFlags: word; const SheetName: WideString);
var
NewName: WideString;
begin
NewName:= FSheetNames.AddUniqueName(SheetName);
FBoundSheets.Insert(BeforeSheet, TBoundSheetRecord.CreateNew(OptionFlags, NewName));
end;
procedure TBoundSheetList.SaveRangeToStream(const DataStream: TStream; const SheetIndex: integer);
begin
if (SheetIndex>=FBoundSheets.Count)or (SheetIndex<0) then raise Exception.CreateFmt(ErrInvalidSheetNo, [SheetIndex,0,FBoundSheets.Count-1]);
FBoundSheets[SheetIndex].SaveToStream(DataStream);
end;
procedure TBoundSheetList.SaveToStream(const DataStream: TStream);
begin
FBoundSheets.SaveToStream(DataStream);
end;
function TBoundSheetList.TotalSize: int64;
begin
TotalSize:= FBoundSheets.TotalSize;
end;
function TBoundSheetList.TotalRangeSize(const SheetIndex: integer): int64;
begin
if (SheetIndex>=FBoundSheets.Count)or (SheetIndex<0) then raise Exception.CreateFmt(ErrInvalidSheetNo, [SheetIndex,0,FBoundSheets.Count-1]);
Result:=FBoundSheets[SheetIndex].TotalSize;
end;
{ TWorkbookGlobals }
procedure TWorkbookGlobals.Clear;
begin
inherited;
if FSST<>nil then FSST.Clear;
if FReferences<>nil then FReferences.Clear;
if FBoundSheets<>nil then FBoundSheets.Clear;
if FMiscRecords<>nil then FMiscRecords.Clear;
if FNames<>nil then FNames.Clear;
if FDrawingGroup<>nil then FDrawingGroup.Clear;
if FXF<>nil then FXF.Clear;
if FFonts<>nil then FFonts.Clear;
if FFormats<>nil then FFormats.Clear;
FPaletteCache:=nil;
FWindow1:=nil;
F1904:=nil;
FBookBool:=nil;
FPrecision:=nil;
FHasMacro:=false;
FCodeName:='';
end;
constructor TWorkbookGlobals.Create;
begin
inherited;
FSST:= TSST.Create;
FReferences:= TReferences.Create;
FBoundSheets:= TBoundSheetList.Create;
FMiscRecords:= TBaseRecordList.Create;
FNames:=TNameRecordList.Create;
FDrawingGroup:= TDrawingGroup.Create;
FXF:= TXFRecordList.Create;
FFonts:= TFontRecordList.Create;
FFormats:= TFormatRecordList.Create;
FPaletteCache:=nil;
FWindow1:=nil;
F1904:=nil;
FBookBool:=nil;
FPrecision:=nil;
FHasMacro:=false;
FCodeName:='';
end;
procedure TWorkbookGlobals.DeleteRowsAndCols(const aRow, aRowCount, aCol, aColCount: word; const SheetInfo: TSheetInfo);
begin
FNames.ArrangeInsertRowsAndCols(aRow, -aRowCount, aCol, -aColCount, SheetInfo);
end;
procedure TWorkbookGlobals.DeleteSheets(const SheetIndex,
SheetCount: integer);
var
i: integer;
begin
if HasMacro then raise Exception.Create(ErrCantDeleteSheetWithMacros); //If we delete a sheet that has a corresponding macro on the vba stream, Excel 2000 will crash when opening the file. Excel Xp seems to handle this ok.
for i:=0 to SheetCount-1 do
FBoundSheets.DeleteSheet(SheetIndex);
FReferences.InsertSheets(SheetIndex, -SheetCount);
FNames.DeleteSheets(SheetIndex, SheetCount);
end;
destructor TWorkbookGlobals.Destroy;
begin
FreeAndNil(FSST);
FreeAndNil(FReferences);
FreeAndNil(FBoundSheets);
FreeAndNil(FMiscRecords);
FreeAndNil(FNames);
FreeAndNil(FDrawingGroup);
FreeAndNil(FXF);
FreeAndNil(FFonts);
FreeAndNil(FFormats);
inherited;
end;
function TWorkbookGlobals.GetActivesheet: integer;
begin
if FWindow1<>nil then Result:= FWindow1.ActiveSheet else Result:=0;
end;
function TWorkbookGlobals.GetColorPalette(Index: integer): LongWord;
begin
if FPaletteCache=nil then Result:=StandardPalette(Index) else Result:=FPaletteCache.Color[Index];
end;
function TWorkbookGlobals.GetIs1904: boolean;
begin
if F1904<>nil then Result:=F1904.Is1904 else Result:=false;
end;
function TWorkbookGlobals.GetPrecisionAsDisplayed: boolean;
begin
if FPrecision<>nil then Result:=FPrecision.PrecisionAsDisplayed else Result:=false;
end;
function TWorkbookGlobals.GetSaveExternalLinkValues: boolean;
begin
if FBookBool<>nil then Result:=FBookBool.SaveExternalLinkValues else Result:=false;
end;
function TWorkbookGlobals.GetSheetCount: integer;
begin
Result:= FBoundSheets.BoundSheets.Count;
end;
function TWorkbookGlobals.GetSheetName(const index: integer): Widestring;
begin
Result:= FBoundSheets.BoundSheets.SheetName[index];
end;
function TWorkbookGlobals.GetSheetOptionFlags(const index: integer): word;
begin
Result:= FBoundSheets.BoundSheets[index].OptionFlags;
end;
function TWorkbookGlobals.GetSheetVisible(const index: integer): TXlsSheetVisible;
begin
Result:= FBoundSheets.BoundSheets.SheetVisible[index];
end;
procedure TWorkbookGlobals.InsertAndCopyRowsAndCols(const FirstRow, LastRow, DestRow, aRowCount, FirstCol, LastCol, DestCol, aColCount: integer; const SheetInfo: TSheetInfo);
begin
FNames.ArrangeInsertRowsAndCols(DestRow, (LastRow -FirstRow +1)* aRowCount, DestCol, (LastCol -FirstCol +1)* aColCount, SheetInfo);
end;
procedure TWorkbookGlobals.InsertSheets(const CopyFrom: integer; BeforeSheet: integer;
const OptionFlags: word; const Name: WideString; const SheetCount: byte);
var
i, ofs: integer;
SheetInfo: TSheetInfo;
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -