You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@poi.apache.org by Filip Defoort <fi...@cirquedigital.com> on 2009/04/09 02:48:35 UTC

SUMIFS function

Hi,

I was wondering if anyone has implemented HSSF support for the SUMIFS
function ? The SUMIF function is there and works fine, but when using
SUMIFS, I'm getting an exception.

Alternatively, is it possible to insert a "raw" function via HSSF ? I really
don't need HSSF to understand/be able to compute the function result, I just
need to be able to insert the formula and save the spreadsheet.

Thanks much for any hints,
- Filip

Re: SUMIFS function

Posted by Josh Micich <jo...@gmail.com>.
On Mon, Apr 13, 2009 at 4:10 PM, Filip Defoort <fi...@cirquedigital.com>
wrote:
>So... could I create a add-in function that is uses SUMIFS internally or
>would that just cause the same problem (not sure if the user defined
>functions require to be parsed) ?
This might work.  The main problem POI has with parsing non-'built-in'
functions is creation of the DEFINEDNAME record.  These are required for
'add-in' functions and local VB functions.  As far as I understand, POI
*can* parse formulas which have calls to non-'built-in' functions, when the
DEFINEDNAME record already exists.  If you save a copy of a spreadsheet with
an existing call to your wrapper 'add-in' func or or local VB func, POI
should be able to parse new formulas which use the same functions.

Note - this hack won't work when directly parsing SUMIFS, for 3 reasons:
 1 - the required defined name name is different ("_xlfn.sumifs")
 2 - the formula parser currently expects names to start with a letter
character
 3 - POI gets some option flag bits wrong and also gets the operand token
classes wrong for some of the parameters
I'm not sure which part of reason (3) is important - I found those
differences using BiffViewer when Excel fixed a test file I generated from
POI.


>I was tempted to take a stab at implementing SUMIFS, but it seems to be
>quite a bit more complex than just copying Sumif.java and adjusting it...
Unfortunately, writing an implementation for Sumifs.java would be the
easiest of all the changes, but you probably don't even need if you are not
using POI to evaluate SUMIFS.  If you *do* need POI evaluation, the biggest
effort is going to be in writing the general code to invoke this category of
function.

Re: SUMIFS function

Posted by Filip Defoort <fi...@cirquedigital.com>.
On Mon, Apr 13, 2009 at 2:38 PM, Josh Micich <jo...@gmail.com> wrote:

> What you are trying to do is a little unusual - adding an Excel 2007
> feature
> in an Excel 97 (HSSF/BIFF8/.xls) file.  Having said that, Excel *can* do
> this with a little coaxing, and POI *will* need to handle post-BIFF8
> features within XSSF.


Ha, got it. I didn't realize SUMIFS was a 2007 feature. Sigh.

>
>
> It seems like Excel encodes SUMIFS using a NamePtg that points to a defined
> name called "_xlfn.sumifs".  This technique is different again to that used
> for 'add-in' functions (which POI partially supports).  To make this all


So... could I create a add-in function that is uses SUMIFS internally or
would that just cause the same problem (not sure if the user defined
functions require to be parsed) ?

New contributors and patches are always welcome.
>

I was tempted to take a stab at implementing SUMIFS, but it seems to be
quite a bit more complex than just copying Sumif.java and adjusting it...

Thanks for the info!
- Filip

Re: SUMIFS function

Posted by Josh Micich <jo...@gmail.com>.
What you are trying to do is a little unusual - adding an Excel 2007 feature
in an Excel 97 (HSSF/BIFF8/.xls) file.  Having said that, Excel *can* do
this with a little coaxing, and POI *will* need to handle post-BIFF8
features within XSSF.

So far, POI doesn't support much more than the standard 'built-in' functions
of Excel 97. That's why you are getting the formula parser error. There is
no way around formula parsing - the Ptg tokens also need to be encoded
somehow (and in XSSF, formula parsing is still required as a syntax check).

It seems like Excel encodes SUMIFS using a NamePtg that points to a defined
name called "_xlfn.sumifs".  This technique is different again to that used
for 'add-in' functions (which POI partially supports).  To make this all
work, POI needs to:
 - recognise this category of function and get the metadata (needed for
operand class transformation of parameters)
 - find or add the appropriate DEFINEDNAME record (seems like some extra
flags (0x380) are required)

Some thought would also need to be given (in the HSSF world) to version
issues.  Even though Excel 2007 can save BIFF8 (.xls) files containing
SUMIFS(), it does so with warnings.  Apparently Excel 97 will fail to
evaluate those formulas.

