Improve fixed width settings
This commit is contained in:
parent
d53be7bd2b
commit
8ba78df14a
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 .= '<style:style style:name="co'.$colNo.
|
||||
|
@ -165,6 +165,22 @@ abstract class WriterMultiSheetsAbstract extends WriterAbstract
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set fixed sheet width size option
|
||||
*
|
||||
* @param int $option The fixed width
|
||||
* @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened
|
||||
* @return Writer
|
||||
*/
|
||||
public function setFixedWidth($option)
|
||||
{
|
||||
$this->throwIfWriterAlreadyOpened('Writer must be configured before opening it.');
|
||||
|
||||
$this->optionsManager->setOption(Options::ROWWIDTH_FIXED, $option);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -67,6 +67,9 @@ EOD;
|
||||
/** @var int Width calculation style */
|
||||
protected $widthCalcuationStyle;
|
||||
|
||||
/** @var int Fixed Width */
|
||||
protected $fixedWidth;
|
||||
|
||||
/**
|
||||
* WorksheetManager constructor.
|
||||
*
|
||||
@ -89,6 +92,7 @@ EOD;
|
||||
) {
|
||||
$this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS);
|
||||
$this->widthCalcuationStyle = $optionsManager->getOption(Options::ROWWIDTH_CALC_STYLE);
|
||||
$this->fixedWidth = $optionsManager->getOption(Options::ROWWIDTH_FIXED);
|
||||
$this->rowManager = $rowManager;
|
||||
$this->styleManager = $styleManager;
|
||||
$this->styleMerger = $styleMerger;
|
||||
@ -115,6 +119,7 @@ EOD;
|
||||
|
||||
$worksheet->setFilePointer($sheetFilePointer);
|
||||
$worksheet->setWidthCalculation($this->widthCalcuationStyle);
|
||||
$worksheet->setFixedSheetWidth($this->fixedWidth);
|
||||
|
||||
\fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER);
|
||||
if ($worksheet->getWidthCalculation() != Worksheet::W_NONE) {
|
||||
@ -310,6 +315,16 @@ EOD;
|
||||
if ($worksheet->getWidthCalculation() != Worksheet::W_NONE) {
|
||||
$colNode ='<cols>';
|
||||
$widths = $worksheet->getColumnWidths();
|
||||
|
||||
//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){
|
||||
$colAffect = $i + 1;
|
||||
$colNode .= '<col hidden="false" collapsed="false" min="'.$colAffect.'" max="'.$colAffect.'" width="'.$width.'" customWidth="true"/>';
|
||||
|
Loading…
x
Reference in New Issue
Block a user