diff --git a/src/PhpSpreadsheet/Calculation/Calculation.php b/src/PhpSpreadsheet/Calculation/Calculation.php index 0cc64c32e8..ab52fb1af9 100644 --- a/src/PhpSpreadsheet/Calculation/Calculation.php +++ b/src/PhpSpreadsheet/Calculation/Calculation.php @@ -3141,6 +3141,53 @@ private function getLocaleFile(string $localeDir, string $locale, string $langua return $localeFileName; } + /** @var array> */ + private static array $falseTrueArray = []; + + /** @return array> */ + public function getFalseTrueArray(): array + { + if (!empty(self::$falseTrueArray)) { + return self::$falseTrueArray; + } + if (count(self::$validLocaleLanguages) == 1) { + self::loadLocales(); + } + $falseTrueArray = [['FALSE'], ['TRUE']]; + foreach (self::$validLocaleLanguages as $language) { + if (str_starts_with($language, 'en')) { + continue; + } + $locale = $language; + if (str_contains($locale, '_')) { + [$language] = explode('_', $locale); + } + $localeDir = implode(DIRECTORY_SEPARATOR, [__DIR__, 'locale', null]); + + try { + $functionNamesFile = $this->getLocaleFile($localeDir, $locale, $language, 'functions'); + } catch (Exception $e) { + continue; + } + // Retrieve the list of locale or language specific function names + $localeFunctions = file($functionNamesFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: []; + foreach ($localeFunctions as $localeFunction) { + [$localeFunction] = explode('##', $localeFunction); // Strip out comments + if (str_contains($localeFunction, '=')) { + [$fName, $lfName] = array_map('trim', explode('=', $localeFunction)); + if ($fName === 'FALSE') { + $falseTrueArray[0][] = $lfName; + } elseif ($fName === 'TRUE') { + $falseTrueArray[1][] = $lfName; + } + } + } + } + self::$falseTrueArray = $falseTrueArray; + + return $falseTrueArray; + } + /** * Set the locale code. * diff --git a/src/PhpSpreadsheet/Reader/Html.php b/src/PhpSpreadsheet/Reader/Html.php index 4ca781a87d..045e6b6b6e 100644 --- a/src/PhpSpreadsheet/Reader/Html.php +++ b/src/PhpSpreadsheet/Reader/Html.php @@ -7,6 +7,7 @@ use DOMElement; use DOMNode; use DOMText; +use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Comment; @@ -271,6 +272,12 @@ protected function flushCell(Worksheet $sheet, string $column, int|string $row, ->setQuotePrefix(true); } } + if ($datatype === DataType::TYPE_BOOL) { + $cellContent = self::convertBoolean($cellContent); + if (!is_bool($cellContent)) { + $attributeArray['data-type'] = DataType::TYPE_STRING; + } + } //catching the Exception and ignoring the invalid data types try { @@ -291,6 +298,31 @@ protected function flushCell(Worksheet $sheet, string $column, int|string $row, $cellContent = (string) ''; } + /** @var array> */ + private static array $falseTrueArray = []; + + private function convertBoolean(?string $cellContent): bool|string + { + if ($cellContent === '1') { + return true; + } + if ($cellContent === '0' || $cellContent === '' || $cellContent === null) { + return false; + } + if (empty(self::$falseTrueArray)) { + $calc = Calculation::getInstance(); + self::$falseTrueArray = $calc->getFalseTrueArray(); + } + if (in_array(mb_strtoupper($cellContent), self::$falseTrueArray[1], true)) { + return true; + } + if (in_array(mb_strtoupper($cellContent), self::$falseTrueArray[0], true)) { + return false; + } + + return $cellContent; + } + private function processDomElementBody(Worksheet $sheet, int &$row, string &$column, string &$cellContent, DOMElement $child): void { $attributeArray = []; diff --git a/src/PhpSpreadsheet/Writer/Html.php b/src/PhpSpreadsheet/Writer/Html.php index 1b2d33abff..f85b5f0961 100644 --- a/src/PhpSpreadsheet/Writer/Html.php +++ b/src/PhpSpreadsheet/Writer/Html.php @@ -5,6 +5,7 @@ use PhpOffice\PhpSpreadsheet\Calculation\Calculation; use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Cell\Coordinate; +use PhpOffice\PhpSpreadsheet\Cell\DataType; use PhpOffice\PhpSpreadsheet\Chart\Chart; use PhpOffice\PhpSpreadsheet\Comment; use PhpOffice\PhpSpreadsheet\Document\Properties; @@ -36,6 +37,9 @@ class Html extends BaseWriter private const DEFAULT_CELL_WIDTH_PIXELS = 56; + private const TRUE_SUBSTITUTE = "\u{fffe}"; + private const FALSE_SUBSTITUTE = "\u{feff}"; + /** * Migration aid to tell if html tags will be treated as plaintext in comments. * if ( @@ -141,6 +145,12 @@ class Html extends BaseWriter /** @var Chart[] */ private $sheetCharts; + private bool $betterBoolean = false; + + private string $getTrue = 'TRUE'; + + private string $getFalse = 'FALSE'; + /** * Create a new HTML. */ @@ -148,6 +158,9 @@ public function __construct(Spreadsheet $spreadsheet) { $this->spreadsheet = $spreadsheet; $this->defaultFont = $this->spreadsheet->getDefaultStyle()->getFont(); + $calc = Calculation::getInstance($this->spreadsheet); + $this->getTrue = $calc->getTRUE(); + $this->getFalse = $calc->getFALSE(); } /** @@ -1346,8 +1359,21 @@ private function generateRowCellDataValue(Worksheet $worksheet, Cell $cell, stri if ($cell->getValue() instanceof RichText) { $cellData .= $this->generateRowCellDataValueRich($cell->getValue()); } else { - $origData = $this->preCalculateFormulas ? $cell->getCalculatedValue() : $cell->getValue(); - $origData2 = $this->preCalculateFormulas ? $cell->getCalculatedValueString() : $cell->getValueString(); + if ($this->preCalculateFormulas) { + $origData = $cell->getCalculatedValue(); + if ($this->betterBoolean && is_bool($origData)) { + $origData2 = $origData ? self::TRUE_SUBSTITUTE : self::FALSE_SUBSTITUTE; + } else { + $origData2 = $cell->getCalculatedValueString(); + } + } else { + $origData = $cell->getValue(); + if ($this->betterBoolean && is_bool($origData)) { + $origData2 = $origData ? self::TRUE_SUBSTITUTE : self::FALSE_SUBSTITUTE; + } else { + $origData2 = $cell->getValueString(); + } + } $formatCode = $worksheet->getParentOrThrow()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode(); $cellData = NumberFormat::toFormattedString( @@ -1448,6 +1474,13 @@ private function generateRowWriteCell( $htmlx .= $this->generateRowIncludeCharts($worksheet, $coordinate); // Column start $html .= ' <' . $cellType; + if ($cellData === self::TRUE_SUBSTITUTE) { + $html .= ' data-type="' . DataType::TYPE_BOOL . '"'; + $cellData = $this->getTrue; + } elseif ($cellData === self::FALSE_SUBSTITUTE) { + $html .= ' data-type="' . DataType::TYPE_BOOL . '"'; + $cellData = $this->getFalse; + } if (!$this->useInlineCss && !$this->isPdf && is_string($cssClass)) { $html .= ' class="' . $cssClass . '"'; if ($htmlx) { @@ -1903,4 +1936,16 @@ private function shouldGenerateColumn(Worksheet $sheet, string $colStr): bool return $sheet->getColumnDimension($colStr)->getVisible(); } + + public function getBetterBoolean(): bool + { + return $this->betterBoolean; + } + + public function setBetterBoolean(bool $betterBoolean): self + { + $this->betterBoolean = $betterBoolean; + + return $this; + } } diff --git a/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php b/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php new file mode 100644 index 0000000000..558dc7ed34 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Html/BetterBooleanTest.php @@ -0,0 +1,140 @@ +locale = $calculation->getLocale(); + } + + protected function tearDown(): void + { + $calculation = Calculation::getInstance(); + $calculation->setLocale($this->locale); + } + + public function testDefault(): void + { + $spreadsheet = new Spreadsheet(); + $writer = new HtmlWriter($spreadsheet); + // Default will change with next PhpSpreadsheet release + self::assertFalse($writer->getBetterBoolean()); + $spreadsheet->disconnectWorksheets(); + } + + public function setBetter(HtmlWriter $writer): void + { + $writer->setBetterBoolean(true); + } + + public function setNotBetter(HtmlWriter $writer): void + { + $writer->setBetterBoolean(false); + } + + public function testBetterBoolean(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getCell('A1')->setValue(10); + $sheet->getCell('B1')->setValue('Hello'); + $sheet->getCell('C1')->setValue(true); + $sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); + + /** @var callable */ + $callableWriter = [$this, 'setBetter']; + $reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); + $spreadsheet->disconnectWorksheets(); + + $rsheet = $reloaded->getActiveSheet(); + self::assertSame(10, $rsheet->getCell('A1')->getValue()); + self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); + self::assertTrue($rsheet->getCell('C1')->getValue()); + self::assertFalse($rsheet->getCell('D1')->getValue()); + $reloaded->disconnectWorksheets(); + } + + public function testNotBetterBoolean(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getCell('A1')->setValue(10); + $sheet->getCell('B1')->setValue('Hello'); + $sheet->getCell('C1')->setValue(true); + $sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); + + /** @var callable */ + $callableWriter = [$this, 'setNotBetter']; + $reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); + $spreadsheet->disconnectWorksheets(); + + $rsheet = $reloaded->getActiveSheet(); + self::assertSame(10, $rsheet->getCell('A1')->getValue()); + self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); + self::assertSame(1, $rsheet->getCell('C1')->getValue()); + self::assertNull($rsheet->getCell('D1')->getValue()); + $reloaded->disconnectWorksheets(); + } + + public function testLocale(): void + { + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->getCell('A1')->setValue(10); + $sheet->getCell('B1')->setValue('Hello'); + $sheet->getCell('C1')->setValue(true); + $sheet->getCell('D1')->setValue('=IF(1>2, TRUE, FALSE)'); + $calc = Calculation::getInstance(); + $calc->setLocale('fr'); + $writer = new HtmlWriter($spreadsheet); + $writer->setBetterBoolean(true); + $html = $writer->generateHtmlAll(); + self::assertStringContainsString('VRAI', $html); + self::assertStringNotContainsString('TRUE', $html); + + /** @var callable */ + $callableWriter = [$this, 'setBetter']; + $reloaded = $this->writeAndReload($spreadsheet, 'Html', null, $callableWriter); + $spreadsheet->disconnectWorksheets(); + + $rsheet = $reloaded->getActiveSheet(); + self::assertSame(10, $rsheet->getCell('A1')->getValue()); + self::assertSame('Hello', $rsheet->getCell('B1')->getValue()); + self::assertTrue($rsheet->getCell('C1')->getValue()); + self::assertFalse($rsheet->getCell('D1')->getValue()); + $reloaded->disconnectWorksheets(); + } + + public function testForeignNoLocale(): void + { + $fragment = '' + . '' + . '' + . '' // Bulgarian TRUE + . '' // Finnish FALSE + . '' + . '' + . '
10HelloИСТИНАEPÄTOSIwhatevertRuE
'; + $reader = new HtmlReader(); + $spreadsheet = $reader->loadFromString($fragment); + $sheet = $spreadsheet->getActiveSheet(); + self::assertTrue($sheet->getCell('C1')->getValue()); + self::assertFalse($sheet->getCell('D1')->getValue()); + self::assertSame('whatever', $sheet->getCell('E1')->getValue()); + self::assertTrue($sheet->getCell('F1')->getValue()); + $spreadsheet->disconnectWorksheets(); + } +}