public function test()
 {
     $service = new ExcelSelectionSetter();
     $service->addStrategy(new AllFirst());
     $service->addStrategy(new SynchronizeToTemplate());
     $originalFile = __DIR__ . '/data/functional/target.xlsx';
     $templateFile = new ZipArchive();
     $templateFile->open(__DIR__ . '/data/functional/template.xlsx');
     // allfirst
     $allFirstTarget = __DIR__ . '/data/functional/all_first.xlsx';
     copy($originalFile, $allFirstTarget);
     $service->execute($allFirstTarget, 'all_first');
     // synchro
     $syncTarget = __DIR__ . '/data/functional/sync.xlsx';
     copy($originalFile, $syncTarget);
     $service->execute($syncTarget, 'synchronize_to_template', $templateFile);
     $this->assertTrue(true);
 }
 /**
  * 選択されているシート名を返す
  * @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());
 }