Skip to content

Commit

Permalink
Merge branch 'master' into csvphp9
Browse files Browse the repository at this point in the history
  • Loading branch information
oleibman authored Oct 15, 2024
2 parents 748ef0a + d6a3676 commit 753732b
Show file tree
Hide file tree
Showing 37 changed files with 6,219 additions and 6,175 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org).

### Added

- Nothing yet.
- Add Dynamic valueBinder Property to Spreadsheet and Readers. [Issue #1395](https://github.com/PHPOffice/PhpSpreadsheet/issues/1395) [PR #4185](https://github.com/PHPOffice/PhpSpreadsheet/pull/4185)
- Allow Omitting Chart Border. [Issue #562](https://github.com/PHPOffice/PhpSpreadsheet/issues/562) [PR #4188](https://github.com/PHPOffice/PhpSpreadsheet/pull/4188)

### Changed

- Nothing yet.
- Refactor Xls Reader. [PR #4118](https://github.com/PHPOffice/PhpSpreadsheet/pull/4118)

### Deprecated

Expand All @@ -31,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- SUMIFS Does Not Require xlfn. [Issue #4182](https://github.com/PHPOffice/PhpSpreadsheet/issues/4182) [PR #4186](https://github.com/PHPOffice/PhpSpreadsheet/pull/4186)
- Image Transparency/Opacity with Html Reader Changes. [Discussion #4117](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4117) [PR #4142](https://github.com/PHPOffice/PhpSpreadsheet/pull/4142)
- Option to Write Hyperlink Rather Than Label to Csv. [Issue #1412](https://github.com/PHPOffice/PhpSpreadsheet/issues/1412) [PR #4151](https://github.com/PHPOffice/PhpSpreadsheet/pull/4151)
- Invalid Html Due to Cached Filesize. [Issue #1107](https://github.com/PHPOffice/PhpSpreadsheet/issues/1107) [PR #4184](https://github.com/PHPOffice/PhpSpreadsheet/pull/4184)
- Excel 2003 Allows Html Entities. [Issue #2157](https://github.com/PHPOffice/PhpSpreadsheet/issues/2157) [PR #4187](https://github.com/PHPOffice/PhpSpreadsheet/pull/4187)

## 2024-09-29 - 3.3.0 (no 3.0.\*, 3.1.\*, 3.2.\*)

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you would like to contribute, here are some notes and guidelines:
- The code must work with all PHP versions that we support.
- You can call `composer versions` to test version compatibility.
- Code style should be maintained.
- `composer style` will identify any issues with Coding Style`.
- `composer style` will identify any issues with Coding Style.
- `composer fix` will fix most issues with Coding Style.
- All code changes must be validated by `composer check`.
- Please include Unit Tests to verify that a bug exists, and that this PR fixes it.
Expand Down
3 changes: 3 additions & 0 deletions docs/topics/Behind the Mask.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ If you wish to emulate the MS Excel behaviour, and automatically convert string

You can do this by changing the Value Binder, which will then apply every time you set a Cell value.
```php
// Old method using static property
Cell::setValueBinder(new AdvancedValueBinder());
// Preferred method using dynamic property since 3.4.0
$spreadsheet->setValueBinder(new AdvancedValueBinder());

// Set Cell C21 using a formatted string value
$worksheet->getCell('C20')->setValue('€ -12345.6789');
Expand Down
3 changes: 3 additions & 0 deletions docs/topics/The Dating Game.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ $spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
// Use the Advanced Value Binder so that our string date/time values will be automatically converted
// to Excel serialized date/timestamps
// Old method using static property
Cell::setValueBinder(new AdvancedValueBinder());
// Preferred method using dynamic property since 3.4.0
$spreadsheet->setValueBinder(new AdvancedValueBinder());

// Write our data to the worksheet
$worksheet->fromArray($projectHeading);
Expand Down
17 changes: 12 additions & 5 deletions docs/topics/accessing-cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,15 @@ style information. The following example demonstrates how to set the
value binder in PhpSpreadsheet:

```php
/** PhpSpreadsheet */
require_once 'src/Boostrap.php';

// Set value binder
// Older method using static property
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// Preferred method using dynamic property since 3.4.0
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

// ...
// Add some data, resembling some different data types
$spreadsheet->getActiveSheet()->setCellValue('A4', 'Percentage value:');
Expand Down Expand Up @@ -555,13 +555,20 @@ $stringValueBinder->setNumericConversion(false)
->setBooleanConversion(false)
->setNullConversion(false)
->setFormulaConversion(false);
// Older method using static property
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( $stringValueBinder );
// Preferred method using dynamic property since 3.4.0
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$spreadsheet->setValueBinder( $stringValueBinder );
```

You can override the current binder when setting individual cell values by specifying a different Binder to use in the Cell's `setValue()` or the Worksheet's `setCellValue()` methods.
```php
$spreadsheet = new Spreadsheet();
// Old method using static property
Cell::setValueBinder(new AdvancedValueBinder());
// Preferred method using dynamic property since 3.4.0
$spreadsheet->setValueBinder(new AdvancedValueBinder());

$value = '12.5%';

Expand Down
12 changes: 8 additions & 4 deletions docs/topics/reading-files.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,18 @@ So using a Value Binder allows a great deal more flexibility in the
loader logic when reading unformatted text files.

```php
/** Tell PhpSpreadsheet that we want to use the Advanced Value Binder **/
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

$inputFileType = 'Csv';
$inputFileName = './sampleData/example1.tsv';

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setDelimiter("\t");

/** Tell PhpSpreadsheet that we want to use the Advanced Value Binder **/
// Old method using static property
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
// Preferred method using dynamic property since 3.4.0
$reader::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

$spreadsheet = $reader->load($inputFileName);
```

Expand All @@ -774,7 +778,7 @@ Loading using a Value Binder applies to:
Reader | Y/N |Reader | Y/N |Reader | Y/N
----------|:---:|--------|:---:|--------------|:---:
Xlsx | NO | Xls | NO | Xml | NO
Ods | NO | SYLK | NO | Gnumeric | NO
Ods | NO | SYLK | YES | Gnumeric | NO
CSV | YES | HTML | YES

Note that you can also use the Binder to determine how PhpSpreadsheet identified datatypes for values when you set a cell value without explicitly setting a datatype.
Expand Down
6 changes: 6 additions & 0 deletions docs/topics/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,10 @@ method that suits you the best. Here are some examples:

```php
// MySQL-like timestamp '2008-12-31' or date string
// Old method using static property
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
// Preferred method using dynamic property since 3.4.0
$spreadsheet->setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

$spreadsheet->getActiveSheet()
->setCellValue('D1', '2008-12-31');
Expand Down Expand Up @@ -599,7 +602,10 @@ when it sees a newline character in a string that you are inserting in a
cell. Just like Microsoft Office Excel. Try this:

```php
// Old method using static property
\PhpOffice\PhpSpreadsheet\Cell\Cell::setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );
// Preferred method using dynamic property since 3.4.0
$spreadsheet->setValueBinder( new \PhpOffice\PhpSpreadsheet\Cell\AdvancedValueBinder() );

$spreadsheet->getActiveSheet()->getCell('A1')->setValue("hello\nworld");
```
Expand Down
2 changes: 2 additions & 0 deletions samples/Chart33a/33_Chart_create_area.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
$chart->setTopLeftPosition('A7');
$chart->setBottomRightPosition('H20');

$chart->setNoBorder(true);

// Add the chart to the worksheet
$worksheet->addChart($chart);

Expand Down
10 changes: 7 additions & 3 deletions src/PhpSpreadsheet/Cell/Cell.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,11 @@ public function __construct(mixed $value, ?string $dataType, Worksheet $workshee
$dataType = DataType::TYPE_STRING;
}
$this->dataType = $dataType;
} elseif (self::getValueBinder()->bindValue($this, $value) === false) {
throw new SpreadsheetException('Value could not be bound to cell.');
} else {
$valueBinder = $worksheet->getParent()?->getValueBinder() ?? self::getValueBinder();
if ($valueBinder->bindValue($this, $value) === false) {
throw new SpreadsheetException('Value could not be bound to cell.');
}
}
$this->ignoredErrors = new IgnoredErrors();
}
Expand Down Expand Up @@ -232,7 +235,8 @@ protected static function updateIfCellIsTableHeader(?Worksheet $workSheet, self
*/
public function setValue(mixed $value, ?IValueBinder $binder = null): self
{
$binder ??= self::getValueBinder();
// Cells?->Worksheet?->Spreadsheet
$binder ??= $this->parent?->getParent()?->getParent()?->getValueBinder() ?? self::getValueBinder();
if (!$binder->bindValue($this, $value)) {
throw new SpreadsheetException('Value could not be bound to cell.');
}
Expand Down
14 changes: 14 additions & 0 deletions src/PhpSpreadsheet/Chart/Chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class Chart

private bool $noFill = false;

private bool $noBorder = false;

private bool $roundedCorners = false;

private GridLines $borderLines;
Expand Down Expand Up @@ -696,6 +698,18 @@ public function setNoFill(bool $noFill): self
return $this;
}

public function getNoBorder(): bool
{
return $this->noBorder;
}

public function setNoBorder(bool $noBorder): self
{
$this->noBorder = $noBorder;

return $this;
}

public function getRoundedCorners(): bool
{
return $this->roundedCorners;
Expand Down
15 changes: 15 additions & 0 deletions src/PhpSpreadsheet/Reader/BaseReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Reader;

use PhpOffice\PhpSpreadsheet\Cell\IValueBinder;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Reader\Exception as ReaderException;
use PhpOffice\PhpSpreadsheet\Reader\Security\XmlScanner;
Expand Down Expand Up @@ -56,6 +57,8 @@ abstract class BaseReader implements IReader

protected ?XmlScanner $securityScanner = null;

protected ?IValueBinder $valueBinder = null;

public function __construct()
{
$this->readFilter = new DefaultReadFilter();
Expand Down Expand Up @@ -242,4 +245,16 @@ public function listWorksheetNames(string $filename): array

return $returnArray;
}

public function getValueBinder(): ?IValueBinder
{
return $this->valueBinder;
}

public function setValueBinder(?IValueBinder $valueBinder): self
{
$this->valueBinder = $valueBinder;

return $this;
}
}
4 changes: 3 additions & 1 deletion src/PhpSpreadsheet/Reader/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
Expand All @@ -275,6 +276,7 @@ public function loadSpreadsheetFromString(string $contents): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);

// Load into this instance
return $this->loadStringOrFile('data://text/plain,' . urlencode($contents), $spreadsheet, true);
Expand Down Expand Up @@ -413,7 +415,7 @@ private function loadStringOrFile2(string $filename, Spreadsheet $spreadsheet, b
// Loop through each line of the file in turn
$delimiter = $this->delimiter ?? '';
$rowData = self::getCsv($fileHandle, 0, $delimiter, $this->enclosure, $this->escapeCharacter);
$valueBinder = Cell::getValueBinder();
$valueBinder = $this->valueBinder ?? Cell::getValueBinder();
$preserveBooleanString = method_exists($valueBinder, 'getBooleanConversion') && $valueBinder->getBooleanConversion();
$this->getTrue = Calculation::getTRUE();
$this->getFalse = Calculation::getFALSE();
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Gnumeric.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);
$spreadsheet->removeSheetByIndex(0);

// Load into this instance
Expand Down
3 changes: 3 additions & 0 deletions src/PhpSpreadsheet/Reader/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ private function readEnding(): string
// Phpstan incorrectly flags following line for Php8.2-, corrected in 8.3
$filename = $meta['uri']; //@phpstan-ignore-line

clearstatcache(true, $filename);
$size = (int) filesize($filename);
if ($size === 0) {
return '';
Expand Down Expand Up @@ -210,6 +211,7 @@ public function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
Expand Down Expand Up @@ -792,6 +794,7 @@ public function loadFromString(string $content, ?Spreadsheet $spreadsheet = null
throw new Exception('Failed to load content as a DOM Document', 0, $e ?? null);
}
$spreadsheet = $spreadsheet ?? new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);
self::loadProperties($dom, $spreadsheet);

return $this->loadDocument($dom, $spreadsheet);
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);
$spreadsheet->removeSheetByIndex(0);

// Load into this instance
Expand Down
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Reader/Slk.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ protected function loadSpreadsheetFromFile(string $filename): Spreadsheet
{
// Create new Spreadsheet
$spreadsheet = new Spreadsheet();
$spreadsheet->setValueBinder($this->valueBinder);

// Load into this instance
return $this->loadIntoExisting($filename, $spreadsheet);
Expand Down
Loading

0 comments on commit 753732b

Please sign in to comment.