public function setSelection(ZipArchive $xlsx) { $sheets = $this->bookUtil->makeSheetMap($xlsx); foreach ($sheets as $sheetName) { $this->cellSetter->set($xlsx, $sheetName, $this->firstCell); } $firstSheet = reset($sheets); $this->sheetSetter->set($xlsx, $firstSheet); }
/** * @param ZipArchive $xlsx * @param ZipArchive|null $template */ public function setSelection(ZipArchive $xlsx, ZipArchive $template = null) { if (!$template instanceof ZipArchive) { throw new \RuntimeException('Invalid template'); } // テンプレートで選択されているシートを取得 $selectedSheet = $this->sheetReader->read($template); // テンプレートの各シートで選択されているセルを取得 $sheets = $this->bookUtil->makeSheetMap($template); $selectedCells = []; foreach ($sheets as $sheetId => $sheetName) { $selectedCells[$sheetName] = $this->cellReader->read($template, $sheetName); } // 選択シートを書き込む $this->sheetSetter->set($xlsx, $selectedSheet); // 各シートの選択セルを書き込む foreach ($selectedCells as $sheetName => $cellPosition) { $this->cellSetter->set($xlsx, $sheetName, $cellPosition); } }
/** * 選択されているシート名を返す * @inheritdoc */ public function read(ZipArchive $xlsx) { $worksheetXml = $xlsx->getFromName('xl/workbook.xml'); if (!$worksheetXml) { throw new \RuntimeException('Could not find workbook.xml'); } $sheets = $this->bookUtil->makeSheetMap($xlsx); $dom = new \DOMDocument(); $dom->loadXML($worksheetXml); $xpath = new \DOMXPath($dom); $xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL); // 選択されているシートIDを調べる $activeSheetId = null; $views = $xpath->query('//s:workbook/s:bookViews/s:workbookView'); if ($views->length == 1) { /** @var \DOMElement $workbookView */ $workbookView = $views->item(0); $activeSheetId = $workbookView->getAttribute('activeTab'); } return $activeSheetId && isset($sheets[$activeSheetId]) ? $sheets[$activeSheetId] : reset($sheets); }
private function setInWorkbook(ZipArchive $xlsx, $targetSheetName) { $workbookXml = $xlsx->getFromName('xl/workbook.xml'); if (!$workbookXml) { throw new \RuntimeException('Could not find workbook.xml'); } // 対象のシートindexを探す $sheets = $this->bookUtil->makeSheetMap($xlsx); $i = 0; $targetSheetIndex = false; foreach ($sheets as $sheetName) { if ($sheetName == $targetSheetName) { $targetSheetIndex = $i; break; } $i++; } if ($targetSheetIndex === false) { throw new \RuntimeException(sprintf('Could not find sheet"%s" in workbook', $targetSheetName)); } $dom = new \DOMDocument(); $dom->loadXML($workbookXml); $xpath = new \DOMXPath($dom); $xpath->registerNamespace('s', XmlNamespace::SPREADSHEETML_NS_URL); $views = $xpath->query('//s:workbook/s:bookViews/s:workbookView'); if ($views->length == 1) { /** @var \DOMElement $workbookView */ $workbookView = $views->item(0); $workbookView->setAttribute('activeTab', $targetSheetIndex); } else { $workbookView = $dom->createElement('workbookView'); $workbookView->setAttribute('activeTab', $targetSheetIndex); $bookView = $xpath->query('//s:workbook/s:bookViews')->item(0); $bookView->appendChild($workbookView); } $xlsx->addFromString('xl/workbook.xml', $dom->saveXML()); }