parent
0c7df7721b
commit
c723833d6f
|
@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
- Support to read Xlsm templates with form elements, macros, printer settings, protected elements and back compatibility drawing, and save result without losing important elements of document - [#435](https://github.com/PHPOffice/PhpSpreadsheet/issues/435)
|
- Support to read Xlsm templates with form elements, macros, printer settings, protected elements and back compatibility drawing, and save result without losing important elements of document - [#435](https://github.com/PHPOffice/PhpSpreadsheet/issues/435)
|
||||||
- Expose sheet title maximum length as `Worksheet::SHEET_TITLE_MAXIMUM_LENGTH` - [#482](https://github.com/PHPOffice/PhpSpreadsheet/issues/482)
|
- Expose sheet title maximum length as `Worksheet::SHEET_TITLE_MAXIMUM_LENGTH` - [#482](https://github.com/PHPOffice/PhpSpreadsheet/issues/482)
|
||||||
|
- Allow escape character to be set in CSV reader – [#492](https://github.com/PHPOffice/PhpSpreadsheet/issues/492)
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,13 @@ class Csv extends BaseReader
|
||||||
*/
|
*/
|
||||||
private $contiguousRow = -1;
|
private $contiguousRow = -1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The character that can escape the enclosure.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $escapeCharacter = '\\';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new CSV Reader instance.
|
* Create a new CSV Reader instance.
|
||||||
*/
|
*/
|
||||||
|
@ -254,7 +261,7 @@ class Csv extends BaseReader
|
||||||
$worksheetInfo[0]['totalColumns'] = 0;
|
$worksheetInfo[0]['totalColumns'] = 0;
|
||||||
|
|
||||||
// Loop through each line of the file in turn
|
// Loop through each line of the file in turn
|
||||||
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
|
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure, $this->escapeCharacter)) !== false) {
|
||||||
++$worksheetInfo[0]['totalRows'];
|
++$worksheetInfo[0]['totalRows'];
|
||||||
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
|
$worksheetInfo[0]['lastColumnIndex'] = max($worksheetInfo[0]['lastColumnIndex'], count($rowData) - 1);
|
||||||
}
|
}
|
||||||
|
@ -326,7 +333,7 @@ class Csv extends BaseReader
|
||||||
}
|
}
|
||||||
|
|
||||||
// Loop through each line of the file in turn
|
// Loop through each line of the file in turn
|
||||||
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure)) !== false) {
|
while (($rowData = fgetcsv($fileHandle, 0, $this->delimiter, $this->enclosure, $this->escapeCharacter)) !== false) {
|
||||||
$columnLetter = 'A';
|
$columnLetter = 'A';
|
||||||
foreach ($rowData as $rowDatum) {
|
foreach ($rowData as $rowDatum) {
|
||||||
if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) {
|
if ($rowDatum != '' && $this->readFilter->readCell($columnLetter, $currentRow)) {
|
||||||
|
@ -458,6 +465,30 @@ class Csv extends BaseReader
|
||||||
return $this->contiguous;
|
return $this->contiguous;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set escape backslashes.
|
||||||
|
*
|
||||||
|
* @param string $escapeCharacter
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setEscapeCharacter($escapeCharacter)
|
||||||
|
{
|
||||||
|
$this->escapeCharacter = $escapeCharacter;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get escape backslashes.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function getEscapeCharacter()
|
||||||
|
{
|
||||||
|
return $this->escapeCharacter;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Can the current IReader read the file?
|
* Can the current IReader read the file?
|
||||||
*
|
*
|
||||||
|
|
|
@ -89,4 +89,19 @@ class CsvTest extends TestCase
|
||||||
[true, '../samples/Reader/sampleData/example2.csv'],
|
[true, '../samples/Reader/sampleData/example2.csv'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testEscapeCharacters()
|
||||||
|
{
|
||||||
|
$reader = (new Csv())->setEscapeCharacter('"');
|
||||||
|
$worksheet = $reader->load(__DIR__ . '/../../data/Reader/CSV/backslash.csv')
|
||||||
|
->getActiveSheet();
|
||||||
|
|
||||||
|
$expected = [
|
||||||
|
['field 1', 'field 2\\'],
|
||||||
|
['field 3\\', 'field 4'],
|
||||||
|
];
|
||||||
|
|
||||||
|
$this->assertSame('"', $reader->getEscapeCharacter());
|
||||||
|
$this->assertSame($expected, $worksheet->toArray());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
"field 1","field 2\"
|
||||||
|
"field 3\","field 4"
|
|
Loading…
Reference in New Issue