From 8ba78df14afcc22e2fc065ccc22e478859d9f01d Mon Sep 17 00:00:00 2001 From: Xwiz Date: Sat, 5 Feb 2022 11:08:17 +0100 Subject: [PATCH] Improve fixed width settings --- src/Spout/Writer/Common/Entity/Options.php | 1 + src/Spout/Writer/Common/Entity/Worksheet.php | 7 +----- .../Writer/ODS/Manager/WorksheetManager.php | 22 ++++++++++++++++++- .../Writer/WriterMultiSheetsAbstract.php | 16 ++++++++++++++ .../Writer/XLSX/Manager/WorksheetManager.php | 15 +++++++++++++ 5 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/Spout/Writer/Common/Entity/Options.php b/src/Spout/Writer/Common/Entity/Options.php index c655699..94dffa6 100644 --- a/src/Spout/Writer/Common/Entity/Options.php +++ b/src/Spout/Writer/Common/Entity/Options.php @@ -17,6 +17,7 @@ abstract class Options public const TEMP_FOLDER = 'tempFolder'; public const DEFAULT_ROW_STYLE = 'defaultRowStyle'; public const ROWWIDTH_CALC_STYLE = 'rowCalcMethod'; + public const ROWWIDTH_FIXED = 'rowFixedWith'; public const SHOULD_CREATE_NEW_SHEETS_AUTOMATICALLY = 'shouldCreateNewSheetsAutomatically'; // XLSX specific options diff --git a/src/Spout/Writer/Common/Entity/Worksheet.php b/src/Spout/Writer/Common/Entity/Worksheet.php index ffdd1d2..aa0b97f 100644 --- a/src/Spout/Writer/Common/Entity/Worksheet.php +++ b/src/Spout/Writer/Common/Entity/Worksheet.php @@ -36,7 +36,7 @@ class Worksheet public const W_FIXED = 2; public const W_NONE = 0; public const DEFAULT_COL_WIDTH = 30; - public const DEFAULT_FIXED_WIDTH = 1068; + public const DEFAULT_FIXED_WIDTH = 320; /** * Worksheet constructor. @@ -143,11 +143,6 @@ class Worksheet { $size = 1 + strlen($cell->getValue());//ensure we have at least 1 space $size *= $style->isFontBold() ? 1.2 : 1.0; - if ($this->getWidthCalculation() == Worksheet::W_FIXED) { - $total = array_sum($this->getColumnWidths()); - $total = $total ?: $size; - $size = ($size / $total) * $this->getFixedSheetWidth(); - } $this->setMaxColumnWidth($zeroBasedIndex, $size); } diff --git a/src/Spout/Writer/ODS/Manager/WorksheetManager.php b/src/Spout/Writer/ODS/Manager/WorksheetManager.php index 388a747..8160c78 100644 --- a/src/Spout/Writer/ODS/Manager/WorksheetManager.php +++ b/src/Spout/Writer/ODS/Manager/WorksheetManager.php @@ -42,6 +42,9 @@ class WorksheetManager implements WorksheetManagerInterface /** @var int Width calculation style */ protected $widthCalcuationStyle; + /** @var int Fixed Width */ + protected $fixedWidth; + /** * WorksheetManager constructor. * @@ -59,6 +62,7 @@ class WorksheetManager implements WorksheetManagerInterface StringHelper $stringHelper ) { $this->widthCalcuationStyle = $optionsManager->getOption(Options::ROWWIDTH_CALC_STYLE); + $this->fixedWidth = $optionsManager->getOption(Options::ROWWIDTH_FIXED); $this->styleManager = $styleManager; $this->styleMerger = $styleMerger; $this->stringsEscaper = $stringsEscaper; @@ -78,6 +82,7 @@ class WorksheetManager implements WorksheetManagerInterface $this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer); $worksheet->setWidthCalculation($this->widthCalcuationStyle); + $worksheet->setFixedSheetWidth($this->fixedWidth); if ($worksheet->getWidthCalculation() != Worksheet::W_NONE) { $this->headWritePosition = ftell($sheetFilePointer); } @@ -278,6 +283,11 @@ class WorksheetManager implements WorksheetManagerInterface return $data; } + /** + * Generate the related column widths style xml to be inserted in content.xml + * @param Worksheet $worksheet + * @return string + */ public function getWidthStylesContent($worksheet) { if ($worksheet->getWidthCalculation() != Worksheet::W_NONE) { @@ -285,8 +295,18 @@ class WorksheetManager implements WorksheetManagerInterface $style = ''; $widths = $worksheet->getColumnWidths(); //todo: this may not be adequate for multiple worksheets + + //re-calculate width for fixed sets + if ($worksheet->getWidthCalculation() == Worksheet::W_FIXED) { + $total = array_sum($widths); + foreach($widths as $i => $w) { + $wr = ($w / $total) * $worksheet->getFixedSheetWidth(); + $widths[$i] = $wr; + } + } + foreach ($widths as $i => $width){ - //this is a rough equivalent based on pixel density + //this is a rough equivalent based on pixel density, $win = round($width / 9.6, 2);//convert to inches $colNo = $i + 1; $style .= '';