diff --git a/CHANGELOG.md b/CHANGELOG.md
index 99c771bf90..6788a0a1d9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). Thia is a
- Slightly better support for escaped characters in Xlsx Reader/Writer. [Discussion #4724](https://github.com/PHPOffice/PhpSpreadsheet/discussions/4724) [PR #4726](https://github.com/PHPOffice/PhpSpreadsheet/pull/4726)
- CODE/UNICODE and CHAR/UNICHAR. [PR #4727](https://github.com/PHPOffice/PhpSpreadsheet/pull/4727)
+- Minor changes to TextGrid. [PR #4735](https://github.com/PHPOffice/PhpSpreadsheet/pull/4735)
## 2025-11-24 - 5.3.0
diff --git a/samples/Autofilter/10_Autofilter.php b/samples/Autofilter/10_Autofilter.php
index 4002f1806b..bf8a11b547 100644
--- a/samples/Autofilter/10_Autofilter.php
+++ b/samples/Autofilter/10_Autofilter.php
@@ -86,7 +86,7 @@
['2011', 'Q4', 'Italy', 335],
];
$spreadsheet->getActiveSheet()->fromArray($dataArray, null, 'A2');
-$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, false, true));
+$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, false, false, true), true);
// Set title row bold
$helper->log('Set title row bold');
diff --git a/samples/Autofilter/10_Autofilter_selection_1.php b/samples/Autofilter/10_Autofilter_selection_1.php
index ce1bcd435e..cdaeb265af 100644
--- a/samples/Autofilter/10_Autofilter_selection_1.php
+++ b/samples/Autofilter/10_Autofilter_selection_1.php
@@ -26,11 +26,11 @@
// Create the worksheet
$helper->log('Add data');
$spreadsheet->setActiveSheetIndex(0);
-$spreadsheet->getActiveSheet()->setCellValue('A1', 'Financial Year')
- ->setCellValue('B1', 'Financial Period')
+$spreadsheet->getActiveSheet()->setCellValue('A1', 'Year')
+ ->setCellValue('B1', 'Period')
->setCellValue('C1', 'Country')
->setCellValue('D1', 'Date')
- ->setCellValue('E1', 'Sales Value')
+ ->setCellValue('E1', 'Sales')
->setCellValue('F1', 'Expenditure');
$dateTime = new DateTime();
$startYear = $endYear = $currentYear = (int) $dateTime->format('Y');
diff --git a/samples/Autofilter/10_Autofilter_selection_2.php b/samples/Autofilter/10_Autofilter_selection_2.php
index 456f149f08..cece24959c 100644
--- a/samples/Autofilter/10_Autofilter_selection_2.php
+++ b/samples/Autofilter/10_Autofilter_selection_2.php
@@ -26,11 +26,11 @@
// Create the worksheet
$helper->log('Add data');
$spreadsheet->setActiveSheetIndex(0);
-$spreadsheet->getActiveSheet()->setCellValue('A1', 'Financial Year')
- ->setCellValue('B1', 'Financial Period')
+$spreadsheet->getActiveSheet()->setCellValue('A1', 'Year')
+ ->setCellValue('B1', 'Period')
->setCellValue('C1', 'Country')
->setCellValue('D1', 'Date')
- ->setCellValue('E1', 'Sales Value')
+ ->setCellValue('E1', 'Sales')
->setCellValue('F1', 'Expenditure');
$dateTime = new DateTime();
$startYear = $endYear = $currentYear = (int) $dateTime->format('Y');
diff --git a/samples/Autofilter/10_Autofilter_selection_display.php b/samples/Autofilter/10_Autofilter_selection_display.php
index c2d8f871c6..8c660b8e6e 100644
--- a/samples/Autofilter/10_Autofilter_selection_display.php
+++ b/samples/Autofilter/10_Autofilter_selection_display.php
@@ -26,11 +26,11 @@
// Create the worksheet
$helper->log('Add data');
$spreadsheet->setActiveSheetIndex(0);
-$spreadsheet->getActiveSheet()->setCellValue('A1', 'Financial Year')
- ->setCellValue('B1', 'Financial Period')
+$spreadsheet->getActiveSheet()->setCellValue('A1', 'Year')
+ ->setCellValue('B1', 'Period')
->setCellValue('C1', 'Country')
->setCellValue('D1', 'Date')
- ->setCellValue('E1', 'Sales Value')
+ ->setCellValue('E1', 'Sales')
->setCellValue('F1', 'Expenditure');
$dateTime = new DateTime();
$startYear = $endYear = $currentYear = (int) $dateTime->format('Y');
diff --git a/samples/Database/DAVERAGE.php b/samples/Database/DAVERAGE.php
index 0416dd3a2b..3016d3564a 100644
--- a/samples/Database/DAVERAGE.php
+++ b/samples/Database/DAVERAGE.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The Average age of all Apple and Pear trees in the orchard');
$worksheet->setCellValue('B13', '=DAVERAGE(A4:E10,3,A1:A3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DCOUNT.php b/samples/Database/DCOUNT.php
index 476e365deb..60d6fb0f12 100644
--- a/samples/Database/DCOUNT.php
+++ b/samples/Database/DCOUNT.php
@@ -40,7 +40,7 @@
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DCOUNTA.php b/samples/Database/DCOUNTA.php
index ecee4206c4..96682c4a34 100644
--- a/samples/Database/DCOUNTA.php
+++ b/samples/Database/DCOUNTA.php
@@ -40,7 +40,7 @@
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DGET.php b/samples/Database/DGET.php
index cfe650961e..5008a55e36 100644
--- a/samples/Database/DGET.php
+++ b/samples/Database/DGET.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The height of the Apple tree (will return an Excel error, because there is more than one apple tree)');
$worksheet->setCellValue('B13', '=DGET(A4:E10,"Height",A1:A2)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DMAX.php b/samples/Database/DMAX.php
index fbb6b906a4..9244594482 100644
--- a/samples/Database/DMAX.php
+++ b/samples/Database/DMAX.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The Oldest apple tree in the orchard');
$worksheet->setCellValue('B13', '=DMAX(A4:E10,3,A1:A2)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DMIN.php b/samples/Database/DMIN.php
index 565c9501ad..4a458ff234 100644
--- a/samples/Database/DMIN.php
+++ b/samples/Database/DMIN.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The Youngest apple tree in the orchard');
$worksheet->setCellValue('B13', '=DMIN(A4:E10,3,A1:A2)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DPRODUCT.php b/samples/Database/DPRODUCT.php
index d9cf469303..39890ab795 100644
--- a/samples/Database/DPRODUCT.php
+++ b/samples/Database/DPRODUCT.php
@@ -40,7 +40,7 @@
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DSTDEV.php b/samples/Database/DSTDEV.php
index b8e23b6e27..d18de94c07 100644
--- a/samples/Database/DSTDEV.php
+++ b/samples/Database/DSTDEV.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The estimated standard deviation in height of Apple and Pear trees');
$worksheet->setCellValue('B13', '=DSTDEV(A4:E10,2,A1:A3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DSTDEVP.php b/samples/Database/DSTDEVP.php
index 0e20ce8007..447f7efecb 100644
--- a/samples/Database/DSTDEVP.php
+++ b/samples/Database/DSTDEVP.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The standard deviation in height of Apple and Pear trees');
$worksheet->setCellValue('B13', '=DSTDEVP(A4:E10,2,A1:A3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DSUM.php b/samples/Database/DSUM.php
index 2a88008a52..cefe09b65d 100644
--- a/samples/Database/DSUM.php
+++ b/samples/Database/DSUM.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'Total profit from apple trees with a height between 10 and 16 feet, and all pear trees');
$worksheet->setCellValue('B13', '=DSUM(A4:E10,"Profit",A1:F3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DVAR.php b/samples/Database/DVAR.php
index 69809cc170..f26b76a278 100644
--- a/samples/Database/DVAR.php
+++ b/samples/Database/DVAR.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The estimated variance in height of Apple and Pear trees');
$worksheet->setCellValue('B13', '=DVAR(A4:E10,2,A1:A3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Database/DVARP.php b/samples/Database/DVARP.php
index d362a82007..3c589d9d8d 100644
--- a/samples/Database/DVARP.php
+++ b/samples/Database/DVARP.php
@@ -36,11 +36,13 @@
$worksheet->setCellValue('A13', 'The variance in height of Apple and Pear trees');
$worksheet->setCellValue('B13', '=DVARP(A4:E10,2,A1:A3)');
+$worksheet->getStyle('E5:E10')->getNumberFormat()
+ ->setFormatCode('0.00');
$helper->log('Database');
$databaseData = $worksheet->rangeToArray('A4:E10', null, true, true, true);
-$helper->displayGrid($databaseData);
+$helper->displayGrid($databaseData, true);
// Test the formulae
$helper->log('Criteria');
diff --git a/samples/Reader/11_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_1).php b/samples/Reader/11_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_1).php
index 517c327bf9..26b2a12501 100644
--- a/samples/Reader/11_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_1).php
+++ b/samples/Reader/11_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_1).php
@@ -53,10 +53,13 @@ public function readCell(string $columnAddress, int $row, string $worksheetName
$reader->setReadFilter($chunkFilter);
// Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object
$spreadsheet = $reader->load($inputFileName);
+ $sheet = $spreadsheet->getActiveSheet();
// Do some processing here
- $activeRange = $spreadsheet->getActiveSheet()->calculateWorksheetDataDimension();
- $sheetData = $spreadsheet->getActiveSheet()->rangeToArray($activeRange, null, true, true, true);
- $helper->displayGrid($sheetData);
+ $activeRange = $sheet->calculateWorksheetDataDimension();
+ $sheet->getStyle($activeRange)->getNumberFormat()
+ ->setFormatCode('0.000');
+ $sheetData = $sheet->rangeToArray($activeRange, null, true, true, true);
+ $helper->displayGrid($sheetData, true);
}
diff --git a/samples/Reader/12_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_2).php b/samples/Reader/12_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_2).php
index 614a012fa9..968fe0d609 100644
--- a/samples/Reader/12_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_2).php
+++ b/samples/Reader/12_Reading_a_workbook_in_chunks_using_a_configurable_read_filter_(version_2).php
@@ -56,10 +56,13 @@ public function readCell(string $columnAddress, int $row, string $worksheetName
$chunkFilter->setRows($startRow, $chunkSize);
// Load only the rows that match our filter from $inputFileName to a PhpSpreadsheet Object
$spreadsheet = $reader->load($inputFileName);
+ $sheet = $spreadsheet->getActiveSheet();
// Do some processing here
- $activeRange = $spreadsheet->getActiveSheet()->calculateWorksheetDataDimension();
- $sheetData = $spreadsheet->getActiveSheet()->rangeToArray($activeRange, null, true, true, true);
- $helper->displayGrid($sheetData);
+ $activeRange = $sheet->calculateWorksheetDataDimension();
+ $sheet->getStyle($activeRange)->getNumberFormat()
+ ->setFormatCode('0.000');
+ $sheetData = $sheet->rangeToArray($activeRange, null, true, true, true);
+ $helper->displayGrid($sheetData, true);
}
diff --git a/samples/Reader2/13_Simple_file_reader_for_multiple_CSV_files.php b/samples/Reader2/13_Simple_file_reader_for_multiple_CSV_files.php
index cf38746a12..59d30fab59 100644
--- a/samples/Reader2/13_Simple_file_reader_for_multiple_CSV_files.php
+++ b/samples/Reader2/13_Simple_file_reader_for_multiple_CSV_files.php
@@ -23,8 +23,11 @@
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$helper->log('Worksheet #' . $sheetIndex . ' -> ' . $loadedSheetName . '');
$spreadsheet->setActiveSheetIndexByName($loadedSheetName);
+ $sheet = $spreadsheet->getActiveSheet();
- $activeRange = $spreadsheet->getActiveSheet()->calculateWorksheetDataDimension();
- $sheetData = $spreadsheet->getActiveSheet()->rangeToArray($activeRange, null, true, true, true);
- $helper->displayGrid($sheetData);
+ $activeRange = $sheet->calculateWorksheetDataDimension();
+ $sheet->getStyle($activeRange)->getNumberFormat()
+ ->setFormatCode('0.000');
+ $sheetData = $sheet->rangeToArray($activeRange, null, true, true, true);
+ $helper->displayGrid($sheetData, true);
}
diff --git a/samples/Reader2/14_Reading_a_large_CSV_file_in_chunks_to_split_across_multiple_worksheets.php b/samples/Reader2/14_Reading_a_large_CSV_file_in_chunks_to_split_across_multiple_worksheets.php
index 0bc3b86653..27c0fcdfca 100644
--- a/samples/Reader2/14_Reading_a_large_CSV_file_in_chunks_to_split_across_multiple_worksheets.php
+++ b/samples/Reader2/14_Reading_a_large_CSV_file_in_chunks_to_split_across_multiple_worksheets.php
@@ -77,8 +77,11 @@ public function readCell(string $columnAddress, int $row, string $worksheetName
foreach ($loadedSheetNames as $sheetIndex => $loadedSheetName) {
$helper->log('Worksheet #' . $sheetIndex . ' -> ' . $loadedSheetName . '');
$spreadsheet->setActiveSheetIndexByName($loadedSheetName);
+ $sheet = $spreadsheet->getActiveSheet();
- $activeRange = $spreadsheet->getActiveSheet()->calculateWorksheetDataDimension();
- $sheetData = $spreadsheet->getActiveSheet()->rangeToArray($activeRange, null, true, true, true);
- $helper->displayGrid($sheetData);
+ $activeRange = $sheet->calculateWorksheetDataDimension();
+ $sheet->getStyle($activeRange)->getNumberFormat()
+ ->setFormatCode('0.000');
+ $sheetData = $sheet->rangeToArray($activeRange, null, true, true, true);
+ $helper->displayGrid($sheetData, true);
}
diff --git a/samples/Table/02_Table_Total.php b/samples/Table/02_Table_Total.php
index 7c5964548f..c32c15c049 100644
--- a/samples/Table/02_Table_Total.php
+++ b/samples/Table/02_Table_Total.php
@@ -76,7 +76,7 @@
$helper->log('Calculate Structured References');
-$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true));
+$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true), true);
// Save
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
diff --git a/samples/Table/04_Column_Formula_with_Totals.php b/samples/Table/04_Column_Formula_with_Totals.php
index c3e7a57f6b..8cc9465d17 100644
--- a/samples/Table/04_Column_Formula_with_Totals.php
+++ b/samples/Table/04_Column_Formula_with_Totals.php
@@ -86,7 +86,7 @@
$helper->log('Calculate Structured References');
-$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true));
+$helper->displayGrid($spreadsheet->getActiveSheet()->toArray(null, true, true, true), true);
// Save
$helper->write($spreadsheet, __FILE__, ['Xlsx']);
diff --git a/src/PhpSpreadsheet/Helper/Sample.php b/src/PhpSpreadsheet/Helper/Sample.php
index 729d5d736a..5933f74695 100644
--- a/src/PhpSpreadsheet/Helper/Sample.php
+++ b/src/PhpSpreadsheet/Helper/Sample.php
@@ -242,9 +242,12 @@ public function titles(string $category, string $functionName, ?string $descript
}
/** @param mixed[][] $matrix */
- public function displayGrid(array $matrix): void
+ public function displayGrid(array $matrix, ?bool $numbersRight = null): void
{
$renderer = new TextGrid($matrix, $this->isCli());
+ if ($numbersRight !== null) {
+ $renderer->setNumbersRight($numbersRight);
+ }
echo $renderer->render();
}
diff --git a/src/PhpSpreadsheet/Helper/TextGrid.php b/src/PhpSpreadsheet/Helper/TextGrid.php
index 9c64c617cc..d9b78a0975 100644
--- a/src/PhpSpreadsheet/Helper/TextGrid.php
+++ b/src/PhpSpreadsheet/Helper/TextGrid.php
@@ -6,7 +6,7 @@
class TextGrid
{
- private bool $isCli;
+ protected bool $isCli;
/** @var mixed[][] */
protected array $matrix;
@@ -17,16 +17,18 @@ class TextGrid
/** @var string[] */
protected array $columns;
- private string $gridDisplay;
+ protected string $gridDisplay;
- private bool $rowDividers = false;
+ protected bool $rowDividers = false;
- private bool $rowHeaders = true;
+ protected bool $rowHeaders = true;
- private bool $columnHeaders = true;
+ protected bool $columnHeaders = true;
+
+ protected bool $numbersRight = false;
/** @param mixed[][] $matrix */
- public function __construct(array $matrix, bool $isCli = true, bool $rowDividers = false, bool $rowHeaders = true, bool $columnHeaders = true)
+ public function __construct(array $matrix, bool $isCli = true, bool $rowDividers = false, bool $rowHeaders = true, bool $columnHeaders = true, bool $numbersRight = false)
{
$this->rows = array_keys($matrix);
$this->columns = array_keys($matrix[$this->rows[0]]);
@@ -44,6 +46,12 @@ function (&$row): void {
$this->rowDividers = $rowDividers;
$this->rowHeaders = $rowHeaders;
$this->columnHeaders = $columnHeaders;
+ $this->numbersRight = $numbersRight;
+ }
+
+ public function setNumbersRight(bool $numbersRight): void
+ {
+ $this->numbersRight = $numbersRight;
}
public function render(): string
@@ -52,7 +60,7 @@ public function render(): string
if (!empty($this->rows)) {
$maxRow = max($this->rows);
- $maxRowLength = strlen((string) $maxRow) + 1;
+ $maxRowLength = $this->strlen((string) $maxRow) + 1;
$columnWidths = $this->getColumnWidths();
$this->renderColumnHeader($maxRowLength, $columnWidths);
@@ -68,7 +76,7 @@ public function render(): string
}
/** @param int[] $columnWidths */
- private function renderRows(int $maxRowLength, array $columnWidths): void
+ protected function renderRows(int $maxRowLength, array $columnWidths): void
{
foreach ($this->matrix as $row => $rowData) {
if ($this->rowHeaders) {
@@ -86,18 +94,27 @@ private function renderRows(int $maxRowLength, array $columnWidths): void
* @param mixed[] $rowData
* @param int[] $columnWidths
*/
- private function renderCells(array $rowData, array $columnWidths): void
+ protected function renderCells(array $rowData, array $columnWidths): void
{
foreach ($rowData as $column => $cell) {
$valueForLength = $this->getString($cell);
$displayCell = $this->isCli ? $valueForLength : htmlentities($valueForLength);
$this->gridDisplay .= '| ';
- $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
+ if ($this->rightAlign($displayCell)) {
+ $this->gridDisplay .= str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength)) . $displayCell . ' ';
+ } else {
+ $this->gridDisplay .= $displayCell . str_repeat(' ', $columnWidths[$column] - $this->strlen($valueForLength) + 1);
+ }
}
}
+ protected function rightAlign(string $displayCell): bool
+ {
+ return $this->numbersRight && is_numeric($displayCell);
+ }
+
/** @param int[] $columnWidths */
- private function renderColumnHeader(int $maxRowLength, array &$columnWidths): void
+ protected function renderColumnHeader(int $maxRowLength, array &$columnWidths): void
{
if (!$this->columnHeaders) {
$this->renderFooter($maxRowLength, $columnWidths);
@@ -129,7 +146,7 @@ private function renderColumnHeader(int $maxRowLength, array &$columnWidths): vo
}
/** @param int[] $columnWidths */
- private function renderFooter(int $maxRowLength, array $columnWidths): void
+ protected function renderFooter(int $maxRowLength, array $columnWidths): void
{
if ($this->rowHeaders) {
$this->gridDisplay .= '+' . str_repeat('-', $maxRowLength + 1);
@@ -142,7 +159,7 @@ private function renderFooter(int $maxRowLength, array $columnWidths): void
}
/** @return int[] */
- private function getColumnWidths(): array
+ protected function getColumnWidths(): array
{
$columnCount = count($this->matrix, COUNT_RECURSIVE) / count($this->matrix);
$columnWidths = [];
@@ -154,7 +171,7 @@ private function getColumnWidths(): array
}
/** @param mixed[] $columnData */
- private function getColumnWidth(array $columnData): int
+ protected function getColumnWidth(array $columnData): int
{
$columnWidth = 0;
$columnData = array_values($columnData);
diff --git a/tests/PhpSpreadsheetTests/Helper/TextGridExtended.php b/tests/PhpSpreadsheetTests/Helper/TextGridExtended.php
new file mode 100644
index 0000000000..32ab95d8c8
--- /dev/null
+++ b/tests/PhpSpreadsheetTests/Helper/TextGridExtended.php
@@ -0,0 +1,16 @@
+numbersRight && preg_match('/^[-+$,.0-9]+$/', $displayCell);
+ }
+}
diff --git a/tests/PhpSpreadsheetTests/Helper/TextGridTest.php b/tests/PhpSpreadsheetTests/Helper/TextGridTest.php
index 2363260290..bf12aefea4 100644
--- a/tests/PhpSpreadsheetTests/Helper/TextGridTest.php
+++ b/tests/PhpSpreadsheetTests/Helper/TextGridTest.php
@@ -161,4 +161,108 @@ public function testBool(): void
self::assertSame($expected, $lines);
$spreadsheet->disconnectWorksheets();
}
+
+ public function testNumbersDefault(): void
+ {
+ $spreadsheet = new Spreadsheet();
+ $sheet = $spreadsheet->getActiveSheet();
+ $sheet->fromArray([
+ [0, 1, 'string1'],
+ [18, 13.25, 'string2'],
+ [100, 2.5, 'string12'],
+ ], strictNullComparison: true);
+ $sheet->getStyle('B1:B3')->getNumberFormat()
+ ->setFormatCode('0.00');
+ /** @var mixed[][] */
+ $temp = $sheet->toArray(null, true, true, true);
+ $textGrid = new TextGrid(
+ $temp,
+ true,
+ rowDividers: false,
+ rowHeaders: false,
+ columnHeaders: false,
+ );
+ $expected = [
+ '+-----+-------+----------+',
+ '| 0 | 1.00 | string1 |',
+ '| 18 | 13.25 | string2 |',
+ '| 100 | 2.50 | string12 |',
+ '+-----+-------+----------+',
+ '',
+ ];
+ $result = $textGrid->render();
+ $lines = explode(PHP_EOL, $result);
+ self::assertSame($expected, $lines);
+ $spreadsheet->disconnectWorksheets();
+ }
+
+ public function testNumbersRight(): void
+ {
+ $spreadsheet = new Spreadsheet();
+ $sheet = $spreadsheet->getActiveSheet();
+ $sheet->fromArray([
+ [0, 1, '$1,234.56'],
+ [18, 13.25, 'string2'],
+ [100, 2.5, 'string123456'],
+ ], strictNullComparison: true);
+ $sheet->getStyle('B1:B3')->getNumberFormat()
+ ->setFormatCode('0.00');
+ /** @var mixed[][] */
+ $temp = $sheet->toArray(null, true, true, true);
+ $textGrid = new TextGrid(
+ $temp,
+ true,
+ rowDividers: false,
+ rowHeaders: false,
+ columnHeaders: false,
+ numbersRight: true,
+ );
+ $expected = [
+ '+-----+-------+--------------+',
+ '| 0 | 1.00 | $1,234.56 |',
+ '| 18 | 13.25 | string2 |',
+ '| 100 | 2.50 | string123456 |',
+ '+-----+-------+--------------+',
+ '',
+ ];
+ $result = $textGrid->render();
+ $lines = explode(PHP_EOL, $result);
+ self::assertSame($expected, $lines);
+ $spreadsheet->disconnectWorksheets();
+ }
+
+ public function testExtendedNumbersRight(): void
+ {
+ $spreadsheet = new Spreadsheet();
+ $sheet = $spreadsheet->getActiveSheet();
+ $sheet->fromArray([
+ [0, 1, '$1,234.56'],
+ [18, 13.25, 'string2'],
+ [100, 2.5, 'string123456'],
+ ], strictNullComparison: true);
+ $sheet->getStyle('B1:B3')->getNumberFormat()
+ ->setFormatCode('0.00');
+ /** @var mixed[][] */
+ $temp = $sheet->toArray(null, true, true, true);
+ $textGrid = new TextGridExtended(
+ $temp,
+ true,
+ rowDividers: false,
+ rowHeaders: false,
+ columnHeaders: false,
+ );
+ $textGrid->setNumbersRight(true);
+ $expected = [
+ '+-----+-------+--------------+',
+ '| 0 | 1.00 | $1,234.56 |',
+ '| 18 | 13.25 | string2 |',
+ '| 100 | 2.50 | string123456 |',
+ '+-----+-------+--------------+',
+ '',
+ ];
+ $result = $textGrid->render();
+ $lines = explode(PHP_EOL, $result);
+ self::assertSame($expected, $lines);
+ $spreadsheet->disconnectWorksheets();
+ }
}