Lazarus

Programming => Packages and Libraries => FPSpreadsheet => Topic started by: jcmontherock on March 08, 2018, 03:53:50 pm

Title: Cells range borders
Post by: jcmontherock on March 08, 2018, 03:53:50 pm
I am starting to play with the sample «demo_virtualmode_write». I'm able to define a cells range, but I can't find how to draw a border around the range (sheet for example). How to do that ?
Title: Re: Cells range borders
Post by: wp on March 10, 2018, 10:24:14 am
Since there is no Range variant of the WriteBorder methods you must iterate through the cells at the border of the range and set the borders accordingly (Worksheet.WriteBorders, .WriteBorderStyle -- see http://wiki.lazarus.freepascal.org/FPSpreadsheet#Cell_borders). It's a bit lengthy, though...
Title: Re: Cells range borders
Post by: jcmontherock on March 13, 2018, 06:32:26 pm
Thanks for your answer. For a big table lengthy is the word....    ;O)
Title: Re: Cells range borders
Post by: wp on March 13, 2018, 07:03:23 pm
No, using loops it is no difference whether you deal  with a 2x2 or a 100x100 range. The problem is the in the many cases. Here is a simple demo with an outer border only:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   SysUtils,
  7.   fpstypes, fpspreadsheet, fpsallformats;
  8.  
  9.   procedure WriteBorder(ASheet: TsWorksheet; ALeft,ATop,ARight,ABottom: Integer;
  10.     ALeftStyle, ATopStyle, ARightStyle, ABottomStyle: TsCellBorderStyle);
  11.   var
  12.     r, c: Integer;
  13.     cell: PCell;
  14.   begin
  15.     for c := ALeft to ARight do begin
  16.       ASheet.WriteBorderStyle(ATop, c, cbNorth, ATopStyle);
  17.       ASheet.WriteBorders(ATop, c, [cbNorth]);
  18.       ASheet.WriteBorderStyle(ABottom, c, cbSouth, ABottomStyle);
  19.       ASheet.WriteBorders(ABottom, c, [cbSouth]);
  20.     end;
  21.     for r := ATop to ABottom do begin
  22.       ASheet.WriteBorderStyle(r, ALeft, cbWest, ALeftStyle);
  23.       ASheet.WriteBorders(r, ALeft, [cbWest]);
  24.       ASheet.WriteBorderStyle(r, ARight, cbEast, ARightStyle);
  25.       ASheet.WriteBorders(r, ARight, [cbEast]);
  26.     end;
  27.     ASheet.WriteBorders(ATop, ALeft, [cbNorth, cbWest]);
  28.     ASheet.WriteBorders(ABottom, ALeft, [cbSouth, cbWest]);
  29.     ASheet.WriteBorders(ATop, ARight, [cbNorth, cbEast]);
  30.     ASheet.WriteBorders(ABottom, ARight, [cbSouth, cbEast]);
  31.   end;
  32.  
  33. var
  34.   book: TsWorkbook;
  35.   sheet: TsWorksheet;
  36.   r, c: Cardinal;
  37.  
  38. const
  39.   ThinBorder: TsCellBorderStyle = (Linestyle:lsThin; Color:scBlack);
  40.   ThickBorder: TsCellBorderStyle = (LineStyle:lsThick; Color:scBlack);
  41.  
  42. begin
  43.   book := TsWorkbook.Create;
  44.   try
  45.     sheet := book.AddWorksheet('Test');
  46.     for r := 0 to 99 do
  47.       for c := 0 to 99 do
  48.         sheet.WriteText(r, c, Format('R%dC%d', [r,c]));
  49.  
  50.     WriteBorder(sheet, 5, 5, 50, 50, ThickBorder, ThinBorder, ThickBorder, ThinBorder);
  51.  
  52.     book.WriteToFile('test.xlsx', sfOOXML, true);
  53.   finally
  54.     book.Free;
  55.   end;
  56.  
  57. end.
Title: Re: Cells range borders
Post by: jcmontherock on March 21, 2018, 06:47:43 pm
Thanks a lot. It's what I was looking for.
TinyPortal © 2005-2018