Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #113 from box/all_ods_cell_types
Browse files Browse the repository at this point in the history
Support all ODS cell types
  • Loading branch information
adrilo committed Sep 2, 2015
2 parents aa79781 + 818ec24 commit 6124528
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 34 deletions.
123 changes: 92 additions & 31 deletions src/Spout/Reader/ODS/Helper/CellValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ class CellValueFormatter
{
/** Definition of all possible cell types */
const CELL_TYPE_STRING = 'string';
const CELL_TYPE_BOOLEAN = 'boolean';
const CELL_TYPE_FLOAT = 'float';
const CELL_TYPE_BOOLEAN = 'boolean';
const CELL_TYPE_DATE = 'date';
const CELL_TYPE_TIME = 'time';
const CELL_TYPE_CURRENCY = 'currency';
const CELL_TYPE_PERCENTAGE = 'percentage';
const CELL_TYPE_VOID = 'void';

/** Definition of XML nodes names used to parse data */
const XML_NODE_P = 'p';
const XML_NODE_S = 'text:s';

/** Definition of XML attribute used to parse data */
const XML_ATTRIBUTE_TYPE = 'office:value-type';
const XML_ATTRIBUTE_VALUE = 'office:value';
const XML_ATTRIBUTE_BOOLEAN_VALUE = 'office:boolean-value';
const XML_ATTRIBUTE_DATE_VALUE = 'office:date-value';
const XML_ATTRIBUTE_TIME_VALUE = 'office:time-value';
const XML_ATTRIBUTE_CURRENCY = 'office:currency';
const XML_ATTRIBUTE_C = 'text:c';

/** @var \Box\Spout\Common\Escaper\ODS Used to unescape XML data */
Expand All @@ -38,45 +48,36 @@ public function __construct()
/**
* Returns the (unescaped) correctly marshalled, cell value associated to the given XML node.
* @TODO Add other types !!
* @see http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
*
* @param \DOMNode $node
* @return string|int|float|bool The value associated with the cell (or empty string if cell's type is undefined)
* @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error
*/
public function extractAndFormatNodeValue($node)
{
$cellType = $node->getAttribute(self::XML_ATTRIBUTE_TYPE);
$pNodeValue = $this->getFirstPNodeValue($node);

switch ($cellType) {
case self::CELL_TYPE_STRING:
return $this->formatStringCellValue($node);
case self::CELL_TYPE_FLOAT:
return $this->formatFloatCellValue($pNodeValue);
return $this->formatFloatCellValue($node);
case self::CELL_TYPE_BOOLEAN:
return $this->formatBooleanCellValue($pNodeValue);
return $this->formatBooleanCellValue($node);
case self::CELL_TYPE_DATE:
return $this->formatDateCellValue($node);
case self::CELL_TYPE_TIME:
return $this->formatTimeCellValue($node);
case self::CELL_TYPE_CURRENCY:
return $this->formatCurrencyCellValue($node);
case self::CELL_TYPE_PERCENTAGE:
return $this->formatPercentageCellValue($node);
case self::CELL_TYPE_VOID:
default:
return '';
}
}

/**
* Returns the value of the first "<text:p>" node within the given node.
*
* @param \DOMNode $node
* @return string Value for the first "<text:p>" node or empty string if no "<text:p>" found
*/
protected function getFirstPNodeValue($node)
{
$nodeValue = '';
$pNodes = $node->getElementsByTagName(self::XML_NODE_P);

if ($pNodes->length > 0) {
$nodeValue = $pNodes->item(0)->nodeValue;
}

return $nodeValue;
}

/**
* Returns the cell String value.
*
Expand Down Expand Up @@ -110,27 +111,87 @@ protected function formatStringCellValue($node)
}

/**
* Returns the cell Numeric value from string of nodeValue.
* Returns the cell Numeric value from the given node.
*
* @param string $pNodeValue
* @param \DOMNode $node
* @return int|float The value associated with the cell
*/
protected function formatFloatCellValue($pNodeValue)
protected function formatFloatCellValue($node)
{
$cellValue = is_int($pNodeValue) ? intval($pNodeValue) : floatval($pNodeValue);
$nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_VALUE);
$cellValue = is_int($nodeValue) ? intval($nodeValue) : floatval($nodeValue);
return $cellValue;
}

/**
* Returns the cell Boolean value from a specific node's Value.
* Returns the cell Boolean value from the given node.
*
* @param string $pNodeValue
* @param \DOMNode $node
* @return bool The value associated with the cell
*/
protected function formatBooleanCellValue($pNodeValue)
protected function formatBooleanCellValue($node)
{
$nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_BOOLEAN_VALUE);
// !! is similar to boolval()
$cellValue = !!$pNodeValue;
$cellValue = !!$nodeValue;
return $cellValue;
}

