Skip to content

Ods writer and reader handle bool values differently (with diff of potential fix) #460

@discordier

Description

@discordier

This is:

- [X] a bug report
- [X] **not** a usage question (ask them on https://stackoverflow.com/questions/tagged/phpspreadsheet or https://gitter.im/PHPOffice/PhpSpreadsheet)

What is the expected behavior?

Ods reader should read boolean values properly.

What is the current behavior?

Ods reader can not read a boolean value created via Ods Writer.

What are the steps to reproduce?

<?php

require __DIR__ . '/vendor/autoload.php';

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...
$sheet = $spreadsheet->getActiveSheet();
// Set the boolean value to true.
$sheet->getCellByColumnAndRow(1, 1)->setValue(true);

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Ods($spreadsheet);
$writer->save('test.ods');

$reader = new \PhpOffice\PhpSpreadsheet\Reader\Ods();
$spreadsheet2 = $reader->load('test.ods')
$sheet2 = $spreadsheet2->getActiveSheet();
// This now returns false instead of true.
$sheet2->getCellByColumnAndRow(1, 1)->getValue();

Can be fixed with src/PhpSpreadsheet/Reader/Ods.php (at line 529):

 case 'boolean':
     $type = DataType::TYPE_BOOL;
-   $dataValue = ($allCellDataText == 'TRUE') ? true : false;
+   $dataValue = (bool) $cellData->getAttributeNS($officeNs, 'value');

Which versions of PhpSpreadsheet and PHP are affected?

Probably all?

Elaboration on the generated and expected XML contents in the Ods file.

The PhpSpreadsheet Ods reader for a boolean cell expects (formatted for better readability):

<table:table-cell
  office:value-type="boolean"
  office:boolean-value="true"
  calcext:value-type="boolean">
  <text:p>TRUE</text:p>
</table:table-cell>

The generated content by PhpSpreadsheet Ods writer for a boolean cell is (formatted for better readability):

<table:table-cell
  table:style-name="ce1"
  office:value-type="boolean"
  office:value="1">
  <text:p>1</text:p>
</table:table-cell>

The generated content by LibreOffice 6.0.2.1.0+ with non english locale (German in this case) for a boolean cell is (formatted for better readability):

<table:table-cell
  office:value-type="boolean"
  office:boolean-value="true"
  calcext:value-type="boolean">
  <text:p>WAHR</text:p>
</table:table-cell>

Hence the hard coded value of TRUE is not good at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions