setInstanceArrayReturnType(
Calculation::RETURN_ARRAY_AS_VALUE
);
$sheet = $workbook->getActiveSheet();
$sheet->setTitle('2010');
$sheet = $workbook->createSheet();
$sheet->setTitle('2013');
$sheet = $workbook->createSheet();
$sheet->setTitle('2016');
$sheet = $workbook->createSheet();
$sheet->setTitle('2019');
$sheet = $workbook->createSheet();
$sheet->setTitle('365');
foreach ($formulas as $values) {
$sheet = $workbook->setActiveSheetIndexByName($values[0]);
$sheet->setCellValue($values[1], $values[2]);
}
$sheet = $workbook->setActiveSheetIndexByName('2013');
$sheet->getStyle('A3:A5')->getNumberFormat()->setFormatCode('yyyy-mm-dd');
$sheet->getColumnDimension('A')->setAutoSize(true);
$condition0 = new Conditional();
$condition0->setConditionType(Conditional::CONDITION_EXPRESSION);
$condition0->addCondition('ABS(B3)<2');
$condition0->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$condition0->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_RED);
$condition1 = new Conditional();
$condition1->setConditionType(Conditional::CONDITION_EXPRESSION);
$condition1->addCondition('ABS(B3)>2');
$condition1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$condition1->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_GREEN);
$cond = [$condition0, $condition1];
$sheet->getStyle('B3:B5')->setConditionalStyles($cond);
$condition0 = new Conditional();
$condition0->setConditionType(Conditional::CONDITION_EXPRESSION);
$condition0->addCondition('ISOWEEKNUM(A3)<10');
$condition0->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$condition0->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_RED);
$condition1 = new Conditional();
$condition1->setConditionType(Conditional::CONDITION_EXPRESSION);
$condition1->addCondition('ISOWEEKNUM(A3)>40');
$condition1->getStyle()->getFill()->setFillType(Fill::FILL_SOLID);
$condition1->getStyle()->getFill()->getStartColor()->setARGB(Color::COLOR_GREEN);
$cond = [$condition0, $condition1];
$sheet->getStyle('A3:A5')->setConditionalStyles($cond);
$sheet->setSelectedCell('B1');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($workbook, 'Xlsx');
$oufil = File::temporaryFilename();
$writer->save($oufil);
$file = "zip://$oufil#xl/worksheets/sheet1.xml";
$contents = (string) file_get_contents($file);
self::assertStringContainsString('_xlfn.MODE.SNGL({5.6,4,4,3,2,4})4', $contents);
self::assertStringContainsString('_xlfn.MODE.SNGL({"x","y"})#N/A', $contents);
$file = "zip://$oufil#xl/worksheets/sheet2.xml";
$contents = (string) file_get_contents($file);
self::assertStringContainsString('_xlfn.ISOWEEKNUM("2019-12-19")51', $contents);
self::assertStringContainsString('_xlfn.SHEET("2019")', $contents);
self::assertStringContainsString('_xlfn.ISOWEEKNUM(A3)+WEEKNUM(A4)+_xlfn.ISOWEEKNUM(A5)77', $contents);
self::assertStringContainsString('ABS(B3)<2ABS(B3)>2', $contents);
self::assertStringContainsString('_xlfn.ISOWEEKNUM(A3)<10_xlfn.ISOWEEKNUM(A3)>40', $contents);
$file = "zip://$oufil#xl/worksheets/sheet3.xml";
$contents = (string) file_get_contents($file);
self::assertStringContainsString('_xlfn.SWITCH(WEEKDAY("2019-12-22",1),1,"Sunday",2,"Monday","No Match")Sunday', $contents);
self::assertStringContainsString('_xlfn.SWITCH(WEEKDAY("2019-12-20",1),1,"Sunday",2,"Monday","No Match")No Match', $contents);
$file = "zip://$oufil#xl/worksheets/sheet4.xml";
$contents = (string) file_get_contents($file);
self::assertStringContainsString('_xlfn.CONCAT("The"," ","sun"," ","will"," ","come"," ","up"," ","tomorrow.")The sun will come up tomorrow.', $contents);
$file = "zip://$oufil#xl/worksheets/sheet5.xml";
$contents = (string) file_get_contents($file);
self::assertStringContainsString('_xlfn._xlws.SORT({7;1;5})1', $contents);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$rdobj = $reader->load($oufil);
unlink($oufil);
$calculation = Calculation::getInstance($rdobj);
$calculation->setInstanceArrayReturnType(
Calculation::RETURN_ARRAY_AS_VALUE
);
foreach ($formulas as $values) {
$sheet = $rdobj->setActiveSheetIndexByName($values[0]);
self::assertEquals($values[3], $sheet->getCell($values[1])->getValue());
if ($values[4] !== null) {
self::assertEquals($values[4], $sheet->getCell($values[1])->getCalculatedValue());
}
}
$sheet = $rdobj->setActiveSheetIndexByName('2013');
$cond = $sheet->getConditionalStyles('A3:A5');
self::assertEquals('ISOWEEKNUM(A3)<10', $cond[0]->getConditions()[0]);
self::assertEquals('ISOWEEKNUM(A3)>40', $cond[1]->getConditions()[0]);
$cond = $sheet->getConditionalStyles('B3:B5');
self::assertEquals('ABS(B3)<2', $cond[0]->getConditions()[0]);
self::assertEquals('ABS(B3)>2', $cond[1]->getConditions()[0]);
}
}