I'm trying to read in cell values, and in some cases the cell is defined by a formula and I'm reading in the formula, but I'd like to get the value.

For example, the formula is: =CONCATENATE(G2," ",D2," of ",E2)

but I'd like the 'rendered' cell, which is 1275x1200h Cost of production

Is there a way to tell PHPExcel to take the calculated value, rather than the 'raw' formula?

Thanks

Comments

wadmiraal’s picture

Status: Active » Closed (duplicate)

Yes, but it does not provide this out of the box. The thing is that it's a bit tricky for PHPExcel to determine when to fetch the calculated value (what you're after) and when to fetch the text value (what it does by default).

You could just implement the import hook and listen to any cell that's been imported ($op = 'post cell'). Then, you could check the string value of $data (ex: regular expression) and if you know this is a cell you want to process, just call getCalculatedValue() on $phpexcel (which will contain the current cell) to alter $data and get what you want.

Example:

function mymodule_phpexcel_import($op, &$data, $phpexcel, $options, $column = NULL, $row = NULL) {
  switch ($op) {
    case 'post cell':
      if (preg_match('/CONCATENATE/', $data)) {
        $data = $phpexcel->getCalculatedValue();
      }
      break;
  }
}

I haven't checked the code, but it should work. Let me know.

This is a duplicate of #1887652: Support for getCalculatedValue(), so I'll close it.

gaele’s picture

Thanks, this seems to work. At least on MS files.

However if I let this run on an xls or xlsx file created by LibreOffice it throws this error:

PHPExcel_Calculation_Exception: Data!C6 -> Data!C6 -> Data!C6 -> Formula Error: An unexpected error occured in PHPExcel_Cell->getCalculatedValue() (line 298 of /sites/all/libraries/PHPExcel/Classes/PHPExcel/Cell.php).

(Actually I used an xlsx file created by MS Office, read by and changed in LibreOffice, and than saved as either xls or xlsx by LibreOffice).

Do you have any idea of how to solve or circumvent this? Does PHP(Excel) have a method to check if a file is created by LibreOffice?

gaele’s picture

Title: Get value rather than forumula » Get value rather than formula
Version: 7.x-3.6 » 7.x-3.7
Issue summary: View changes
Status: Closed (duplicate) » Active
wadmiraal’s picture

I have no idea, I'm sorry. I've never run into that error before. Have you checked the PHPExcel library issue queue ? Maybe someone else has had the same problem.

gaele’s picture

Status: Active » Closed (fixed)

Turned out the error was already in the original file.
So the code above works fine.
Thanks for this nice module, BTW.