updated-packages

This commit is contained in:
RafficMohammed
2023-01-08 00:13:22 +05:30
parent 3ff7df7487
commit da241bacb6
12659 changed files with 563377 additions and 510538 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
/**
* @author Alexander Pervakov <frost-nzcr4@jagmort.com>
*/
class Comment
{
public static function write(XMLWriter $objWriter, Cell $cell): void
{
$comments = $cell->getWorksheet()->getComments();
if (!isset($comments[$cell->getCoordinate()])) {
return;
}
$comment = $comments[$cell->getCoordinate()];
$objWriter->startElement('office:annotation');
$objWriter->writeAttribute('svg:width', $comment->getWidth());
$objWriter->writeAttribute('svg:height', $comment->getHeight());
$objWriter->writeAttribute('svg:x', $comment->getMarginLeft());
$objWriter->writeAttribute('svg:y', $comment->getMarginTop());
$objWriter->writeElement('dc:creator', $comment->getAuthor());
$objWriter->writeElement('text:p', $comment->getText()->getPlainText());
$objWriter->endElement();
}
}

View File

@@ -0,0 +1,382 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Font;
use PhpOffice\PhpSpreadsheet\Worksheet\Row;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\Exception;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Ods\Cell\Comment;
/**
* @author Alexander Pervakov <frost-nzcr4@jagmort.com>
*/
class Content extends WriterPart
{
const NUMBER_COLS_REPEATED_MAX = 1024;
const NUMBER_ROWS_REPEATED_MAX = 1048576;
const CELL_STYLE_PREFIX = 'ce';
private $formulaConvertor;
/**
* Set parent Ods writer.
*/
public function __construct(Ods $writer)
{
parent::__construct($writer);
$this->formulaConvertor = new Formula($this->getParentWriter()->getSpreadsheet()->getDefinedNames());
}
/**
* Write content.xml to XML format.
*
* @return string XML Output
*/
public function write()
{
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8');
// Content
$objWriter->startElement('office:document-content');
$objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
$objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
$objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
$objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
$objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
$objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
$objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
$objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
$objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0');
$objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
$objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
$objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
$objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
$objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
$objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
$objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
$objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
$objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
$objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
$objWriter->writeAttribute('xmlns:xforms', 'http://www.w3.org/2002/xforms');
$objWriter->writeAttribute('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema');
$objWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
$objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
$objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
$objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
$objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
$objWriter->writeAttribute('xmlns:field', 'urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0');
$objWriter->writeAttribute('xmlns:formx', 'urn:openoffice:names:experimental:ooxml-odf-interop:xmlns:form:1.0');
$objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
$objWriter->writeAttribute('office:version', '1.2');
$objWriter->writeElement('office:scripts');
$objWriter->writeElement('office:font-face-decls');
// Styles XF
$objWriter->startElement('office:automatic-styles');
$this->writeXfStyles($objWriter, $this->getParentWriter()->getSpreadsheet());
$objWriter->endElement();
$objWriter->startElement('office:body');
$objWriter->startElement('office:spreadsheet');
$objWriter->writeElement('table:calculation-settings');
$this->writeSheets($objWriter);
// Defined names (ranges and formulae)
(new NamedExpressions($objWriter, $this->getParentWriter()->getSpreadsheet(), $this->formulaConvertor))->write();
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
return $objWriter->getData();
}
/**
* Write sheets.
*/
private function writeSheets(XMLWriter $objWriter): void
{
$spreadsheet = $this->getParentWriter()->getSpreadsheet(); /** @var Spreadsheet $spreadsheet */
$sheetCount = $spreadsheet->getSheetCount();
for ($i = 0; $i < $sheetCount; ++$i) {
$objWriter->startElement('table:table');
$objWriter->writeAttribute('table:name', $spreadsheet->getSheet($i)->getTitle());
$objWriter->writeElement('office:forms');
$objWriter->startElement('table:table-column');
$objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
$objWriter->endElement();
$this->writeRows($objWriter, $spreadsheet->getSheet($i));
$objWriter->endElement();
}
}
/**
* Write rows of the specified sheet.
*/
private function writeRows(XMLWriter $objWriter, Worksheet $sheet): void
{
$numberRowsRepeated = self::NUMBER_ROWS_REPEATED_MAX;
$span_row = 0;
$rows = $sheet->getRowIterator();
while ($rows->valid()) {
--$numberRowsRepeated;
$row = $rows->current();
if ($row->getCellIterator()->valid()) {
if ($span_row) {
$objWriter->startElement('table:table-row');
if ($span_row > 1) {
$objWriter->writeAttribute('table:number-rows-repeated', $span_row);
}
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', self::NUMBER_COLS_REPEATED_MAX);
$objWriter->endElement();
$objWriter->endElement();
$span_row = 0;
}
$objWriter->startElement('table:table-row');
$this->writeCells($objWriter, $row);
$objWriter->endElement();
} else {
++$span_row;
}
$rows->next();
}
}
/**
* Write cells of the specified row.
*/
private function writeCells(XMLWriter $objWriter, Row $row): void
{
$numberColsRepeated = self::NUMBER_COLS_REPEATED_MAX;
$prevColumn = -1;
$cells = $row->getCellIterator();
while ($cells->valid()) {
/** @var \PhpOffice\PhpSpreadsheet\Cell\Cell $cell */
$cell = $cells->current();
$column = Coordinate::columnIndexFromString($cell->getColumn()) - 1;
$this->writeCellSpan($objWriter, $column, $prevColumn);
$objWriter->startElement('table:table-cell');
$this->writeCellMerge($objWriter, $cell);
// Style XF
$style = $cell->getXfIndex();
if ($style !== null) {
$objWriter->writeAttribute('table:style-name', self::CELL_STYLE_PREFIX . $style);
}
switch ($cell->getDataType()) {
case DataType::TYPE_BOOL:
$objWriter->writeAttribute('office:value-type', 'boolean');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
break;
case DataType::TYPE_ERROR:
throw new Exception('Writing of error not implemented yet.');
break;
case DataType::TYPE_FORMULA:
$formulaValue = $cell->getValue();
if ($this->getParentWriter()->getPreCalculateFormulas()) {
try {
$formulaValue = $cell->getCalculatedValue();
} catch (Exception $e) {
// don't do anything
}
}
$objWriter->writeAttribute('table:formula', $this->formulaConvertor->convertFormula($cell->getValue()));
if (is_numeric($formulaValue)) {
$objWriter->writeAttribute('office:value-type', 'float');
} else {
$objWriter->writeAttribute('office:value-type', 'string');
}
$objWriter->writeAttribute('office:value', $formulaValue);
$objWriter->writeElement('text:p', $formulaValue);
break;
case DataType::TYPE_INLINE:
throw new Exception('Writing of inline not implemented yet.');
break;
case DataType::TYPE_NUMERIC:
$objWriter->writeAttribute('office:value-type', 'float');
$objWriter->writeAttribute('office:value', $cell->getValue());
$objWriter->writeElement('text:p', $cell->getValue());
break;
case DataType::TYPE_STRING:
$objWriter->writeAttribute('office:value-type', 'string');
$objWriter->writeElement('text:p', $cell->getValue());
break;
}
Comment::write($objWriter, $cell);
$objWriter->endElement();
$prevColumn = $column;
$cells->next();
}
$numberColsRepeated = $numberColsRepeated - $prevColumn - 1;
if ($numberColsRepeated > 0) {
if ($numberColsRepeated > 1) {
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', $numberColsRepeated);
$objWriter->endElement();
} else {
$objWriter->writeElement('table:table-cell');
}
}
}
/**
* Write span.
*
* @param int $curColumn
* @param int $prevColumn
*/
private function writeCellSpan(XMLWriter $objWriter, $curColumn, $prevColumn): void
{
$diff = $curColumn - $prevColumn - 1;
if (1 === $diff) {
$objWriter->writeElement('table:table-cell');
} elseif ($diff > 1) {
$objWriter->startElement('table:table-cell');
$objWriter->writeAttribute('table:number-columns-repeated', $diff);
$objWriter->endElement();
}
}
/**
* Write XF cell styles.
*/
private function writeXfStyles(XMLWriter $writer, Spreadsheet $spreadsheet): void
{
foreach ($spreadsheet->getCellXfCollection() as $style) {
$writer->startElement('style:style');
$writer->writeAttribute('style:name', self::CELL_STYLE_PREFIX . $style->getIndex());
$writer->writeAttribute('style:family', 'table-cell');
$writer->writeAttribute('style:parent-style-name', 'Default');
// style:text-properties
// Font
$writer->startElement('style:text-properties');
$font = $style->getFont();
if ($font->getBold()) {
$writer->writeAttribute('fo:font-weight', 'bold');
$writer->writeAttribute('style:font-weight-complex', 'bold');
$writer->writeAttribute('style:font-weight-asian', 'bold');
}
if ($font->getItalic()) {
$writer->writeAttribute('fo:font-style', 'italic');
}
if ($color = $font->getColor()) {
$writer->writeAttribute('fo:color', sprintf('#%s', $color->getRGB()));
}
if ($family = $font->getName()) {
$writer->writeAttribute('fo:font-family', $family);
}
if ($size = $font->getSize()) {
$writer->writeAttribute('fo:font-size', sprintf('%.1Fpt', $size));
}
if ($font->getUnderline() && $font->getUnderline() != Font::UNDERLINE_NONE) {
$writer->writeAttribute('style:text-underline-style', 'solid');
$writer->writeAttribute('style:text-underline-width', 'auto');
$writer->writeAttribute('style:text-underline-color', 'font-color');
switch ($font->getUnderline()) {
case Font::UNDERLINE_DOUBLE:
$writer->writeAttribute('style:text-underline-type', 'double');
break;
case Font::UNDERLINE_SINGLE:
$writer->writeAttribute('style:text-underline-type', 'single');
break;
}
}
$writer->endElement(); // Close style:text-properties
// style:table-cell-properties
$writer->startElement('style:table-cell-properties');
$writer->writeAttribute('style:rotation-align', 'none');
// Fill
if ($fill = $style->getFill()) {
switch ($fill->getFillType()) {
case Fill::FILL_SOLID:
$writer->writeAttribute('fo:background-color', sprintf(
'#%s',
strtolower($fill->getStartColor()->getRGB())
));
break;
case Fill::FILL_GRADIENT_LINEAR:
case Fill::FILL_GRADIENT_PATH:
/// TODO :: To be implemented
break;
case Fill::FILL_NONE:
default:
}
}
$writer->endElement(); // Close style:table-cell-properties
// End
$writer->endElement(); // Close style:style
}
}
/**
* Write attributes for merged cell.
*/
private function writeCellMerge(XMLWriter $objWriter, Cell $cell): void
{
if (!$cell->isMergeRangeValueCell()) {
return;
}
$mergeRange = Coordinate::splitRange($cell->getMergeRange());
[$startCell, $endCell] = $mergeRange[0];
$start = Coordinate::coordinateFromString($startCell);
$end = Coordinate::coordinateFromString($endCell);
$columnSpan = Coordinate::columnIndexFromString($end[0]) - Coordinate::columnIndexFromString($start[0]) + 1;
$rowSpan = $end[1] - $start[1] + 1;
$objWriter->writeAttribute('table:number-columns-spanned', $columnSpan);
$objWriter->writeAttribute('table:number-rows-spanned', $rowSpan);
}
}

