Reputation: 5520
I'm trying to figure out why result is not affected when using setCellValueByColumnAndRow
with PHPExcel.
Am I doing something wrong?
This first version sets the result to integer 0 (trying with two different approaches to fetch the calculated value)
//Version 1 - generating incorrect result (should 77*5 = 385, but is 0)
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, 1, '=A2*5' ); //A1
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 1, 1, 77 ); //A2
echo '<br /> Value A1= ' . $objPHPExcel->getActiveSheet()-
>getCellByColumnAndRow(0,1)->getValue();
echo '<br />Value A2= ' . $objPHPExcel->getActiveSheet()-
>getCellByColumnAndRow(1,1)->getValue();
echo '<br />Result=' . $objPHPExcel->getActiveSheet()-
>getCellByColumnAndRow(0,1)->getCalculatedValue();
echo '<br />Result=' . $objPHPExcel->getActiveSheet()->getCell("A1")-
>getCalculatedValue();
echo '<br /><br />';
This second version when using letters and characters to set values it works as expected:
//Version 2 - generating correct result (77*5 = 385)
$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->setCellValue( "A1", '=A2*5' ); //A1
$objPHPExcel->getActiveSheet()->setCellValue( "A2", 77 ); //A2
echo '<br />Value A1= ' . $objPHPExcel->getActiveSheet()->getCell("A1")-
>getValue();
echo '<br />Value A2= ' . $objPHPExcel->getActiveSheet()->getCell("A2")-
>getValue();
echo '<br />Result=' . $objPHPExcel->getActiveSheet()->getCell("A1")-
>getCalculatedValue();
echo '<br />Result=' . $objPHPExcel->getActiveSheet()-
>getCellByColumnAndRow(0,1)->getCalculatedValue();
Output version 1:
Value A1= =A2*5
Value A2= 77
Result=0
Result=0
Output version 2:
Value A1= =A2*5
Value A2= 77
Result=385
Result=385
Is this a bug in PHPEXcel or am I simply doing something wrong?
The reason why I want to use setCellValueByColumnAndRow
is that I have an array like this that is created from a post request and I want recalculate changed values of the array: (The examples with version1 and version2 above were for testing how it worked with PHPExcel and has nothing to do with this array)
array
0 =>
array (size=2)
1 => string '6000' (length=4)
2 => string '=A2*5' (length=5)
1 =>
array (size=2)
1 => string '60009' (length=5)
2 => string '=A2*5' (length=5)
2 =>
array (size=2)
1 => string '7000' (length=4)
2 => string '=A3*5' (length=5)
....
UPDATE:
This is not a duplicate PHPExcel setCellValueByColumnAndRow not writing data to spreadsheet, because that question had nothing to do with result of formula calculations. The actual values are set within my question into the cells, but when fetching the calculated value with getCellValueByColumnAndRow
it does not seem to work. I don't the see the connection between my question and the question that is flagged as possible duplicate. I've also changed the title of this question to make it more clear what the actual issue is.
Upvotes: 0
Views: 1960
Reputation: 5520
Strangely enough...
sometimes it's so easy... far to easy... I just mixed up rows and columns for some reason ):
Now it works with this code (as expected):
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, 1, '=A2*5' ); //A1
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, 2, 77 ); //A2
and NOT
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 0, 1, '=A2*5' ); //A1
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( 1, 1, 77 ); //A2
Upvotes: 1