Recent

Author Topic: Printing formulas  (Read 14902 times)

HGabor

  • New Member
  • *
  • Posts: 17
Printing formulas
« on: April 27, 2012, 04:37:48 pm »
Is there a way to get formulas working without loading or saving a worksheet?

It would be really handy if I could work with a temporary spreadsheet and then print from it with LazReport. So I do not need the sheet nor before, nor after - so it may be an Excel or anything else too -, I just want to spare myself of writing an expression parser if FPSpreadSheet contains one. I'm not sure that I will be able to save my sheets later to get the proper sheetstyle (Excel).


felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Printing formulas
« Reply #1 on: April 27, 2012, 04:50:52 pm »
There is no code currently in FPSpreadsheet to resolve formulas, but patches are welcome =) There are 2 kinds of formulas in FPSpreadsheet: string representation of formulas and RPN representation. We need two things:

1->Converter from string formula to RPN prepresentation
2->A function which resolves the value of a RPN formula. RPN is simple to resolve, the other part of the solution would be the string->RPN convertor

About printing I have two ideas:

1->Write a new module which can print spreadsheets
2->You can copy the spreadsheet to a TStringGrid, I don't know if TStringGrid has something about printing ... maybe

I don't use LazReport, so no idea here.

By the way, I split your post to a new topic. Please always start new threads for new topics.
« Last Edit: April 27, 2012, 04:52:42 pm by felipemdc »

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #2 on: April 27, 2012, 05:42:03 pm »
It does not let me answer.

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #3 on: April 27, 2012, 05:42:40 pm »
Well for the third time again.  >:(

Briefly again. I know how to print from LazReport, because I need only the datas, not the format itself. Any kind of matrix will do.
I have to collect datas from a MySQL database then feed them into a LazReport form, which could be done via its OnGetValue event.

My problem is that I will have to make some sums on the collected datas before print them and I thought that a spreadsheet could
do me this favor. :)

Unfortunately I'm continouosly lack of time, this is why I am searching components instead of writing them.

There is a freeware component TFatExpression, maybe you could put them into FPSpreadsheet to resolve formulas. It is working under Lazarus, there is two or three minor errors that have to be corrected to compile it (typecasts as I remember well). I will use it too.

http://www.delphipages.com/comp/tfatexpression-3840.html

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Printing formulas
« Reply #4 on: April 27, 2012, 06:39:47 pm »
What kinds of formulas are you executing exactly? =X+Y or what else?

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #5 on: April 27, 2012, 06:48:03 pm »
Yes, exactly. I needed something like '=B2+B3+B24'.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Printing formulas
« Reply #6 on: April 27, 2012, 06:51:53 pm »
1->Converter from string formula to RPN prepresentation
2->A function which resolves the value of a RPN formula. RPN is simple to resolve, the other part of the solution would be the string->RPN convertor

These are both available in unit symbolic
 

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #7 on: April 28, 2012, 01:37:46 am »
I made a workaround based on TFatExpression. Because it can handle infix formulas, I am using them in the table and simply change the cell names to their value in a string which finally I give to the FatExpression component. If the value starts with "=" then of course I call recursively the algorithm again, so finally I get a string with the fomula and numbers.

Not really correct, because I am giving 0 if there is no value in a cell, but it will do, since I won't use nor multiplication, nor division. Anyway, this  can be corrected too to 1.

Just as an information if someone is interested, TFatExpression has an error: it cannot handle only scientific floats, but it can be corrected in the source.
By line 250 in function GetTokenType
 
Code: [Select]
if (Error = 0) then
      Result := ttNumeric
has to be change to
if (Error = 0) or (S = DefaultFormatSettings.DecimalSeparator) then
      Result := ttNumeric

Later in function TExpParser.ReadNextToken by line 325

Code: [Select]
before the line

Part := Part + Ch; 

the following code should be added:

      if (FirstType = ttNumeric) and (Ch = DefaultFormatSettings.DecimalSeparator)
         and (DefaultFormatSettings.DecimalSeparator <> '.') then Ch := '.'; 

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Printing formulas
« Reply #8 on: April 28, 2012, 01:15:49 pm »
I made a workaround based on TFatExpression. Because it can handle infix formulas, I am using them in the table and simply change the cell names to their value in a string which finally I give to the FatExpression component. If the value starts with "=" then of course I call recursively the algorithm again, so finally I get a string with the fomula and numbers.

One can pass an expression to symbolic, and get back a list of variables/constants that are undefined. You can then define these, and evaluate the numeric result. And symbolic is already part of FPC.

