Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ods Boolean Data #4093

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Reader/Ods.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ public function loadIntoExisting(string $filename, Spreadsheet $spreadsheet): Sp
break;
case 'boolean':
$type = DataType::TYPE_BOOL;
$dataValue = ($allCellDataText == 'TRUE') ? true : false;
$dataValue = ($cellData->getAttributeNS($officeNs, 'boolean-value') === 'true') ? true : false;

break;
case 'percentage':
Expand Down
5 changes: 3 additions & 2 deletions src/PhpSpreadsheet/Writer/Ods/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Writer\Ods;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalculationException;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
Expand Down Expand Up @@ -209,8 +210,8 @@ private function writeCells(XMLWriter $objWriter, RowCellIterator $cells): void
switch ($cell->getDataType()) {
case DataType::TYPE_BOOL:
$objWriter->writeAttribute('office:value-type', 'boolean');
$objWriter->writeAttribute('office:value', $cell->getValueString());
$objWriter->writeElement('text:p', $cell->getValueString());
$objWriter->writeAttribute('office:boolean-value', $cell->getValue() ? 'true' : 'false');
$objWriter->writeElement('text:p', Calculation::getInstance()->getLocaleBoolean($cell->getValue() ? 'TRUE' : 'FALSE'));

break;
case DataType::TYPE_ERROR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,15 @@ class CalculationFunctionListTest extends TestCase
{
private string $compatibilityMode;

private string $locale;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$calculation = Calculation::getInstance();
$this->locale = $calculation->getLocale();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
$calculation = Calculation::getInstance();
$calculation->setLocale($this->locale);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions tests/PhpSpreadsheetTests/Calculation/CalculationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ class CalculationTest extends TestCase
{
private string $compatibilityMode;

private string $locale;

protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$calculation = Calculation::getInstance();
$this->locale = $calculation->getLocale();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
}

protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
$calculation = Calculation::getInstance();
$calculation->setLocale($this->locale);
}

/**
Expand Down
83 changes: 83 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Ods/BooleanDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace PhpOffice\PhpSpreadsheetTests\Reader\Ods;

use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Reader\Ods as OdsReader;
use PhpOffice\PhpSpreadsheet\Shared\File;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Ods as OdsWriter;
use PHPUnit\Framework\TestCase;

class BooleanDataTest extends TestCase
{
private string $tempfile = '';

private string $locale;

protected function setUp(): void
{
$calculation = Calculation::getInstance();
$this->locale = $calculation->getLocale();
}

protected function tearDown(): void
{
$calculation = Calculation::getInstance();
$calculation->setLocale($this->locale);
if ($this->tempfile !== '') {
unlink($this->tempfile);
$this->tempfile = '';
}
}

public function testBooleanData(): void
{
$spreadsheetOld = new Spreadsheet();
$sheetOld = $spreadsheetOld->getActiveSheet();
$sheetOld->getCell('A1')->setValue(true);
$sheetOld->getCell('A2')->setValue(false);
$writer = new OdsWriter($spreadsheetOld);
$this->tempfile = File::temporaryFileName();
$writer->save($this->tempfile);
$spreadsheetOld->disconnectWorksheets();
$reader = new OdsReader();
$spreadsheet = $reader->load($this->tempfile);
$sheet = $spreadsheet->getActiveSheet();
self::assertTrue($sheet->getCell('A1')->getValue());
self::assertFalse($sheet->getCell('A2')->getValue());
$spreadsheet->disconnectWorksheets();
$zipFile = 'zip://' . $this->tempfile . '#content.xml';
$contents = (string) file_get_contents($zipFile);
self::assertStringContainsString('<text:p>TRUE</text:p>', $contents);
self::assertStringContainsString('<text:p>FALSE</text:p>', $contents);
}

public function testBooleanDataGerman(): void
{
$calculation = Calculation::getInstance();
$calculation->setLocale('de');
$spreadsheetOld = new Spreadsheet();
$sheetOld = $spreadsheetOld->getActiveSheet();
$sheetOld->getCell('A1')->setValue(true);
$sheetOld->getCell('A2')->setValue(false);
$writer = new OdsWriter($spreadsheetOld);
$this->tempfile = File::temporaryFileName();
$writer->save($this->tempfile);
$spreadsheetOld->disconnectWorksheets();
$reader = new OdsReader();
$spreadsheet = $reader->load($this->tempfile);
$sheet = $spreadsheet->getActiveSheet();
self::assertTrue($sheet->getCell('A1')->getValue());
self::assertFalse($sheet->getCell('A2')->getValue());
$spreadsheet->disconnectWorksheets();
$zipFile = 'zip://' . $this->tempfile . '#content.xml';
$contents = (string) file_get_contents($zipFile);
self::assertStringContainsString('<text:p>WAHR</text:p>', $contents);
self::assertStringContainsString('<text:p>FALSCH</text:p>', $contents);
self::assertStringNotContainsString('<text:p>TRUE</text:p>', $contents);
self::assertStringNotContainsString('<text:p>FALSE</text:p>', $contents);
}
}
8 changes: 4 additions & 4 deletions tests/data/Writer/Ods/content-with-data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@
<table:table-cell table:number-columns-repeated="1017"/>
</table:table-row>
<table:table-row>
<table:table-cell office:value="1" office:value-type="boolean" table:style-name="ce0">
<text:p>1</text:p>
<table:table-cell office:boolean-value="true" office:value-type="boolean" table:style-name="ce0">
<text:p>TRUE</text:p>
</table:table-cell>
<table:table-cell office:value="" office:value-type="boolean" table:style-name="ce0">
<text:p/>
<table:table-cell office:boolean-value="false" office:value-type="boolean" table:style-name="ce0">
<text:p>FALSE</text:p>
</table:table-cell>
<table:table-cell office:value="1 1" office:value-type="string" table:formula="of:=IF([.A3]; CONCATENATE([.A1]; &quot; &quot;; [.A2]); CONCATENATE([.A2]; &quot; &quot;; [.A1]))" table:style-name="ce10">
<text:p>1 1</text:p>
Expand Down
Loading