PHPOffice / PHPExcel

ARCHIVED
Other
11.46k stars 4.19k forks source link

Strange indirection issue #573

Open otstrel opened 9 years ago

otstrel commented 9 years ago

Hello,

I'm trying to implement simple template engine which requires indirection feature - ability to set cell value based on some other cell, kind of formula implemented in my code. I stumbled upon pretty strange behavior when setValue call on one cell object changes value of two cells. I narrowed it down to the following piece of code:

$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load("template.xls");

$cell=$objPHPExcel->getActiveSheet()->getCell('C3');
$target=$cell->getValue();
$data=$objPHPExcel->getActiveSheet()->getCell($target)->getValue();
$data='***'.$data;
$cell->setValue($data);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('bug.xls');

Excel files can be found here, template.xls is a, well, template, expected.xls is what I expect to see after I run above code with 1.8.1 and bug.xls is what I'm getting instead. Looks like some weird caching issue to me but hopefully that's just me missing something pretty obvious here.

Regards,

Roman

MarkBaker commented 9 years ago

This "bug" is impossible to fix without changing PHPExcel in such a way that cell caching is completely impossible, with all the memory overheads that would create..... $cell is only a pointer to a current "active" cell. In calling getCell() again to read a different cell, you are changing the "active" cell, so that pointer is now pointing to a different cell; so when you set the value for $cell you're changing the value for the new "active" cell

MarkBaker commented 9 years ago

And before you ask why this isn't explained in the docs..... people reading those docs often have enough difficulty understanding the difference PHP dates and Excel dates, or between workbooks and worksheets.... the docs try to keep things as simple as possible

otstrel commented 9 years ago

Thank you for quick response, I really appreciate it. Single active cell makes perfect sense, my code works as expected now. That's a relief!

What I'm still not getting is how single call to setValue changes two cells at once. If there is a single active cell which is being changed by every getCell call then only target cell should be changed by setValue call in my code, shouldn't it?

Regarding documentation I totally welcome your approach. With your amazing activity on StackOverflow it is much easier to start quickly and then search for whatever I come to along the way. I've learned quite a bit from your detailed answers there, thank you very much for these!

MarkBaker commented 9 years ago

It shouldn't change two cells at once, it's something I need to investigate because I've never come across it happening before