box / spout

Read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
http://opensource.box.com/spout/
Apache License 2.0
4.23k stars 636 forks source link

Default style with no text wrap is ignored #829

Open fwwarr opened 3 years ago

fwwarr commented 3 years ago

Automatic text wrapping due to the presence of new lines in a cell should only be applied if shouldWrapText has not been set on the default style. Indeed the StyleManager does check !$cellStyle->hasSetWrapText() before applying automatic text wrapping, but the following sample shows the opposite behavior:

$defaultStyle = (new StyleBuilder())->setShouldWrapText(false)->build();
$writer = WriterEntityFactory::createXLSXWriter();
$writer->setDefaultRowStyle($defaultStyle)->openToFile('testNoWrap.xlsx');
$writer->addRow(WriterEntityFactory::createRowFromArray(["test\n"]));
$writer->close();

This seems to be because StyleMerger::mergeCellProperties only calls setShouldWrapText on the style to update if the $baseStyle has shouldWrapText true, whereas it probably should call it whenever hasSetWrapText is true.

Current code:

        if (!$style->hasSetWrapText() && $baseStyle->shouldWrapText()) {
            $styleToUpdate->setShouldWrapText();
        }

Possible fix:

        if (!$style->hasSetWrapText() && $baseStyle->hasSetWrapText()) {
            $styleToUpdate->setShouldWrapText($baseStyle->shouldWrapText());
        }
Slamdunk commented 2 years ago

Fixed in https://github.com/openspout/openspout/pull/63