123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- /**
- * Utility class to access OpenDocument spreadsheets read-only
- *
- * Requires PHP 5.3
- *
- * @license CeCILL 2.0
- * @copyright 2005-2012 Ouest Systemes Informatiques
- */
- declare(strict_types=1);
- namespace Osinet\OpenDocument\Calc;
- /**
- * Utility class to access OpenDocument spreadsheets read-only
- */
- class Reader {
- const NS_TABLE = "urn:oasis:names:tc:opendocument:xmlns:table:1.0";
- const NS_OFFICE = "urn:oasis:names:tc:opendocument:xmlns:office:1.0";
- /**
- * @var \DOMDocument $xml
- */
- public $xml;
- /**
- * @var \DOMXPath $xpath
- */
- public $xpath;
- function __construct(string $path) {
- $this->xml = new \DOMDocument();
- $this->xml->preserveWhiteSpace = FALSE;
- $this->xml->load($path);
- $this->xpath = new \DOMXPath($this->xml);
- $this->xpath->registerNameSpace('office', self::NS_OFFICE);
- $this->xpath->registerNameSpace('table', self::NS_TABLE);
- }
- /**
- * Return a DOMNodeList of the worksheets available in the document
- *
- * @return \DOMNodeList<\DOMNode>
- */
- function getCalcSheets(): \DOMNodeList {
- $sq = '//table:table';
- $list = $this->xpath->query($sq);
- unset($sq);
- if ($list === FALSE) {
- $list = new \DOMNodeList();
- }
- return $list;
- }
- /**
- * Return the names of the worksheets available in the document
- *
- * @return array<string>
- */
- function getCalcSheetNames() {
- $dnlSheets = $this->getCalcSheets();
- $ret = array();
- foreach ($dnlSheets as $node) {
- $ret[] = $node->getAttributeNS(self::NS_TABLE, 'name');
- }
- unset($dnlSheets);
- return $ret;
- }
- /**
- * Return the rows in the spreadsheet as a DOMNodeList.
- *
- * WARNING: this means ALL the rows, in all the sheets, not just the first sheet
- *
- * @param string $namedRange
- * Optional name of range, notably Sheet name
- * @return \DOMNodeList<\DOMNode>
- */
- function getRows ($namedRange = NULL): \DOMNodeList {
- $query = empty($namedRange)
- ? '//table:table-row'
- : '//table:table[@table:name="' . $namedRange . '"]/table:table-row';
- $list = $this->xpath->query($query);
- unset($query);
- if ($list === FALSE) {
- $list = new \DOMNodeList();
- }
- return $list;
- }
- }
|