Recent

Author Topic: Can we create pivotable  (Read 426 times)

Packs

  • Sr. Member
  • ****
  • Posts: 366
Can we create pivotable
« on: October 22, 2024, 04:40:51 am »
Can we create pivotable

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: Can we create pivotable
« Reply #1 on: October 22, 2024, 06:33:12 am »
Yes. Why not?
Simple example:
Code: Pascal  [Select][+][-]
  1. program PivotTable;
  2. {$mode objfpc}
  3. type
  4.   TData = record
  5.     Category: string;
  6.     Value: Integer;
  7.   end;
  8.  
  9. var
  10.   Data: array[1..5] of TData;
  11.   Summary: array[1..2] of record
  12.     Category: string;
  13.     Total: Integer;
  14.   end;
  15.   i, j: Integer;
  16.  
  17. begin
  18.   // Sample data
  19.   Data[1].Category := 'A'; Data[1].Value := 10;
  20.   Data[2].Category := 'B'; Data[2].Value := 20;
  21.   Data[3].Category := 'A'; Data[3].Value := 30;
  22.   Data[4].Category := 'B'; Data[4].Value := 40;
  23.   Data[5].Category := 'A'; Data[5].Value := 50;
  24.  
  25.   // Initialize summary
  26.   Summary[1].Category := 'A'; Summary[1].Total := 0;
  27.   Summary[2].Category := 'B'; Summary[2].Total := 0;
  28.  
  29.   // Process data
  30.   for i := 1 to Length(Data) do
  31.   begin
  32.     for j := 1 to Length(Summary) do
  33.     begin
  34.       if Data[i].Category = Summary[j].Category then
  35.       begin
  36.         Summary[j].Total := Summary[j].Total + Data[i].Value;
  37.         Break;
  38.       end;
  39.     end;
  40.   end;
  41.  
  42.   // Output results
  43.   writeln('Category', #9, 'Total');
  44.   for i := 1 to Length(Summary) do
  45.   begin
  46.     writeln(Summary[i].Category, #9, Summary[i].Total);
  47.   end;
  48. end.
You probably want something more advanced, which can be done with rtti and generics or sql query.
Principle is the same, though: summarize arbitrary tabular data based on arbitrary fields in a structure.

« Last Edit: October 22, 2024, 06:40:49 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: Can we create pivotable
« Reply #2 on: October 22, 2024, 07:08:46 am »
Thanks sir.

how to do it in fpspreadsheet .

Thaddy

  • Hero Member
  • *****
  • Posts: 16158
  • Censorship about opinions does not belong here.
Re: Can we create pivotable
« Reply #3 on: October 22, 2024, 07:18:34 am »
Ask wp, but I guess there is something.
If I smell bad code it usually is bad code and that includes my own code.

wp

  • Hero Member
  • *****
  • Posts: 12459
Re: Can we create pivotable
« Reply #4 on: October 22, 2024, 10:54:41 am »
There are no plans to add this feature to fpspreadsheet.

Packs

  • Sr. Member
  • ****
  • Posts: 366
Re: Can we create pivotable
« Reply #5 on: October 22, 2024, 11:53:17 am »
OK.

 

TinyPortal © 2005-2018