View File

@@ -0,0 +1,119 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\DefinedName;
class Formula
{
private $definedNames = [];
/**
* @param DefinedName[] $definedNames
*/
public function __construct(array $definedNames)
{
foreach ($definedNames as $definedName) {
$this->definedNames[] = $definedName->getName();
}
}
public function convertFormula(string $formula, string $worksheetName = ''): string
{
$formula = $this->convertCellReferences($formula, $worksheetName);
$formula = $this->convertDefinedNames($formula);
if (substr($formula, 0, 1) !== '=') {
$formula = '=' . $formula;
}
return 'of:' . $formula;
}
private function convertDefinedNames(string $formula): string
{
$splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_DEFINEDNAME . '/mui',
$formula,
$splitRanges,
PREG_OFFSET_CAPTURE
);
$lengths = array_map('strlen', array_column($splitRanges[0], 0));
$offsets = array_column($splitRanges[0], 1);
$values = array_column($splitRanges[0], 0);
while ($splitCount > 0) {
--$splitCount;
$length = $lengths[$splitCount];
$offset = $offsets[$splitCount];
$value = $values[$splitCount];
if (in_array($value, $this->definedNames, true)) {
$formula = substr($formula, 0, $offset) . '$$' . $value . substr($formula, $offset + $length);
}
}
return $formula;
}
private function convertCellReferences(string $formula, string $worksheetName): string
{
$splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui',
$formula,
$splitRanges,
PREG_OFFSET_CAPTURE
);
$lengths = array_map('strlen', array_column($splitRanges[0], 0));
$offsets = array_column($splitRanges[0], 1);
$worksheets = $splitRanges[2];
$columns = $splitRanges[6];
$rows = $splitRanges[7];
// Replace any commas in the formula with semi-colons for Ods
// If by chance there are commas in worksheet names, then they will be "fixed" again in the loop
// because we've already extracted worksheet names with our preg_match_all()
$formula = str_replace(',', ';', $formula);
while ($splitCount > 0) {
--$splitCount;
$length = $lengths[$splitCount];
$offset = $offsets[$splitCount];
$worksheet = $worksheets[$splitCount][0];
$column = $columns[$splitCount][0];
$row = $rows[$splitCount][0];
$newRange = '';
if (empty($worksheet)) {
if (($offset === 0) || ($formula[$offset - 1] !== ':')) {
// We need a worksheet
$worksheet = $worksheetName;
}
} else {
$worksheet = str_replace("''", "'", trim($worksheet, "'"));
}
if (!empty($worksheet)) {
$newRange = "['" . str_replace("'", "''", $worksheet) . "'";
} elseif (substr($formula, $offset - 1, 1) !== ':') {
$newRange = '[';
}
$newRange .= '.';
if (!empty($column)) {
$newRange .= $column;
}
if (!empty($row)) {
$newRange .= $row;
}
// close the wrapping [] unless this is the first part of a range
$newRange .= substr($formula, $offset + $length, 1) !== ':' ? ']' : '';
$formula = substr($formula, 0, $offset) . $newRange . substr($formula, $offset + $length);
}
return $formula;
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Meta extends WriterPart
{
/**
* Write meta.xml to XML format.
*
* @param Spreadsheet $spreadsheet
*
* @return string XML Output
*/
public function write(?Spreadsheet $spreadsheet = null)
{
if (!$spreadsheet) {
$spreadsheet = $this->getParentWriter()->getSpreadsheet();
}
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8');
// Meta
$objWriter->startElement('office:document-meta');
$objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
$objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
$objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
$objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
$objWriter->writeAttribute('office:version', '1.2');
$objWriter->startElement('office:meta');
$objWriter->writeElement('meta:initial-creator', $spreadsheet->getProperties()->getCreator());
$objWriter->writeElement('dc:creator', $spreadsheet->getProperties()->getCreator());
$objWriter->writeElement('meta:creation-date', date(DATE_W3C, $spreadsheet->getProperties()->getCreated()));
$objWriter->writeElement('dc:date', date(DATE_W3C, $spreadsheet->getProperties()->getCreated()));
$objWriter->writeElement('dc:title', $spreadsheet->getProperties()->getTitle());
$objWriter->writeElement('dc:description', $spreadsheet->getProperties()->getDescription());
$objWriter->writeElement('dc:subject', $spreadsheet->getProperties()->getSubject());
$keywords = explode(' ', $spreadsheet->getProperties()->getKeywords());
foreach ($keywords as $keyword) {
$objWriter->writeElement('meta:keyword', $keyword);
}
//<meta:document-statistic meta:table-count="XXX" meta:cell-count="XXX" meta:object-count="XXX"/>
$objWriter->startElement('meta:user-defined');
$objWriter->writeAttribute('meta:name', 'Company');
$objWriter->writeRaw($spreadsheet->getProperties()->getCompany());
$objWriter->endElement();
$objWriter->startElement('meta:user-defined');
$objWriter->writeAttribute('meta:name', 'category');
$objWriter->writeRaw($spreadsheet->getProperties()->getCategory());
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
return $objWriter->getData();
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
class MetaInf extends WriterPart
{
/**
* Write META-INF/manifest.xml to XML format.
*
* @return string XML Output
*/
public function writeManifest()
{
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8');
// Manifest
$objWriter->startElement('manifest:manifest');
$objWriter->writeAttribute('xmlns:manifest', 'urn:oasis:names:tc:opendocument:xmlns:manifest:1.0');
$objWriter->writeAttribute('manifest:version', '1.2');
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', '/');
$objWriter->writeAttribute('manifest:version', '1.2');
$objWriter->writeAttribute('manifest:media-type', 'application/vnd.oasis.opendocument.spreadsheet');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'meta.xml');
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'settings.xml');
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'content.xml');
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'Thumbnails/thumbnail.png');
$objWriter->writeAttribute('manifest:media-type', 'image/png');
$objWriter->endElement();
$objWriter->startElement('manifest:file-entry');
$objWriter->writeAttribute('manifest:full-path', 'styles.xml');
$objWriter->writeAttribute('manifest:media-type', 'text/xml');
$objWriter->endElement();
$objWriter->endElement();
return $objWriter->getData();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Mimetype extends WriterPart
{
/**
* Write mimetype to plain text format.
*
* @param Spreadsheet $spreadsheet
*
* @return string XML Output
*/
public function write(?Spreadsheet $spreadsheet = null)
{
return 'application/vnd.oasis.opendocument.spreadsheet';
}
}

