Recent

Author Topic: [Solved] fpspread: any method to make range address in order  (Read 2489 times)

kjteng

  • Sr. Member
  • ****
  • Posts: 259
I think there is a method/function that will  change cell range  from 'B3:A1'  to  'A1:B3' but I could not find it. Any clue?
Thanks
« Last Edit: June 11, 2018, 03:15:12 pm by kjteng »

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: fpspread: any method to make range address in order
« Reply #1 on: May 27, 2018, 11:18:40 am »
There's no direct method available. You must use the formula parser to split the adress into parts and have it reassemble them:
Code: Pascal  [Select][+][-]
  1. uses
  2.   fpsExprParser;
  3.  
  4. function FixCellRange(AWorksheet: TsWorksheet; ARangeStr: String): String;
  5. var
  6.   parser: TsSpreadsheetParser;
  7. begin
  8.   parser := TsSpreadsheetParser.Create(AWorksheet);
  9.   try
  10.     parser.Expression := ARangeStr;
  11.     Result := parser.Expression;
  12.   finally
  13.     parser.Free;
  14.   end;
  15. end;
  16.  
  17. procedure TForm1.Button1Click(Sender: TObject);
  18. begin
  19.   ShowMessage(FixCellRange(WorksheetGrid.Worksheet, 'C3:A1'));
  20. end;

I am not sure if this is working in the release version because I changed a lot with the formula parser recently.

kjteng

  • Sr. Member
  • ****
  • Posts: 259
Re: fpspread: any method to make range address in order
« Reply #2 on: May 27, 2018, 12:48:19 pm »
yes. it works in trunk version.

I actually wrote my function as follows
Code: Pascal  [Select][+][-]
  1.  
  2. uses  fpsUtils, fpsTypes;
  3.  
  4.  
  5. function FixCellRange(AN: string): string;
  6. var cr: TsCellrange;
  7.     procedure Check(var a, b: cardinal);
  8.     var ii: cardinal;
  9.     begin
  10.       if a > b then
  11.         begin
  12.           ii := a; a := b; b := ii
  13.         end;
  14.     end;
  15. begin
  16.   ParseCellRangeString(AN, cr);
  17.   with cr do
  18.     begin
  19.       check(Col1, Col2);
  20.       check(Row1, Row2);
  21.     end;
  22.   Result := GetCellRangeString(cr);
  23. end;    
  24.  

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: fpspread: any method to make range address in order
« Reply #3 on: May 27, 2018, 01:29:45 pm »
Yes, of course. This is better because it does not require a worksheet

 

TinyPortal © 2005-2018