/**
* Returns the cell Date value from the given node.
*
* @param \DOMNode $node
* @return \DateTime|null The value associated with the cell or NULL if invalid date value
*/
protected function formatDateCellValue($node)
{
try {
$nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_DATE_VALUE);
return new \DateTime($nodeValue);
} catch (\Exception $e) {
return null;
}
}

/**
* Returns the cell Time value from the given node.
*
* @param \DOMNode $node
* @return \DateInterval|null The value associated with the cell or NULL if invalid time value
*/
protected function formatTimeCellValue($node)
{
try {
$nodeValue = $node->getAttribute(self::XML_ATTRIBUTE_TIME_VALUE);
return new \DateInterval($nodeValue);
} catch (\Exception $e) {
return null;
}
}

/**
* Returns the cell Currency value from the given node.
*
* @param \DOMNode $node
* @return string The value associated with the cell (e.g. "100 USD" or "9.99 EUR")
*/
protected function formatCurrencyCellValue($node)
{
$value = $node->getAttribute(self::XML_ATTRIBUTE_VALUE);
$currency = $node->getAttribute(self::XML_ATTRIBUTE_CURRENCY);

return "$value $currency";
}

/**
* Returns the cell Percentage value from the given node.
*
* @param \DOMNode $node
* @return int|float The value associated with the cell
*/
protected function formatPercentageCellValue($node)
{
// percentages are formatted like floats
return $this->formatFloatCellValue($node);
}
}
2 changes: 1 addition & 1 deletion src/Spout/Reader/ODS/RowIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ protected function getNumColumnsRepeatedForCurrentNode()
* Returns the (unescaped) correctly marshalled, cell value associated to the given XML node.
*
* @param \DOMNode $node
* @return string|int|float|bool The value associated with the cell (or empty string if cell's type is undefined)
* @return string|int|float|bool|\DateTime|\DateInterval|null The value associated with the cell, empty string if cell's type is void/undefined, null on error
*/
protected function getCellValue($node)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Reader/XLSX/Helper/CellValueFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ protected function formatBooleanCellValue($nodeValue)
* Returns a cell's PHP Date value, associated to the given stored nodeValue.
*
* @param string $nodeValue
* @return \DateTime|null The value associated with the cell (null when the cell has an error)
* @return \DateTime|null The value associated with the cell or NULL if invalid date value
*/
protected function formatDateCellValue($nodeValue)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Spout/Writer/ODS/Internal/Worksheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected function getCellContent($cellValue, $styleIndex, $numTimesValueRepeate

$data .= '</table:table-cell>';
} else if (CellHelper::isBoolean($cellValue)) {
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:value="' . $cellValue . '">';
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellValue . '">';
$data .= '<text:p>' . $cellValue . '</text:p>';
$data .= '</table:table-cell>';
} else if (CellHelper::isNumeric($cellValue)) {
Expand Down
17 changes: 17 additions & 0 deletions tests/Spout/Reader/ODS/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,21 @@ public function testReadWithFilesGeneratedByExternalSoftwares($fileName, $skipLa
*/
public function testReadShouldSupportAllCellTypes()
{
$utcTz = new \DateTimeZone('UTC');
$honoluluTz = new \DateTimeZone('Pacific/Honolulu'); // UTC-10

$allRows = $this->getAllRowsForFile('sheet_with_all_cell_types.ods');

$expectedRows = [
[
'ods--11', 'ods--12',
true, false,
0, 10.43,
new \DateTime('1987-11-29T00:00:00', $utcTz), new \DateTime('1987-11-29T13:37:00', $utcTz),
new \DateTime('1987-11-29T13:37:00', $utcTz), new \DateTime('1987-11-29T13:37:00', $honoluluTz),
new \DateInterval('PT13H37M00S'),
0, 0.42,
'42 USD', '9.99 EUR',
'',
],
];
Expand All @@ -165,6 +173,15 @@ public function testReadShouldReturnEmptyStringOnUndefinedCellType()
$this->assertEquals([['ods--11', '', 'ods--13']], $allRows);
}

/**
* @return void
*/
public function testReadShouldReturnNullOnInvalidDateOrTime()
{
$allRows = $this->getAllRowsForFile('sheet_with_invalid_date_time.ods');
$this->assertEquals([[null, null]], $allRows);
}

/**
* @return void
*/
Expand Down
Binary file modified tests/resources/ods/sheet_with_all_cell_types.ods
Binary file not shown.
Binary file not shown.
Binary file modified tests/resources/ods/sheet_with_number_columns_repeated.ods
Binary file not shown.

0 comments on commit 6124528

Please sign in to comment.