If evaluation (by POI) is required, the formula evaluator will need changing
too.

New contributors and patches are always welcome.

Re: SUMIFS function

Posted by Filip Defoort <fi...@cirquedigital.com>.
Hi,

On Mon, Apr 13, 2009 at 12:24 AM, Joe Black <pe...@hotmail.com> wrote:

>
>
> I used this method to write a small program and it is working.
>
>
The sample program is using SUMIF, not SUMIFS -- that's working fine.

With SUMIFS you get:

12:06:51.655[INFO ][               ]Formula: SUMIF(K8:K29, "<>0") +
SUMIFS(I8:I29, K8:K29, "=0", I8:I29, "<>0") + SUMIFS(H8:H29, K8:K29, "=0",
I8:I29, "=0")
show error:
org.apache.poi.ss.formula.FormulaParser$FormulaParseException: Name 'SUMIFS'
is completely unknown in the current workbook
        at
org.apache.poi.ss.formula.FormulaParser.function(FormulaParser.java:546)
        at
org.apache.poi.ss.formula.FormulaParser.parseFunctionReferenceOrName(FormulaParser.java:324)
        at
org.apache.poi.ss.formula.FormulaParser.parseSimpleFactor(FormulaParser.java:733)
        at
org.apache.poi.ss.formula.FormulaParser.percentFactor(FormulaParser.java:693)
        at
org.apache.poi.ss.formula.FormulaParser.powerFactor(FormulaParser.java:680)
        at
org.apache.poi.ss.formula.FormulaParser.Term(FormulaParser.java:982)
        at
org.apache.poi.ss.formula.FormulaParser.additiveExpression(FormulaParser.java:1099)
        at
org.apache.poi.ss.formula.FormulaParser.concatExpression(FormulaParser.java:1067)
        at
org.apache.poi.ss.formula.FormulaParser.comparisonExpression(FormulaParser.java:1024)
        at
org.apache.poi.ss.formula.FormulaParser.unionExpression(FormulaParser.java:1003)
        at
org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:1125)
        at
org.apache.poi.ss.formula.FormulaParser.parse(FormulaParser.java:192)
        at
org.apache.poi.hssf.model.HSSFFormulaParser.parse(HSSFFormulaParser.java:69)
        at
org.apache.poi.hssf.usermodel.HSSFCell.setCellFormula(HSSFCell.java:605)


So either I'd need a way to disable formula parsing -or- I'd need to add
support for SUMIFS in POI.

Thanks!
- Filip

RE: SUMIFS function

Posted by Joe Black <pe...@hotmail.com>.
Hi,

I am using POI 3.5 beta 5.

I used this method to write a small program and it is working.

rgds,

> From: filipdef@cirquedigital.com
> To: user@poi.apache.org
> Subject: Re: SUMIFS function
> Date: Sun, 12 Apr 2009 23:56:48 -0700
> CC: user@poi.apache.org
> 
> 
> 
> On Apr 12, 2009, at 11:46 PM, Joe Black <pe...@hotmail.com> wrote:
> >
> > For example, I want to put the formula
> > =sumif($A$1:$A$7, $A11, $B$1:$B$7)
> > in cell B11, I can do like this
> >
> > row = sheet.createRow(10);
> > cell = row.createCell(1);
> > cell.setCellFormula("sumif($A$1:$A$7,$A11,$B$1:$B$7)");
> 
> Actually if you try putting sumifs in that way, it seems to throw an  
> error complaining about not knowing sumifs.
> >
> >
> > It will work. However if my interpretation of your "raw" function is  
> > not correct, apologies.
> 
> No, you're correct - getting something like the above to work is what  
> I'm trying to do.
> 
> Cheers,
> Filip
> >
> >
> > rgds,
> >
> >
> >
> >
> >> Date: Wed, 8 Apr 2009 17:48:35 -0700
> >> Subject: SUMIFS function
> >> From: filipdef@cirquedigital.com
> >> To: user@poi.apache.org
> >>
> >> Hi,
> >>
> >> I was wondering if anyone has implemented HSSF support for the SUMIFS
> >> function ? The SUMIF function is there and works fine, but when using
> >> SUMIFS, I'm getting an exception.
> >>
> >> Alternatively, is it possible to insert a "raw" function via HSSF ?  
> >> I really
> >> don't need HSSF to understand/be able to compute the function  
> >> result, I just
> >> need to be able to insert the formula and save the spreadsheet.
> >>
> >> Thanks much for any hints,
> >> - Filip
> >
> > _________________________________________________________________
> > More than messages–check out the rest of the Windows Live™.
> > http://www.microsoft.com/windows/windowslive/
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
> For additional commands, e-mail: user-help@poi.apache.org
> 