It has some missing features though:
- no equation support  so only x+y+z  form not   x=y+z+s
- no user definable functions
- no boolean logic.

 

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #9 on: April 29, 2012, 01:21:34 pm »
Quote
One can pass an expression to symbolic, and get back a list of variables/constants that are undefined. You can then define these, and evaluate the numeric result. And symbolic is already part of FPC.

To be honest, I was messing around Symbolic about half an hour, but still found less painfull to give an infix expression to TFatExpression.Text and then read its Value property. It takes about 5-10 minutes to install it under Lazarus.
Not to mention that even if there are no built in boolean logic, it is easy to define functions for such purpose using its feEvaluate event. I'm not sure that it is useful, but seems possible, at least as single functions like "GT(x,y)" (gives back 1 if x>y, 0 otherwise) or something like this.

I am now writing my report-generator and as I expected,  tsWorkSheet may be used on a DataModule.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Printing formulas
« Reply #10 on: April 29, 2012, 01:39:33 pm »
Quote
One can pass an expression to symbolic, and get back a list of variables/constants that are undefined. You can then define these, and evaluate the numeric result. And symbolic is already part of FPC.

To be honest, I was messing around Symbolic about half an hour, but still found less painfull to give an infix expression to TFatExpression.Text and then read its Value property. It takes about 5-10 minutes to install it under Lazarus.

The focus of symbolic always has been to be able to quickly draw graphs, and also the parser errorhandling has received quite some attention (you can get an idea where it went wrong). That makes it less easy for quick and dirty and expressive usage. But it is there, and installed, and if it runs _under_ other code, that all should be relatively easy to deal with.

I have an half finished version with support for booleans and user functions, but its development was stalled. I had hoped to finish it in my employer's time, but the project that needed it kept getting postponed.
 

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #11 on: April 29, 2012, 02:19:13 pm »
Quote
That makes it less easy for quick and dirty and expressive usage. But it is there, and installed,

When I read about Symboilc, I already had TFatExpression installed. Google didn't drop me a line about Symbolic.
I think it would be a good choice to use Symbolic as the built in expression parser in FPSpreadsheet - if someone implemented into it -, but now I am working on a report-generator and I am searchiing for easy solutions because of lack of time.

To be honest, I couldn't get working as I wanted Symbolic in half an hour - even if it is installed - while in such time I was able to download, install, and implement the boolean logic functions to TFatExpression. Later I discovered the problem of the decimal separator, but it was about ten minutes to correct it.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Printing formulas
« Reply #12 on: April 29, 2012, 05:57:25 pm »
To be honest, I couldn't get working as I wanted Symbolic in half an hour - even if it is installed

Strange. It comes with examples (in <fpcdir>/packages/symbolic/examples)

HGabor

  • New Member
  • *
  • Posts: 17
Re: Printing formulas
« Reply #13 on: April 29, 2012, 07:07:15 pm »
I think I had to spend more time to learn it. I searched for something where I can load an infix formula and simply get the counted value (or an error message).  Symbolic seems much more complicated that this.
I mentioned that I had something that does exactly the thing that I was looking for already installed, I only have a look at Symbolic because I have to use Lazarus on several machines and it is a bit tiring to install everything on my desktop computer, then on my laptop and finally on the laptop again with a virtual drive with Linux.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11459
  • FPC developer.
Re: Printing formulas
« Reply #14 on: April 29, 2012, 09:37:59 pm »
I think I had to spend more time to learn it. I searched for something where I can load an infix formula and simply get the counted value (or an error message).  Symbolic seems much more complicated that this.
I mentioned that I had something that does exactly the thing that I was looking for already installed, I only have a look at Symbolic because I have to use Lazarus on several machines and it is a bit tiring to install everything on my desktop computer, then on my laptop and finally on the laptop again with a virtual drive with Linux.

Excerpt of demo easyevalexample.pp:

Code: [Select]
Uses Symbolic;
var s : ansistring
begin
  s:='(5+5+10)*2';
  writeln(s,'=',QuickEvaluate(s,[],[]):10:1);
end;

Added later: Afaik this (simplest) example is fairly recent though. If you used an older Free Pascal like 2.4.4/0.9.30.2 or older, it might not have been in it. It should be in 2.6.0/0.9.30.4 though. So maybe that explains it.
« Last Edit: April 29, 2012, 09:42:03 pm by marcov »

 

TinyPortal © 2005-2018