summaryrefslogtreecommitdiffstats
path: root/src/Util
diff options
context:
space:
mode:
Diffstat (limited to 'src/Util')
-rw-r--r--src/Util/Book.php146
-rw-r--r--src/Util/Sheet.php70
-rw-r--r--src/Util/XmlNamespace.php16
3 files changed, 232 insertions, 0 deletions
diff --git a/src/Util/Book.php b/src/Util/Book.php
new file mode 100644
index 0000000..ac8d557
--- /dev/null
+++ b/src/Util/Book.php
@@ -0,0 +1,146 @@
+<?php
+
+
+namespace PHPExcel\StyleFixer\Util;
+
+
+class Book
+{
+ /**
+ * 人間可読なシート名 => シートのリレーションファイルのパス の連想配列を作る
+ *
+ * @param \ZipArchive $zip
+ * @return array
+ */
+ public function makeSheetRelationMap(\ZipArchive $zip)
+ {
+ $sheetMap = $this->makeSheetMap($zip);
+
+ array_walk($sheetMap, function(&$xmlPath){
+ $xmlPath = str_replace('worksheets/', 'worksheets/_rels/', $xmlPath).'.rels';
+ });
+
+ return $sheetMap;
+ }
+
+ /**
+ * 人間可読なシート名 => シートファイルのパス の連想配列を作る
+ *
+ * @param \ZipArchive $zip
+ * @return array
+ */
+ public function makeSheetMap(\ZipArchive $zip)
+ {
+ $relXml = $zip->getFromName('xl/_rels/workbook.xml.rels');
+
+ $relDom = new \DOMDocument;
+ $relDom->loadXml($relXml);
+ $relXPath = new \DOMXPath($relDom);
+ $relXPath->registerNamespace('r', XmlNamespace::RELATIONSHIPS_NS_URL);
+
+ $sheetRelationIdMaps = $this->makeSheetRelationIdMap($zip);
+
+ $map = [];
+ foreach ($sheetRelationIdMaps as $sheetName => $relId) {
+ /** @var null|\DOMElement $relEntry */
+ $relEntry = $relXPath->query('//r:Relationships/r:Relationship[@Id="'.$relId.'"]')->item(0);
+ if ($relEntry) {
+ $map[$sheetName] = 'xl/'.$relEntry->getAttribute('Target');
+ }
+ }
+
+ return $map;
+ }
+
+ /**
+ * 人間可読なシート名 => relationship id の連想配列を作る
+ * @param \ZipArchive $zip
+ * @return array
+ */
+ private function makeSheetRelationIdMap(\ZipArchive $zip)
+ {
+ $xml = $zip->getFromName('xl/workbook.xml');
+
+ $dom = new \DOMDocument;
+ $dom->loadXML($xml);
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+
+ $map = [];
+ foreach ($xpath->query('//s:workbook/s:sheets/s:sheet') as $sheet) {
+ /** @var \DOMElement $sheet */
+ $map[$sheet->getAttribute('name')] = $sheet->getAttribute('r:id');
+ }
+
+ return $map;
+ }
+
+ /**
+ * 人間可読なシート名 => 印刷範囲 の連想配列を作る
+ *
+ * @param \ZipArchive $zip
+ * @return array
+ */
+ public function makePrintAreaMap(\ZipArchive $zip)
+ {
+ $xml = $zip->getFromName('xl/workbook.xml');
+
+ $dom = new \DOMDocument;
+ $dom->loadXML($xml);
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+
+ $sheets = $xpath->query('//s:workbook/s:sheets/s:sheet');
+ $printAreas = $xpath->query('//s:workbook/s:definedNames/s:definedName[@name="_xlnm.Print_Area"]');
+ $map = [];
+ $localSheetId = 0;
+ foreach ($sheets as $sheet) {
+ /** @var \DOMElement $definedName */
+ $definedName = $printAreas->item($localSheetId);
+ if ($definedName) {
+ /** @var \DOMElement $sheet */
+ $map[$sheet->getAttribute('name')] = $definedName->nodeValue;
+ }
+ $localSheetId++;
+ }
+
+ return $map;
+ }
+
+ /**
+ * 人間可読なシート名 => drawing.xmlファイルの配列 の連想配列を作る
+ *
+ * @param \ZipArchive $zip
+ * @return array
+ */
+ public function makeDrawingMap(\ZipArchive $zip)
+ {
+ $map = [];
+
+ $sheetRelationMap = $this->makeSheetRelationMap($zip);
+ foreach ($sheetRelationMap as $sheetName => $sheetRelFile) {
+ $drawings = [];
+
+ $relXml = $zip->getFromName($sheetRelFile);
+ $dom = new \DOMDocument();
+ $dom->loadXML($relXml);
+ $xpath = new \DOMXPath($dom);
+ $xpath->registerNamespace('r', XmlNamespace::RELATIONSHIPS_NS_URL);
+
+ $drawingFiles = $xpath->query('//r:Relationships/r:Relationship[@Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing"]');
+ foreach ($drawingFiles as $drawingFile) {
+ /** @var \DOMElement $drawingFile */
+ $drawings[] = str_replace('../', 'xl/', $drawingFile->getAttribute('Target'));
+ }
+
+ $xpath = null;
+ $dom = null;
+
+ if (0 !== count($drawings)) {
+ $map[$sheetName] = $drawings;
+ }
+ }
+
+ return $map;
+ }
+}
diff --git a/src/Util/Sheet.php b/src/Util/Sheet.php
new file mode 100644
index 0000000..6e11e1d
--- /dev/null
+++ b/src/Util/Sheet.php
@@ -0,0 +1,70 @@
+<?php
+
+
+namespace PHPExcel\StyleFixer\Util;
+
+
+class Sheet
+{
+
+ /**
+ * セル番地がExcel形式のセル範囲(コロン区切りのみ対応)に含まれるかどうか
+ *
+ * @param string $coordinate
+ * @param string $cellRange
+ * @return bool
+ */
+ public function inRange($coordinate, $cellRange)
+ {
+ // 絶対参照の$を削除
+ $cellRange = str_replace('$', '', $cellRange);
+ list($startCell, $endCell) = explode(':', $cellRange);
+
+ // 行の範囲チェック
+ $rowNumber = $this->detectRowNumberFromCoordinate($coordinate);
+ if ($rowNumber < $this->detectRowNumberFromCoordinate($startCell) || $rowNumber > $this->detectRowNumberFromCoordinate($endCell)) {
+ return false;
+ }
+
+ // 列の範囲チェック
+ $colNumber = $this->detectColNumberFromCoordinate($coordinate);
+ if ($colNumber < $this->detectColNumberFromCoordinate($startCell) || $colNumber > $this->detectColNumberFromCoordinate($endCell)) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * セル番地から行番号を取り出す
+ *
+ * @param string $coordinate
+ * @return int
+ */
+ private function detectRowNumberFromCoordinate($coordinate)
+ {
+ return intval(preg_replace('/[A-Z]+/', '', $coordinate));
+ }
+
+ /**
+ * セル番地から列番号を取り出す
+ *
+ * @param string $coordinate
+ * @return int
+ */
+ private function detectColNumberFromCoordinate($coordinate)
+ {
+ $char = preg_replace('/\d+/', '', $coordinate);
+
+ $charA = 65;
+
+ if (strlen($char) == 1) {
+ return ord($char) - $charA + 1;
+ }
+
+ $quot = ord($char[0]) - $charA + 1;
+ $mod = ord($char[1]) - $charA + 1;
+
+ return $quot * 26 + $mod;
+ }
+}
diff --git a/src/Util/XmlNamespace.php b/src/Util/XmlNamespace.php
new file mode 100644
index 0000000..581510e
--- /dev/null
+++ b/src/Util/XmlNamespace.php
@@ -0,0 +1,16 @@
+<?php
+
+
+namespace PHPExcel\StyleFixer\Util;
+
+
+class XmlNamespace
+{
+ const SPREADSHEETML_NS_URL = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main';
+ const RELATIONSHIPS_NS_URL = 'http://schemas.openxmlformats.org/package/2006/relationships';
+
+ // drawing関係
+ const SPREADSHEETDRAWING_NS_URL = 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheet';
+ const DRAWINGML_NS_URL = 'http://schemas.openxmlformats.org/drawingml/2006/main';
+ const DRAWINGML_CHART_NS_URL = 'http://schemas.openxmlformats.org/drawingml/2006/chart';
+}