Merge branch 'master' into Issue-2551_Enable-Array-Readiness-for-Functions-Maths

This commit is contained in:
Mark Baker 2022-02-12 17:11:45 +01:00 committed by GitHub
commit 5d88c6b534
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 117 additions and 7 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org).
## Unreleased - TBD ## Unreleased - TBD
- Fixed `ReferenceHelper@insertNewBefore` behavior when removing column before last column with null value
### Added ### Added
- Improved support for passing of array arguments to Excel function implementations to return array results (where appropriate). [Issue #2551](https://github.com/PHPOffice/PhpSpreadsheet/issues/2551) - Improved support for passing of array arguments to Excel function implementations to return array results (where appropriate). [Issue #2551](https://github.com/PHPOffice/PhpSpreadsheet/issues/2551)

View File

@ -1,5 +1,6 @@
<?php <?php
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\RichText\RichText; use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Shared\Date; use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Spreadsheet;
@ -144,6 +145,11 @@ $spreadsheet->getActiveSheet()
$spreadsheet->getActiveSheet() $spreadsheet->getActiveSheet()
->setCellValue('C18', '=HYPERLINK("mailto:abc@def.com","abc@def.com")'); ->setCellValue('C18', '=HYPERLINK("mailto:abc@def.com","abc@def.com")');
$spreadsheet->getActiveSheet()
->setCellValue('A20', 'String')
->setCellValue('B20', 'inline')
->setCellValueExplicit('C20', 'This will not be added to sharedStrings.xml', DataType::TYPE_INLINE);
$spreadsheet->getActiveSheet() $spreadsheet->getActiveSheet()
->getColumnDimension('B') ->getColumnDimension('B')
->setAutoSize(true); ->setAutoSize(true);

View File

@ -1691,13 +1691,17 @@ class Xlsx extends BaseReader
} else { } else {
$objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t)); $objText = $value->createTextRun(StringHelper::controlCharacterOOXML2PHP((string) $run->t));
$attr = $run->rPr->rFont->attributes(); if (isset($run->rPr->rFont)) {
if (isset($attr['val'])) { $attr = $run->rPr->rFont->attributes();
$objText->getFont()->setName((string) $attr['val']); if (isset($attr['val'])) {
$objText->getFont()->setName((string) $attr['val']);
}
} }
$attr = $run->rPr->sz->attributes(); if (isset($run->rPr->sz)) {
if (isset($attr['val'])) { $attr = $run->rPr->sz->attributes();
$objText->getFont()->setSize((float) $attr['val']); if (isset($attr['val'])) {
$objText->getFont()->setSize((float) $attr['val']);
}
} }
if (isset($run->rPr->color)) { if (isset($run->rPr->color)) {
$objText->getFont()->setColor(new Color($this->styleReader->readColor($run->rPr->color))); $objText->getFont()->setColor(new Color($this->styleReader->readColor($run->rPr->color)));

View File

@ -398,6 +398,26 @@ class ReferenceHelper
} }
} }
// Find missing coordinates. This is important when inserting column before the last column
$missingCoordinates = array_filter(
array_map(function ($row) use ($highestColumn) {
return $highestColumn . $row;
}, range(1, $highestRow)),
function ($coordinate) use ($allCoordinates) {
return !in_array($coordinate, $allCoordinates);
}
);
// Create missing cells with null values
if (!empty($missingCoordinates)) {
foreach ($missingCoordinates as $coordinate) {
$worksheet->createNewCell($coordinate);
}
// Refresh all coordinates
$allCoordinates = $worksheet->getCoordinates();
}
// Loop through cells, bottom-up, and change cell coordinate // Loop through cells, bottom-up, and change cell coordinate
if ($remove) { if ($remove) {
// It's faster to reverse and pop than to use unshift, especially with large cell collections // It's faster to reverse and pop than to use unshift, especially with large cell collections

View File

@ -1291,7 +1291,7 @@ class Worksheet implements IComparable
* *
* @return Cell Cell that was created * @return Cell Cell that was created
*/ */
private function createNewCell($coordinate) public function createNewCell($coordinate)
{ {
$cell = new Cell(null, DataType::TYPE_NULL, $this); $cell = new Cell(null, DataType::TYPE_NULL, $this);
$this->cellCollection->add($coordinate, $cell); $this->cellCollection->add($coordinate, $cell);

View File

@ -433,6 +433,7 @@ class Worksheet extends BIFFwriter
} else { } else {
switch ($cell->getDatatype()) { switch ($cell->getDatatype()) {
case DataType::TYPE_STRING: case DataType::TYPE_STRING:
case DataType::TYPE_INLINE:
case DataType::TYPE_NULL: case DataType::TYPE_NULL:
if ($cVal === '' || $cVal === null) { if ($cVal === '' || $cVal === null) {
$this->writeBlank($row, $column, $xfIndex); $this->writeBlank($row, $column, $xfIndex);

View File

@ -1189,10 +1189,12 @@ class Worksheet extends WriterPart
{ {
$objWriter->writeAttribute('t', $mappedType); $objWriter->writeAttribute('t', $mappedType);
if (!$cellValue instanceof RichText) { if (!$cellValue instanceof RichText) {
$objWriter->startElement('is');
$objWriter->writeElement( $objWriter->writeElement(
't', 't',
StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue, Settings::htmlEntityFlags())) StringHelper::controlCharacterPHP2OOXML(htmlspecialchars($cellValue, Settings::htmlEntityFlags()))
); );
$objWriter->endElement();
} elseif ($cellValue instanceof RichText) { } elseif ($cellValue instanceof RichText) {
$objWriter->startElement('is'); $objWriter->startElement('is');
$this->getParentWriter()->getWriterPartstringtable()->writeRichText($objWriter, $cellValue); $this->getParentWriter()->getWriterPartstringtable()->writeRichText($objWriter, $cellValue);

View File

@ -0,0 +1,47 @@
<?php
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PHPUnit\Framework\TestCase;
class Issue2542Test extends TestCase
{
/**
* @var string
*/
private static $testbook = 'tests/data/Reader/XLSX/issue.2542.xlsx';
public function testPreliminaries(): void
{
// Rich text without 'sz' tag
$file = 'zip://';
$file .= self::$testbook;
$file .= '#xl/sharedStrings.xml';
$data = file_get_contents($file);
// confirm that file contains expected namespaced xml tag
if ($data === false) {
self::fail('Unable to read file sharedStrings.xml');
} else {
self::assertStringContainsString('<si><r><rPr><rFont val="Arial"/><b/><color theme="1"/></rPr><t xml:space="preserve">Factor group
</t></r><r><rPr><rFont val="Arial"/><b val="0"/><color theme="1"/></rPr><t>(for Rental items only)</t></r></si>', $data);
}
}
public function testIssue2542(): void
{
$filename = self::$testbook;
$reader = new Xlsx();
$spreadsheet = $reader->load($filename);
$sheet = $spreadsheet->getActiveSheet();
$value = $sheet->getCell('P1')->getValue();
if ($value instanceof RichText) {
self::assertSame("Factor group\n(for Rental items only)", $value->getPlainText());
} else {
self::fail('Cell P1 is not RichText');
}
$spreadsheet->disconnectWorksheets();
}
}

View File

@ -149,4 +149,32 @@ class ReferenceHelperTest extends TestCase
self::assertSame($oldValue, $newValue); self::assertSame($oldValue, $newValue);
self::assertSame($oldDataType, $newDataType); self::assertSame($oldDataType, $newDataType);
} }
public function testRemoveColumnShiftsCorrectColumnValueIntoRemovedColumnCoordinates(): void
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->fromArray([
['a1', 'b1', 'c1'],
['a2', 'b2', null],
]);
$cells = $sheet->toArray();
self::assertSame('a1', $cells[0][0]);
self::assertSame('b1', $cells[0][1]);
self::assertSame('c1', $cells[0][2]);
self::assertSame('a2', $cells[1][0]);
self::assertSame('b2', $cells[1][1]);
self::assertNull($cells[1][2]);
$sheet->removeColumn('B');
$cells = $sheet->toArray();
self::assertSame('a1', $cells[0][0]);
self::assertSame('c1', $cells[0][1]);
self::assertArrayNotHasKey(2, $cells[0]);
self::assertSame('a2', $cells[1][0]);
self::assertNull($cells[1][1]);
self::assertArrayNotHasKey(2, $cells[1]);
}
} }

Binary file not shown.