From eb319843402dfe2843e23be56c21f3ccaca26cc2 Mon Sep 17 00:00:00 2001 From: oleibman <10341515+oleibman@users.noreply.github.com> Date: Mon, 24 Mar 2025 21:06:43 -0700 Subject: [PATCH] Make Class Extendable Document some cases where solution might not work well. Making class extendable and offering some over-rideable methods may allow for solutions to some of these problems. --- docs/topics/reading-and-writing-to-file.md | 7 ++++++- src/PhpSpreadsheet/Helper/TextGrid.php | 20 +++++++++++++++----- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/docs/topics/reading-and-writing-to-file.md b/docs/topics/reading-and-writing-to-file.md index f472178f6..a0181993e 100644 --- a/docs/topics/reading-and-writing-to-file.md +++ b/docs/topics/reading-and-writing-to-file.md @@ -1200,7 +1200,12 @@ You can then echo `$result` to a terminal, or write it to a file with `file_put_ | A | B | C | D | +---+-----+------------------+---+----------+ | 1 | 6 | 1900-01-06 00:00 | | 0.572917 | -| 2 | 6 | 1900-01-06 00:00 | | 1<>2 | +| 2 | 6 | TRUE | | 1<>2 | | 3 | xyz | xyz | | | +---+-----+------------------+---+----------+ ``` +Please note that this may produce sub-optimal results for situations such as: +- use of accents as combining characters rather than using pre-composed characters (may be handled by extending the class to override the `getString` or `strlen` methods) +- Fullwidth characters +- right-to-left characters (better display in a browser than a terminal on a non-RTL system) +- multi-line strings diff --git a/src/PhpSpreadsheet/Helper/TextGrid.php b/src/PhpSpreadsheet/Helper/TextGrid.php index 76a5258a2..c7868c050 100644 --- a/src/PhpSpreadsheet/Helper/TextGrid.php +++ b/src/PhpSpreadsheet/Helper/TextGrid.php @@ -48,7 +48,7 @@ class TextGrid if (!empty($this->rows)) { $maxRow = max($this->rows); - $maxRowLength = mb_strlen((string) $maxRow) + 1; + $maxRowLength = strlen((string) $maxRow) + 1; $columnWidths = $this->getColumnWidths(); $this->renderColumnHeader($maxRowLength, $columnWidths); @@ -80,10 +80,10 @@ class TextGrid private function renderCells(array $rowData, array $columnWidths): void { foreach ($rowData as $column => $cell) { - $valueForLength = StringHelper::convertToString($cell, convertBool: true); + $valueForLength = $this->getString($cell); $displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength); $this->gridDisplay .= '| '; - $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - mb_strlen($valueForLength) + 1); + $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1); } } @@ -95,7 +95,7 @@ class TextGrid return; } foreach ($this->columns as $column => $reference) { - $columnWidths[$column] = max($columnWidths[$column], mb_strlen($reference)); + $columnWidths[$column] = max($columnWidths[$column], $this->strlen($reference)); } if ($this->rowHeaders) { $this->gridDisplay .= str_repeat(' ', $maxRowLength + 2); @@ -145,9 +145,19 @@ class TextGrid $columnData = array_values($columnData); foreach ($columnData as $columnValue) { - $columnWidth = max($columnWidth, mb_strlen(StringHelper::convertToString($columnValue, convertBool: true))); + $columnWidth = max($columnWidth, $this->strlen($this->getString($columnValue))); } return $columnWidth; } + + protected function getString(mixed $value): string + { + return StringHelper::convertToString($value, convertBool: true); + } + + protected function strlen(string $value): int + { + return mb_strlen($value); + } }