2016-08-31 16:15:54 +00:00
|
|
|
<?php
|
|
|
|
|
2017-10-29 08:39:42 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
|
2017-05-17 22:02:17 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
|
|
|
2017-10-01 08:48:59 +00:00
|
|
|
require __DIR__ . '/../Header.php';
|
2016-08-31 16:15:54 +00:00
|
|
|
|
|
|
|
// Create new Spreadsheet object
|
|
|
|
$helper->log('Create new Spreadsheet object');
|
2017-05-17 22:02:17 +00:00
|
|
|
$spreadsheet = new Spreadsheet();
|
2016-08-31 16:15:54 +00:00
|
|
|
|
|
|
|
// Add some data, we will use some formulas here
|
|
|
|
$helper->log('Add some data and formulas');
|
|
|
|
$spreadsheet->getActiveSheet()->setCellValue('A1', '=B1')
|
2018-01-28 06:59:38 +00:00
|
|
|
->setCellValue('A2', '=B2+1')
|
|
|
|
->setCellValue('B1', '=A1+1')
|
|
|
|
->setCellValue('B2', '=A2');
|
2016-08-31 16:15:54 +00:00
|
|
|
|
Improve Coverage for CSV (#1475)
I believe that both CSV Reader and Writer are 100% covered now.
There were some errors uncovered during development.
The reader specifically permits encodings other than UTF-8 to be used.
However, fgetcsv will not properly handle other encodings.
I tried replacing it with fgets/iconv/strgetcsv, but that could not
handle line breaks within a cell, even for UTF-8.
This is, I'm sure, a very rare use case.
I eventually handled it by using php://memory to hold the translated
file contents for non-UTF8. There were no tests for this situation,
and now there are (probably too many).
"Contiguous" read was not handle correctly. There is a file
in samples which uses it. It was designed to read a large sheet,
and split it into three. The first sheet was corrrect, but the
second and third were almost entirely empty. This has been corrected,
and the sample code was adapted into a formal test with assertions
to confirm that it works as designed.
I made a minor documentation change. Unlike HTML, where you never
need a BOM because you can declare the encoding in the file,
a CSV with non-ASCII characters must explicitly include a BOM
for Excel to handle it correctly. This was explained in the Reading CSV
section, but was glossed over in the Writing CSV section, which I
have updated.
2020-05-17 09:15:18 +00:00
|
|
|
Calculation::getInstance($spreadsheet)->cyclicFormulaCount = 15;
|
2016-08-31 16:15:54 +00:00
|
|
|
|
|
|
|
// Calculated data
|
|
|
|
$helper->log('Calculated data');
|
|
|
|
for ($row = 1; $row <= 2; ++$row) {
|
|
|
|
for ($col = 'A'; $col != 'C'; ++$col) {
|
2020-07-26 04:51:13 +00:00
|
|
|
if (
|
|
|
|
(($formula = $spreadsheet->getActiveSheet()->getCell($col . $row)->getValue()) !== null) &&
|
|
|
|
($formula[0] == '=')
|
|
|
|
) {
|
2017-10-01 08:48:59 +00:00
|
|
|
$helper->log('Value of ' . $col . $row . ' [' . $formula . ']: ' . $spreadsheet->getActiveSheet()->getCell($col . $row)->getCalculatedValue());
|
2016-08-31 16:15:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save
|
|
|
|
$helper->write($spreadsheet, __FILE__);
|