 1c6dd8923e
			
		
	
	
		1c6dd8923e
		
	
	
	
	
		
			
			* Handle Error in Formula Processing Better for Xls
When there is an error writing a formula to an Xls file,
which seems to happen when a defined name is part of a formula,
the cell is currently left blank. A better result would be to
write the calculated value of the formula.
* Making Changes Suggested in Review
Per comment from Mark Baker:
   1. Made return codes from writeFormula into constants.
   2. Skipped redundant call to getCellValue when possible.
   3. Added support for bool type, adding additional tests
      for bool and string.
Per comment from PowerKiki:
   1. Used standardized convention for assigning file name in test.
      Since before this change, save would throw Exception, I kept
      the unlink for the file in tearDown.
		
	
			
		
			
				
	
	
		
			46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			46 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace PhpOffice\PhpSpreadsheetTests\Writer\Xls\Workbook;
 | |
| 
 | |
| use PhpOffice\PhpSpreadsheet\IOFactory;
 | |
| use PhpOffice\PhpSpreadsheet\NamedRange;
 | |
| use PhpOffice\PhpSpreadsheet\Shared\File;
 | |
| use PHPUnit\Framework\TestCase;
 | |
| 
 | |
| class FormulaErrTest extends TestCase
 | |
| {
 | |
|     public function tearDown()
 | |
|     {
 | |
|         $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
 | |
|         if (file_exists($filename)) {
 | |
|             unlink($filename);
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     public function testFormulaError()
 | |
|     {
 | |
|         $obj = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
 | |
|         $sheet0 = $obj->setActiveSheetIndex(0);
 | |
|         $sheet0->setCellValue('A1', 2);
 | |
|         $obj->addNamedRange(new NamedRange('DEFNAM', $sheet0, 'A1'));
 | |
|         $sheet0->setCellValue('B1', '=2*DEFNAM');
 | |
|         $sheet0->setCellValue('C1', '=DEFNAM=2');
 | |
|         $sheet0->setCellValue('D1', '=CONCAT("X",DEFNAM)');
 | |
|         $writer = IOFactory::createWriter($obj, 'Xls');
 | |
|         $filename = tempnam(File::sysGetTempDir(), 'phpspreadsheet-test');
 | |
|         $writer->save($filename);
 | |
|         $reader = IOFactory::createReader('Xls');
 | |
|         $robj = $reader->load($filename);
 | |
|         $sheet0 = $robj->setActiveSheetIndex(0);
 | |
|         $a1 = $sheet0->getCell('A1')->getCalculatedValue();
 | |
|         self::assertEquals(2, $a1);
 | |
|         $b1 = $sheet0->getCell('B1')->getCalculatedValue();
 | |
|         self::assertEquals(4, $b1);
 | |
|         $c1 = $sheet0->getCell('C1')->getCalculatedValue();
 | |
|         $tru = true;
 | |
|         self::assertEquals($tru, $c1);
 | |
|         $d1 = $sheet0->getCell('D1')->getCalculatedValue();
 | |
|         self::assertEquals('X2', $d1);
 | |
|     }
 | |
| }
 |