Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php excel formula not running in saved spreadsheet

Tags:

php

phpexcel

I'm using php excel codeplex, I'm filling a cell like this:

$objWorksheet->setCellValue('B12', "='Other sheet'!D38");

I save the file successfully, when I open it the formula is there, but it doesn't render the calculated value. If I copy and paste the formula to another cell it runs ok, so, it's not a problem of the formula's syntax. How can I force the formula to execute before saving? I have tried:

\PHPExcel_Calculation::getInstance($objPHPExcel)->disableCalculationCache();
\PHPExcel_Calculation::getInstance($objPHPExcel)->clearCalculationCache();

Without success...

like image 869
Lucia Avatar asked Dec 18 '14 15:12

Lucia


People also ask

Why is my Excel formula not running?

The most common reason for an Excel formula not calculating is that you have inadvertently activated the Show Formulas mode in a worksheet. To get the formula to display the calculated result, just turn off the Show Formulas mode by doing one of the following: Pressing the Ctrl + ` shortcut, or.

Why do my Excel formulas not calculating until I save?

Go to File, then Options, then Formulas to see the same setting options in the Excel Options window. Under the Manual Option, you'll see a checkbox for recalculating the workbook before saving, which is the default setting.

How do you get Excel to automatically run formulas?

On the Formulas tab, in the Calculation group, click Calculation Options, and then click Automatic.

How do I permanently save a formula in Excel?

1. Select the formula cells you need to make them permanent, then press the Ctrl + C keys simultaneously to copy them. Now the formulas in selected cells are replaced with corresponding results immediately.


Video Answer


2 Answers

This is a sample code to solve the problem:

$spreadsheet = new \PHPExcel();
$writer = new \PHPExcel_Writer_Excel2007($spreadsheet);

//Do things with the $spreadsheet

//This is the solution, do it before saving
$writer->setPreCalculateFormulas(); 
$writer->save($saving_filepath);
like image 94
Lucia Avatar answered Sep 28 '22 10:09

Lucia


I added this code and it is working for me.

PHPExcel_Calculation::getInstance($excelObj)->disableCalculationCache();
PHPExcel_Calculation::getInstance($excelObj)->clearCalculationCache();
$writer = PHPExcel_IOFactory::createWriter($excelObj, 'Excel2007');

$writer->setPreCalculateFormulas(true);
$writer->save($output_file);
like image 45
van hoai Avatar answered Sep 28 '22 10:09

van hoai