WPCubed Product Support

About table.GetObjList - Calculating fields with WPReporter

Taken for WPReporter:
{ This is only a dem to show how values on a page can be calculated. To
calculate values in a table you can use
table.GetObjList }
// We don't want to use this in the first editor!

Hi there,

  1. I am looking forward to see a demo of table.GetObjList function, cause I cannot get it to work properly.
  2. I cant find good enough information on how to use formulas with the Report band properies - Will it be possible to sum fields at runtime?
  3. What I am dealing with right now, is I am getting some data from a database, these I want to sum at every line like this:

Line 1: <<amount>> <<price>> [<<amount>> * <<price>>]
Line 2: <<amount>> <<price>> [<<amount>> * <<price>>]
Line 3: <<amount>> <<price>> [<<amount>> * <<price>>]
Line 4: [sum of line 1+2+3]

amount and price comes from the database, last one is calculated, and I want the last one to be calculated by WPRepporter not by database.

Thank you very much, keep up the good work - cause it is good work!

Dennis


Lincoln Cosmopolitan specifications

#1 ✎ ♥ 0
7.2k Views 2 links 2 users read 6 min
support

Hi,

thanks for the compliment.

The GetObjList is a one of the more special procedures:
procedure GetObjList(mask: TWPTextObjTypes; List: TList; LastChildPar: TParagraph = nil; RequiredName: string= '');

This procedure collects all objects of a certain type with a certain name which are used in this paragraph or any of its children paragraphs including the child paragraph 'LastChildPar'.

You can use it for tables but also for table rows, for example to list all merge fields in a table row.

In your case, "I want the last one to be calculated by WPRepporter not by database" the best solution is to add a field which is not in the database and provide its contents trough the GetText event. You have to sum this value up in your code, set it to 0 when you create the query and sum it up for each new row.

WPTools would be able to calculate this sum at runtime in the editor using the WPFormualInterface. But this is more an editing feature (I useit to calculate invoices), not for WPReporter. Here usually static texts are used.

To have a sum with the WPFormulaInterface apply a certain NAME to all cells which should be sumed up. Use the same name as 'command' in the one cell which should display this sum. The goof thing as that this kind of relations are not broken by inserting rows.

The cells with the NAME can also be calculated- Here commands such as "left(2)*left(1)" would be possible - that would produce the same result as you example. Only the columns ammount and price are editable. The demo WPReporter\TableCalc shows how this works.

But as said - with WPReporter better use fixed values.

Julian Ziersch
[/b]

#2 ♥ 0

Can you explain this a bit better:
You wrote: To have a sum with the WPFormulaInterface apply a certain NAME to all cells which should be sumed up. Use the same name as 'command' in the one cell which should display this sum.

What do you mean about 'command', is this a speciel way to create a mergefield?

What can I write in start code, prepare code and end code in the formula editor.

How should I apply the formula?

The reason for asking is that I cant find information on this great issue.
Is there an example or a better description for using the formula tab somewhere?

Thanks
Dennis


health store

#3 ✎ ♥ 0
support

What is meant was very simple.
create a varianble called
sum : Extended;
When you start the query set this to 0
for each row add a certain field

and pass the value of this variable when a certain verianble name is used in a merge field. This does NOT use StartCode/EndCode etc. I think it is much easier to use and much more reliable.

The WPFormulaInterface cannot be used with WPReporter. The code is from the TableCalc demo, it shows how to create an invoice:

procedure TWPTableCalc.InvoiceDemoClick(Sender: TObject);var par : TParagraph;    obj : TWPTextObj;begin  WPRichText1.Clear;  WPRichText1.CheckHasBody;  par := WPRichText1.ActiveParagraph;  par.SetText('This are the ordered products:');  par.ASet(WPAT_ParProtected,1);  par.ASetBorderFlags(WPBRD_DRAW_Bottom);  // ---------------------------------------------------------------------------  par := WPRichText1.TableAdd(7,7,[wptblActivateBorders,wptblAppendTableAtEnd],nil, InvoiceDemoCell);  // ---------------------------------------------------------------------------  par := WPRichText1.ActiveText.AppendPar(nil, par);  par := WPRichText1.ActiveText.AppendPar(nil, par);  par.SetText('Please pay ');  obj := par.AppendNewObject(wpobjTextObject,false, false);  obj.Name := 'CALC';  obj.Source := 'PAR_TOTAL';  obj.Params := '???';  // ---------------------------------------------------------------------------  WPRichText1.RecalcText(true,true);end;

