summaryrefslogtreecommitdiffstats
path: root/src/Plugin
diff options
context:
space:
mode:
Diffstat (limited to 'src/Plugin')
-rw-r--r--src/Plugin/CellStyleFixer.php120
-rw-r--r--src/Plugin/ConditionalFormatFixer.php77
-rw-r--r--src/Plugin/Plugin.php10
3 files changed, 207 insertions, 0 deletions
diff --git a/src/Plugin/CellStyleFixer.php b/src/Plugin/CellStyleFixer.php
new file mode 100644
index 0000000..cbbb4af
--- /dev/null
+++ b/src/Plugin/CellStyleFixer.php
@@ -0,0 +1,120 @@
+<?php
+
+
+namespace PHPExcel\StyleFixer\Plugin;
+
+
+use PHPExcel\StyleFixer\Util\Book as BookUtil;
+use PHPExcel\StyleFixer\Util\Sheet as SheetUtil;
+use PHPExcel\StyleFixer\Util\XmlNamespace;
+
+class CellStyleFixer implements Plugin
+{
+ /**
+ * @var \PHPExcel\StyleFixer\Util\Book
+ */
+ private $bookUtil;
+
+ /**
+ * @var \PHPExcel\StyleFixer\Util\Sheet
+ */
+ private $sheetUtil;
+
+ /**
+ * @param BookUtil $bookUtil
+ * @param SheetUtil $sheetUtil
+ */
+ public function __construct(BookUtil $bookUtil, SheetUtil $sheetUtil)
+ {
+ $this->bookUtil = $bookUtil;
+ $this->sheetUtil = $sheetUtil;
+ }
+
+ /**
+ * 各ワークシート内の各セルに付属している書式設定を修復する
+ *
+ * @param \ZipArchive $output
+ * @param \ZipArchive $template
+ */
+ public function execute(\ZipArchive $output, \ZipArchive $template)
+ {
+ // テンプレート側のシート名のマッピング
+ $srcSheetMap = $this->bookUtil->makeSheetMap($template);
+ // 出力ファイル側のシート名のマッピング
+ $distSheetMap = $this->bookUtil->makeSheetMap($output);
+ // 出力ファイル側のシートの印刷範囲マッピング
+ $distPrintAreas = $this->bookUtil->makePrintAreaMap($output);
+
+ foreach ($distSheetMap as $sheetName => $distSheetPath) {
+ $distXml = $output->getFromName($distSheetPath);
+ $distDom = new \DOMDocument;
+ $distDom->loadXML($distXml);
+ $distXPath = new \DOMXPath($distDom);
+ $distXPath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+
+ $srcXml = $template->getFromName($srcSheetMap[$sheetName]);
+ $srcDom = new \DOMDocument;
+ $srcDom->loadXML($srcXml);
+ $srcXPath = new \DOMXPath($srcDom);
+ $srcXPath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+
+ $printArea = null;
+ if (isset($distPrintAreas[$sheetName])) {
+ list(,$printArea) = explode('!', $distPrintAreas[$sheetName]);
+ }
+
+ // セル番地 => スタイル番号
+ $styleMap = [];
+ foreach ($srcXPath->query('//s:worksheet/s:sheetData/s:row') as $srcRow) {
+ /** @var \DOMElement $srcRow */
+ if ($srcRow->hasAttribute('s')) {
+ /** @var \DOMElement $distRow */
+ $distRow = $distXPath->query('//s:worksheet/s:sheetData/s:row[@r="'.$srcRow->getAttribute('r').'"]')->item(0);
+ if ($distRow) {
+ $distRow->setAttribute('s', $srcRow->getAttribute('s'));
+ }
+ }
+
+ if ($srcRow->hasChildNodes()) {
+ foreach ($srcRow->childNodes as $srcCell) {
+ if ($srcCell instanceOf \DOMElement && $srcCell->tagName == 'c' && $srcCell->hasAttribute('s')) {
+ $styleMap[$srcCell->getAttribute('r')] = $srcCell->getAttribute('s');
+ }
+ }
+ }
+ }
+
+ foreach ($distXPath->query('//s:worksheet/s:sheetData/s:row/s:c') as $distCell) {
+ /** @var \DOMElement $distCell */
+ $coordinate = $distCell->getAttribute('r');
+ if (isset($styleMap[$coordinate]) && ($this->cellHasValue($distCell) || !$printArea || $this->sheetUtil->inRange($coordinate, $printArea))) {
+ $distCell->setAttribute('s', $styleMap[$coordinate]);
+ } else {
+ $distCell->removeAttribute('s');
+ }
+ }
+
+ $output->addFromString($distSheetPath, $distDom->saveXML());
+ }
+ }
+
+ /**
+ * @param \DOMElement $cell
+ * @return bool
+ */
+ private function cellHasValue(\DOMElement $cell)
+ {
+ $hasValue = false;
+
+ if ($cell->hasChildNodes()) {
+ foreach ($cell->childNodes as $child) {
+ if ($child instanceOf \DOMElement && $child->tagName == 'v') {
+ $hasValue = true;
+ break;
+ }
+ }
+ }
+
+ return $hasValue;
+ }
+}
diff --git a/src/Plugin/ConditionalFormatFixer.php b/src/Plugin/ConditionalFormatFixer.php
new file mode 100644
index 0000000..2c6638b
--- /dev/null
+++ b/src/Plugin/ConditionalFormatFixer.php
@@ -0,0 +1,77 @@
+<?php
+
+namespace PHPExcel\StyleFixer\Plugin;
+
+use PHPExcel\StyleFixer\Util\Book as BookUtil;
+use PHPExcel\StyleFixer\Util\XmlNamespace;
+
+/**
+ * Class ConditionalFormatFixer
+ * PHPExcelが壊した条件付き書式を修復する
+ */
+class ConditionalFormatFixer implements Plugin
+{
+ /**
+ * @var \PHPExcel\StyleFixer\Util\Book
+ */
+ private $bookUtil;
+
+ public function __construct(BookUtil $bookUtil)
+ {
+ $this->bookUtil = $bookUtil;
+ }
+
+ /**
+ * 各ワークシート内の条件付き書式設定を修復する
+ *
+ * @param \ZipArchive $output
+ * @param \ZipArchive $template
+ */
+ public function execute(\ZipArchive $output, \ZipArchive $template)
+ {
+ // テンプレート側のシート名のマッピング
+ $srcSheetMap = $this->bookUtil->makeSheetMap($template);
+ // 出力ファイル側のシート名のマッピング
+ $distSheetMap = $this->bookUtil->makeSheetMap($output);
+
+ foreach ($distSheetMap as $sheetName => $distSheetPath) {
+ $distXml = $output->getFromName($distSheetPath);
+ if (false !== strpos($distXml, 'conditionalFormatting')) {
+ $distDom = new \DOMDocument;
+ $distDom->loadXML($distXml);
+ $distXPath = new \DOMXPath($distDom);
+ $distXPath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+ $distRoot = $distXPath->query('//s:worksheet')->item(0);
+ $distConditionalFormattings = $distXPath->query('//s:worksheet/s:conditionalFormatting');
+ $elementAfterConditionalFormatting = null;
+ foreach ($distConditionalFormattings as $distConditionalFormatting) {
+ $elementAfterConditionalFormatting = $distConditionalFormatting->nextSibling;
+ $distRoot->removeChild($distConditionalFormatting);
+ }
+
+ while ($elementAfterConditionalFormatting instanceOf \DOMNode && !$elementAfterConditionalFormatting instanceOf \DOMElement) {
+ $elementAfterConditionalFormatting = $elementAfterConditionalFormatting->nextSibling;
+ }
+ if (!$elementAfterConditionalFormatting instanceOf \DOMElement) {
+ break;
+ }
+
+ $srcSheetPath = $srcSheetMap[$sheetName];
+ $srcXml = $template->getFromName($srcSheetPath);
+ $srcDom = new \DOMDocument;
+ $srcDom->loadXML($srcXml);
+ $srcXPath = new \DOMXPath($srcDom);
+ $srcXPath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL);
+
+ $conditionalFormattings = $srcXPath->query('//s:worksheet/s:conditionalFormatting');
+ foreach ($conditionalFormattings as $conditionalFormatting) {
+ /** @var \DOMElement $conditionalFormatting */
+ $newDistConditionalFormatting = $distDom->importNode($conditionalFormatting, true);
+ $distRoot->insertBefore($newDistConditionalFormatting, $elementAfterConditionalFormatting);
+ }
+
+ $output->addFromString($distSheetPath, $distDom->saveXML());
+ }
+ }
+ }
+}
diff --git a/src/Plugin/Plugin.php b/src/Plugin/Plugin.php
new file mode 100644
index 0000000..8229040
--- /dev/null
+++ b/src/Plugin/Plugin.php
@@ -0,0 +1,10 @@
+<?php
+
+
+namespace PHPExcel\StyleFixer\Plugin;
+
+
+interface Plugin
+{
+ public function execute(\ZipArchive $output, \ZipArchive $template);
+}