spout icon indicating copy to clipboard operation
spout copied to clipboard

Default style with no text wrap is ignored

Open fwwarr opened this issue 4 years ago • 1 comments

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());
        }

fwwarr avatar Sep 15 '21 13:09 fwwarr

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

Slamdunk avatar Mar 28 '22 14:03 Slamdunk