View File

@@ -0,0 +1,126 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\DefinedName;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class NamedExpressions
{
private $objWriter;
private $spreadsheet;
private $formulaConvertor;
public function __construct(XMLWriter $objWriter, Spreadsheet $spreadsheet, $formulaConvertor)
{
$this->objWriter = $objWriter;
$this->spreadsheet = $spreadsheet;
$this->formulaConvertor = $formulaConvertor;
}
public function write(): void
{
$this->objWriter->startElement('table:named-expressions');
$this->writeExpressions();
$this->objWriter->endElement();
}
private function writeExpressions(): void
{
$definedNames = $this->spreadsheet->getDefinedNames();
foreach ($definedNames as $definedName) {
if ($definedName->isFormula()) {
$this->objWriter->startElement('table:named-expression');
$this->writeNamedFormula($definedName, $this->spreadsheet->getActiveSheet());
} else {
$this->objWriter->startElement('table:named-range');
$this->writeNamedRange($definedName);
}
$this->objWriter->endElement();
}
}
private function writeNamedFormula(DefinedName $definedName, Worksheet $defaultWorksheet): void
{
$this->objWriter->writeAttribute('table:name', $definedName->getName());
$this->objWriter->writeAttribute(
'table:expression',
$this->formulaConvertor->convertFormula($definedName->getValue(), $definedName->getWorksheet()->getTitle())
);
$this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
$definedName,
"'" . (($definedName->getWorksheet() !== null) ? $definedName->getWorksheet()->getTitle() : $defaultWorksheet->getTitle()) . "'!\$A\$1"
));
}
private function writeNamedRange(DefinedName $definedName): void
{
$this->objWriter->writeAttribute('table:name', $definedName->getName());
$this->objWriter->writeAttribute('table:base-cell-address', $this->convertAddress(
$definedName,
"'" . $definedName->getWorksheet()->getTitle() . "'!\$A\$1"
));
$this->objWriter->writeAttribute('table:cell-range-address', $this->convertAddress($definedName, $definedName->getValue()));
}
private function convertAddress(DefinedName $definedName, string $address): string
{
$splitCount = preg_match_all(
'/' . Calculation::CALCULATION_REGEXP_CELLREF_RELATIVE . '/mui',
$address,
$splitRanges,
PREG_OFFSET_CAPTURE
);
$lengths = array_map('strlen', array_column($splitRanges[0], 0));
$offsets = array_column($splitRanges[0], 1);
$worksheets = $splitRanges[2];
$columns = $splitRanges[6];
$rows = $splitRanges[7];
while ($splitCount > 0) {
--$splitCount;
$length = $lengths[$splitCount];
$offset = $offsets[$splitCount];
$worksheet = $worksheets[$splitCount][0];
$column = $columns[$splitCount][0];
$row = $rows[$splitCount][0];
$newRange = '';
if (empty($worksheet)) {
if (($offset === 0) || ($address[$offset - 1] !== ':')) {
// We need a worksheet
$worksheet = $definedName->getWorksheet()->getTitle();
}
} else {
$worksheet = str_replace("''", "'", trim($worksheet, "'"));
}
if (!empty($worksheet)) {
$newRange = "'" . str_replace("'", "''", $worksheet) . "'.";
}
if (!empty($column)) {
$newRange .= $column;
}
if (!empty($row)) {
$newRange .= $row;
}
$address = substr($address, 0, $offset) . $newRange . substr($address, $offset + $length);
}
if (substr($address, 0, 1) === '=') {
$address = substr($address, 1);
}
return $address;
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Settings extends WriterPart
{
/**
* Write settings.xml to XML format.
*
* @param Spreadsheet $spreadsheet
*
* @return string XML Output
*/
public function write(?Spreadsheet $spreadsheet = null)
{
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8');
// Settings
$objWriter->startElement('office:document-settings');
$objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
$objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$objWriter->writeAttribute('xmlns:config', 'urn:oasis:names:tc:opendocument:xmlns:config:1.0');
$objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
$objWriter->writeAttribute('office:version', '1.2');
$objWriter->startElement('office:settings');
$objWriter->startElement('config:config-item-set');
$objWriter->writeAttribute('config:name', 'ooo:view-settings');
$objWriter->startElement('config:config-item-map-indexed');
$objWriter->writeAttribute('config:name', 'Views');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->startElement('config:config-item-set');
$objWriter->writeAttribute('config:name', 'ooo:configuration-settings');
$objWriter->endElement();
$objWriter->endElement();
$objWriter->endElement();
return $objWriter->getData();
}
}

View File

@@ -0,0 +1,68 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Shared\XMLWriter;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Styles extends WriterPart
{
/**
* Write styles.xml to XML format.
*
* @param Spreadsheet $spreadsheet
*
* @return string XML Output
*/
public function write(?Spreadsheet $spreadsheet = null)
{
$objWriter = null;
if ($this->getParentWriter()->getUseDiskCaching()) {
$objWriter = new XMLWriter(XMLWriter::STORAGE_DISK, $this->getParentWriter()->getDiskCachingDirectory());
} else {
$objWriter = new XMLWriter(XMLWriter::STORAGE_MEMORY);
}
// XML header
$objWriter->startDocument('1.0', 'UTF-8');
// Content
$objWriter->startElement('office:document-styles');
$objWriter->writeAttribute('xmlns:office', 'urn:oasis:names:tc:opendocument:xmlns:office:1.0');
$objWriter->writeAttribute('xmlns:style', 'urn:oasis:names:tc:opendocument:xmlns:style:1.0');
$objWriter->writeAttribute('xmlns:text', 'urn:oasis:names:tc:opendocument:xmlns:text:1.0');
$objWriter->writeAttribute('xmlns:table', 'urn:oasis:names:tc:opendocument:xmlns:table:1.0');
$objWriter->writeAttribute('xmlns:draw', 'urn:oasis:names:tc:opendocument:xmlns:drawing:1.0');
$objWriter->writeAttribute('xmlns:fo', 'urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0');
$objWriter->writeAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
$objWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$objWriter->writeAttribute('xmlns:meta', 'urn:oasis:names:tc:opendocument:xmlns:meta:1.0');
$objWriter->writeAttribute('xmlns:number', 'urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0');
$objWriter->writeAttribute('xmlns:presentation', 'urn:oasis:names:tc:opendocument:xmlns:presentation:1.0');
$objWriter->writeAttribute('xmlns:svg', 'urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0');
$objWriter->writeAttribute('xmlns:chart', 'urn:oasis:names:tc:opendocument:xmlns:chart:1.0');
$objWriter->writeAttribute('xmlns:dr3d', 'urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0');
$objWriter->writeAttribute('xmlns:math', 'http://www.w3.org/1998/Math/MathML');
$objWriter->writeAttribute('xmlns:form', 'urn:oasis:names:tc:opendocument:xmlns:form:1.0');
$objWriter->writeAttribute('xmlns:script', 'urn:oasis:names:tc:opendocument:xmlns:script:1.0');
$objWriter->writeAttribute('xmlns:ooo', 'http://openoffice.org/2004/office');
$objWriter->writeAttribute('xmlns:ooow', 'http://openoffice.org/2004/writer');
$objWriter->writeAttribute('xmlns:oooc', 'http://openoffice.org/2004/calc');
$objWriter->writeAttribute('xmlns:dom', 'http://www.w3.org/2001/xml-events');
$objWriter->writeAttribute('xmlns:rpt', 'http://openoffice.org/2005/report');
$objWriter->writeAttribute('xmlns:of', 'urn:oasis:names:tc:opendocument:xmlns:of:1.2');
$objWriter->writeAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
$objWriter->writeAttribute('xmlns:grddl', 'http://www.w3.org/2003/g/data-view#');
$objWriter->writeAttribute('xmlns:tableooo', 'http://openoffice.org/2009/table');
$objWriter->writeAttribute('xmlns:css3t', 'http://www.w3.org/TR/css3-text/');
$objWriter->writeAttribute('office:version', '1.2');
$objWriter->writeElement('office:font-face-decls');
$objWriter->writeElement('office:styles');
$objWriter->writeElement('office:automatic-styles');
$objWriter->writeElement('office:master-styles');
$objWriter->endElement();
return $objWriter->getData();
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Thumbnails extends WriterPart
{
/**
* Write Thumbnails/thumbnail.png to PNG format.
*
* @param Spreadsheet $spreadsheet
*
* @return string XML Output
*/
public function writeThumbnail(?Spreadsheet $spreadsheet = null)
{
return '';
}
}

View File

@@ -0,0 +1,33 @@
<?php
namespace PhpOffice\PhpSpreadsheet\Writer\Ods;
use PhpOffice\PhpSpreadsheet\Writer\Ods;
abstract class WriterPart
{
/**
* Parent Ods object.
*
* @var Ods
*/
private $parentWriter;
/**
* Get Ods writer.
*
* @return Ods
*/
public function getParentWriter()
{
return $this->parentWriter;
}
/**
* Set parent Ods writer.
*/
public function __construct(Ods $writer)
{
$this->parentWriter = $writer;
}
}