_________________________________________________________________
What can you do with the new Windows Live? Find out
http://www.microsoft.com/windows/windowslive/default.aspx

Re: SUMIFS function

Posted by Filip Defoort <fi...@cirquedigital.com>.

On Apr 12, 2009, at 11:46 PM, Joe Black <pe...@hotmail.com> wrote:
>
> For example, I want to put the formula
> =sumif($A$1:$A$7, $A11, $B$1:$B$7)
> in cell B11, I can do like this
>
> row = sheet.createRow(10);
> cell = row.createCell(1);
> cell.setCellFormula("sumif($A$1:$A$7,$A11,$B$1:$B$7)");

Actually if you try putting sumifs in that way, it seems to throw an  
error complaining about not knowing sumifs.
>
>
> It will work. However if my interpretation of your "raw" function is  
> not correct, apologies.

No, you're correct - getting something like the above to work is what  
I'm trying to do.

Cheers,
Filip
>
>
> rgds,
>
>
>
>
>> Date: Wed, 8 Apr 2009 17:48:35 -0700
>> Subject: SUMIFS function
>> From: filipdef@cirquedigital.com
>> To: user@poi.apache.org
>>
>> Hi,
>>
>> I was wondering if anyone has implemented HSSF support for the SUMIFS
>> function ? The SUMIF function is there and works fine, but when using
>> SUMIFS, I'm getting an exception.
>>
>> Alternatively, is it possible to insert a "raw" function via HSSF ?  
>> I really
>> don't need HSSF to understand/be able to compute the function  
>> result, I just
>> need to be able to insert the formula and save the spreadsheet.
>>
>> Thanks much for any hints,
>> - Filip
>
> _________________________________________________________________
> More than messages–check out the rest of the Windows Live™.
> http://www.microsoft.com/windows/windowslive/

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


RE: SUMIFS function

Posted by Joe Black <pe...@hotmail.com>.
Hi,

I think that if you just want to put a "hard coded formula" in a cell, it is possible.
I am not sure whether you meant that by "raw function"?

For example, I want to put the formula
=sumif($A$1:$A$7, $A11, $B$1:$B$7)
in cell B11, I can do like this

row = sheet.createRow(10);
cell = row.createCell(1);
cell.setCellFormula("sumif($A$1:$A$7,$A11,$B$1:$B$7)");

It will work. However if my interpretation of your "raw" function is not correct, apologies.

rgds,




> Date: Wed, 8 Apr 2009 17:48:35 -0700
> Subject: SUMIFS function
> From: filipdef@cirquedigital.com
> To: user@poi.apache.org
> 
> Hi,
> 
> I was wondering if anyone has implemented HSSF support for the SUMIFS
> function ? The SUMIF function is there and works fine, but when using
> SUMIFS, I'm getting an exception.
> 
> Alternatively, is it possible to insert a "raw" function via HSSF ? I really
> don't need HSSF to understand/be able to compute the function result, I just
> need to be able to insert the formula and save the spreadsheet.
> 
> Thanks much for any hints,
> - Filip

_________________________________________________________________
More than messages–check out the rest of the Windows Live™.
http://www.microsoft.com/windows/windowslive/

Re: SUMIFS function

Posted by MSB <ma...@tiscali.co.uk>.
I think - emphasise think - that your only option is to request an
enhancement from the developers as I cannot seem to find a work around.

>From what I have found through digging around the javadocs, each of Excel's
functions has a matching concrete class in the
org.apache.poi.hssf.record.formula.functions package. I would imagine that
is accompanied somewhere by a list of recognised function names and their
accompanying formats and that changes will have to be made in both areas to
support the SUMIFS function. If I am correct, SUMIFS was introduced in more
recent versions of Excel and has simply not been added into the API as no
one has yet requested it.


Filip Defoort wrote:
> 
> Hi,
> 
> I was wondering if anyone has implemented HSSF support for the SUMIFS
> function ? The SUMIF function is there and works fine, but when using
> SUMIFS, I'm getting an exception.
> 
> Alternatively, is it possible to insert a "raw" function via HSSF ? I
> really
> don't need HSSF to understand/be able to compute the function result, I
> just
> need to be able to insert the formula and save the spreadsheet.
> 
> Thanks much for any hints,
> - Filip
> 
> 

-- 
View this message in context: http://www.nabble.com/SUMIFS-function-tp22962586p22986839.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org