This code uses this callback routine to fill in the cell text and properties:

procedure TWPTableCalc.InvoiceDemoCell(RowNr, ColNr: Integer; par: TParagraph);
const prods : array[1..5] of string =
 ( 'Cool', 'Master', 'Hummer', 'High Performace', 'Better' );
begin
  // Set the widths of the rows
  case ColNr of
       1 : par.ASet(WPAT_COLWIDTH_PC, 500);  // % * 100 !
       2 : par.ASet(WPAT_COLWIDTH_PC, 2500);
       else
       begin
          par.ASet(WPAT_COLWIDTH_PC, 1400);
          par.ASet(WPAT_Alignment, Integer(paralRight));
       end;
  end;

  // Set the text and the cell names and commands

  if RowNr=1 then  // Header Row ------------------------------
  begin
     case ColNr of
       1 : ;
       2 : par.SetText('Product');
       3 : par.SetText('Price');
       4 : par.SetText('Amount');
       5 : par.SetText('net');
       6 : par.SetText('+VAT');
       7 : par.SetText('total');
     end;
     par.ASetColor(WPAT_FGColor, $A0A0A0);
     par.ASet(WPAT_ParProtected,1);
     par.ASet(WPAT_Alignment, Integer(paralCenter));
  end else
  if RowNr=7 then // Footer Row ------------------------------
  begin
     par.ADel(WPAT_BorderWidth); // Delete the border width for ALL linese
     par.ASet(WPAT_BorderWidthT, 40); // ANd set the top line to 40 twips
     par.ASetAdd( WPAT_BorderFlags, WPBRD_DRAW_Top);  // Add a flag!
     // par.ParentRow.ASet(WPAT_BoxMinHeight, WPCentimeterToTwips(1.5));
     par.ASet(WPAT_SpaceBefore, WPCentimeterToTwips(0.3));
     par.ASet(WPAT_SpaceAfter, WPCentimeterToTwips(0.3));
     par.ASet(WPAT_ParProtected,1);
     case ColNr of
       1 : ;
       2 : ;
       3 : ;
       4 : ;
       5 : begin
             par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_NET');
           end;
       6 : begin
              par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_VAT');
           end;
       7 : begin
              par.ASetStringProp(WPAT_PAR_COMMAND, 'PAR_TOTAL');
              par.ASetCharStyle(true, WPSTY_BOLD);
           end;
     end;

  end else  // Data Rows ------------------------------
  begin
    case ColNr of
       1 : begin
              par.SetText(IntToStr(RowNr-1));
              par.ASet(WPAT_ParProtected,1);
           end;
       2 : par.SetText(prods[RowNr-1]);
       3 : par.SetText(IntToStr(Random(1000)+1));
       4 : par.SetText(IntToStr(Random(3)+1));
       5 : begin  // we multipy the count with the price to get the net amount
             par.ASetStringProp(WPAT_PAR_COMMAND, 'left(2)*left(1)');
             par.ASetStringProp(WPAT_PAR_NAME, 'PAR_NET');
             par.ASet(WPAT_ParProtected,1);
           end;
       6 : begin // we multiply the net with 0.16 to get the VAT
              par.ASetStringProp(WPAT_PAR_COMMAND, 'left(1)*0.16');
              par.ASetStringProp(WPAT_PAR_NAME, 'PAR_VAT');
              par.ASet(WPAT_ParProtected,1);
           end;
       7 : begin // we add the net amout + the VAT to get the total
              par.ASetStringProp(WPAT_PAR_COMMAND, 'left(2)+left(1)');
              par.ASetStringProp(WPAT_PAR_NAME, 'PAR_TOTAL');
              par.ASet(WPAT_ParProtected,1);
           end;
     end;
  end;
end;

You see how par.ASetStringProp is used to set the command and the name.

In this example using the text 'PAR_TOTAL' as command of a certain table cell will fill in the sum of all cells which have 'PAR_TOTAL' as their name.

#4 ♥ 0
This category is archived. No new posts are possible.