From 2ee3d18801d7e0285f1274fa418f3e94d52f6fb4 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 19 Jan 2016 18:40:41 +0000 Subject: [PATCH] fpspreadsheet: Activate html codes for Cells property of TsWorksheetGrid. Extended grid demo showing run-time generation of content. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4442 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../visual/fpsgrid_no_install/fpsgrid.lpi | 6 +- .../visual/fpsgrid_no_install/mainfrm.lfm | 2 +- .../visual/fpsgrid_no_install/mainfrm.pas | 64 +++++++++++++++++-- .../fpspreadsheet/fpspreadsheetgrid.pas | 11 +++- 4 files changed, 68 insertions(+), 15 deletions(-) diff --git a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/fpsgrid.lpi b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/fpsgrid.lpi index 93fe2444a..bd4f9b862 100644 --- a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/fpsgrid.lpi +++ b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/fpsgrid.lpi @@ -56,7 +56,7 @@ - + @@ -68,10 +68,6 @@ - - - - diff --git a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.lfm b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.lfm index f5870dc8c..8da9d74f9 100644 --- a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.lfm +++ b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.lfm @@ -7,7 +7,7 @@ object Form1: TForm1 ClientHeight = 420 ClientWidth = 680 OnCreate = FormCreate - LCLVersion = '1.5' + LCLVersion = '1.7' object ButtonPanel: TPanel Left = 0 Height = 38 diff --git a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.pas b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.pas index 468a012fb..9821b6a5b 100644 --- a/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.pas +++ b/components/fpspreadsheet/examples/visual/fpsgrid_no_install/mainfrm.pas @@ -7,7 +7,7 @@ interface uses Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls, ComCtrls, StdCtrls, Grids, - fpspreadsheet, fpspreadsheetgrid, {%H-}fpsallformats; + fpstypes, fpspreadsheet, fpspreadsheetgrid, {%H-}fpsallformats; type @@ -52,6 +52,8 @@ uses { TForm1 } procedure TForm1.FormCreate(Sender: TObject); +const + THICK_BORDER: TsCellBorderStyle = (LineStyle: lsThick; Color: clNavy); begin Grid := TsWorksheetGrid.Create(self); @@ -66,14 +68,64 @@ begin goThumbTracking, // see the grid scroll while you drag the scrollbar goHeaderHotTracking, // hot-tracking of header cells goHeaderPushedLook, // click at header cells --> pushed look - goDblClickAutoSize // optimum col width/row height after dbl click at header border + goDblClickAutoSize, // optimum col width/row height after dbl click at header border + goCellHints // show cell hints (needed for cell comments) ]; - Grid.AutoAdvance := aaDown; // move active cell down on ENTER + Grid.AutoAdvance := aaDown; // on ENTER, move active cell down Grid.MouseWheelOption := mwGrid; // mouse wheel scrolls the grid, not the active cell Grid.TextOverflow := true; // too long text extends into neighbor cells + Grid.AutoCalc := true; // automatically calculate formulas + Grid.ShowHint := true; // needed to show cell comments // Create an empty worksheet - Grid.NewWorkbook(26, 100); + //Grid.NewWorkbook(26, 100); // Not absolutely necessary - grid will expand automatically + + // Add some cells and formats + Grid.ColWidths[1] := 180; + Grid.ColWidths[2] := 80; + + Grid.Cells[1,1] := 'This is a demo'; + Grid.MergeCells(Rect(1,1, 2,1)); + Grid.HorAlignment[1,1] := haCenter; + Grid.CellBorders[Rect(1,1, 2,1)] := [cbSouth]; + Grid.CellBorderStyles[Rect(1,1, 2,1), cbSouth] := THICK_BORDER; + Grid.BackgroundColors[Rect(1,1, 2,1)] := RGBToColor(220, 220, 220); + Grid.CellFontColor[1,1] := clNavy; + Grid.CellFontStyle[1,1] := [fssBold]; + + Grid.Cells[1,2] := 'Number:'; + Grid.HorAlignment[1,2] := haRight; + Grid.CellFontStyle[1,2] := [fssItalic]; + Grid.CellFontColor[1,2] := clNavy; + Grid.Cells[2,2] := 1.234; + + Grid.Cells[1,3] := 'Date:'; + Grid.HorAlignment[1,3] := haRight; + Grid.CellFontStyle[1,3] := [fssItalic]; + Grid.CellFontColor[1,3] := clNavy; + Grid.NumberFormat[2,3] := 'mm"/"dd, yyyy'; + Grid.Cells[2,3] := date; + + Grid.Cells[1,4] := 'Time:'; + Grid.HorAlignment[1,4] := haRight; + Grid.CellFontStyle[1,4] := [fssItalic]; + Grid.CellFontColor[1,4] := clNavy; + Grid.NumberFormat[2,4] := 'hh:nn'; + Grid.Cells[2,4] := now(); + + Grid.Cells[1,5] := 'Rich text:'; + Grid.HorAlignment[1,5] := haRight; + Grid.CellFontStyle[1,5] := [fssItalic]; + Grid.CellFontColor[1,5] := clNavy; + Grid.Cells[2,5] := '100 cm2'; + + Grid.Cells[1,6] := 'Formula:'; + Grid.HorAlignment[1,6] := haRight; + Grid.CellFontStyle[1,6] := [fssItalic]; + Grid.CellFontColor[1,6] := clNavy; + Grid.Cells[2,6] := '=B2^2*PI()'; + Grid.CellComment[2,6] := 'Area of the circle with radius given in cell B2'; + Grid.NumberFormat[2,6] := '0.000'; end; procedure TForm1.BtnLoadClick(Sender: TObject); @@ -143,7 +195,7 @@ begin try try // Load file into workbook and grid - Grid.LoadFromSpreadsheetFile(UTF8ToSys(AFileName)); + Grid.LoadFromSpreadsheetFile(UTF8ToAnsi(AFileName)); // Update user interface Caption := Format('fpsGrid - %s (%s)', [ @@ -160,7 +212,7 @@ begin Grid.NewWorkbook(26, 100); Caption := 'fpsGrid - no name'; TabControl.Tabs.Clear; - // Grab the error message + // Grab the error message, it will be displayed below Grid.Workbook.AddErrorMsg(E.Message); end; end; diff --git a/components/fpspreadsheet/fpspreadsheetgrid.pas b/components/fpspreadsheet/fpspreadsheetgrid.pas index 0f9edb57e..11ed2af99 100644 --- a/components/fpspreadsheet/fpspreadsheetgrid.pas +++ b/components/fpspreadsheet/fpspreadsheetgrid.pas @@ -664,7 +664,7 @@ implementation uses Types, LCLType, LCLIntf, LCLProc, Math, StrUtils, - fpCanvas, fpsStrings, fpsUtils, fpsVisualUtils, fpsNumFormat; + fpCanvas, fpsStrings, fpsUtils, fpsVisualUtils, fpsNumFormat, fpsHTMLUtils; const {@@ Default number of columns prepared for a new empty worksheet } @@ -4992,7 +4992,8 @@ var fmt: PsCellFormat = nil; nfp: TsNumFormatParams; r, c: Cardinal; - s: String; + s, plain: String; + rtParams: TsRichTextParams; begin if not Assigned(Worksheet) then exit; @@ -5014,7 +5015,11 @@ begin if (s <> '') and (s[1] = '=') then Worksheet.WriteFormula(r, c, Copy(s, 2, Length(s)), true) else - Worksheet.WriteText(r, c, s); // This will erase a non-formatted cell if s = '' + begin + cell := Worksheet.GetCell(r, c); + HTMLToRichText(Workbook, Worksheet.ReadCellFont(cell), s, plain, rtParams); + Worksheet.WriteText(cell, plain, rtParams); // This will erase a non-formatted cell if s = '' + end; end else if VarIsType(AValue, varDate) then Worksheet.WriteDateTime(r, c, VarToDateTime(AValue))