Skip to content

Commit

Permalink
ODS Writer support for 'date' and 'time' types
Browse files Browse the repository at this point in the history
Fixes issue box#751
  • Loading branch information
guss77 authored Jun 8, 2020
1 parent ab973ca commit b91eab2
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Spout/Writer/ODS/Manager/WorksheetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,22 @@ private function getCellXML(Cell $cell, $styleIndex, $numTimesValueRepeated)
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">';
$data .= '<text:p>' . $cell->getValue() . '</text:p>';
$data .= '</table:table-cell>';
} elseif ($cell->isDate()) {
$value = $cell->getValue();
if ($value instanceof \DateTime) {
$data .= ' office:value-type="date" calcext:value-type="date" office:date-value="' . $value->format(\DateTimeInterface::W3C) . '">';
$data .= '<text:p>' . $value->format(\DateTimeInterface::W3C) . '</text:p>';
} else if ($value instanceof \DateInterval) {
// workaround for missing DateInterval::format('c'), see https://stackoverflow.com/a/61088115/53538
static $f = ['M0S', 'H0M', 'DT0H', 'M0D', 'Y0M', 'P0Y', 'Y0M', 'P0M'];
static $r = ['M', 'H', 'DT', 'M', 'Y0M', 'P', 'Y', 'P'];
$value = rtrim(str_replace($f, $r, $value->format('P%yY%mM%dDT%hH%iM%sS')), 'PT') ?: $default;
$data .= ' office:value-type="time" office:time-value="' . $value . '">';
$data .= '<text:p>' . $value . '</text:p>';
} else {
throw new InvalidArgumentException('Trying to add a date value with an unsupported type: ' . \gettype($cell->getValue()));
}
$data .= '</table:table-cell>';
} elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) {
// only writes the error value if it's a string
$data .= ' office:value-type="string" calcext:value-type="error" office:value="">';
Expand Down

0 comments on commit b91eab2

Please sign in to comment.