From babd09e1b483de2be90be540ec9b57551bcb2165 Mon Sep 17 00:00:00 2001 From: Marcel Hauri Date: Mon, 7 Nov 2016 12:46:34 +0100 Subject: [PATCH] initial commit --- Model/Parser.php | 88 ++++++++++++++++++++++++++++++++++++ Model/Uploader.php | 66 +++++++++++++++++++++++++++ README.md | 47 +++++++++++++++++++ Test/Unit/Model/Parser.php | 7 +++ Test/Unit/Model/Uploader.php | 7 +++ composer.json | 27 +++++++++++ etc/module.xml | 3 ++ registration.php | 6 +++ 8 files changed, 251 insertions(+) create mode 100644 Model/Parser.php create mode 100644 Model/Uploader.php create mode 100644 README.md create mode 100644 Test/Unit/Model/Parser.php create mode 100644 Test/Unit/Model/Uploader.php create mode 100644 composer.json create mode 100644 etc/module.xml create mode 100644 registration.php diff --git a/Model/Parser.php b/Model/Parser.php new file mode 100644 index 0000000..69cb77d --- /dev/null +++ b/Model/Parser.php @@ -0,0 +1,88 @@ +spreadsheetParser = $spreadsheetParser; + } + + /** + * @param $file + * @param int $worksheetIndex + * @return array + */ + public function readFile($file, $worksheetIndex = 0) + { + $this->spreadsheet = $this->spreadsheetParser->open($file); + return $this->getWorksheetData($worksheetIndex); + } + + /** + * @return \Akeneo\Component\SpreadsheetParser\Csv\Spreadsheet|\Akeneo\Component\SpreadsheetParser\Xlsx\Spreadsheet + */ + public function getSpreadsheet() + { + return $this->spreadsheet; + } + + /** + * @return array|bool|\string[] + */ + public function getWorksheets() + { + if ($this->spreadsheet) { + return $this->spreadsheet->getWorksheets(); + } + return false; + } + + /** + * @param int $worksheetIndex + * @return array + */ + public function getWorksheetData($worksheetIndex = 0) + { + if (is_string($worksheetIndex)) { + $worksheetIndex = $this->spreadsheet->getWorksheetIndex($worksheetIndex); + } + $this->worksheet = $this->spreadsheet->createRowIterator($worksheetIndex); + + if (!$this->data) { + foreach ($this->worksheet as $index => $rows) { + $this->data[$index] = $rows; + } + } + return $this->data; + } +} diff --git a/Model/Uploader.php b/Model/Uploader.php new file mode 100644 index 0000000..c153637 --- /dev/null +++ b/Model/Uploader.php @@ -0,0 +1,66 @@ +uploaderFactory = $uploaderFactory; + $this->filesystem = $filesystem; + $this->uploadDir = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR); + } + + /** + * @param array $params + * @return string + * @throws \Exception + */ + public function upload($params = ['fileId' => 'file']) + { + /** @var $uploader \Magento\Framework\File\Uploader */ + $uploader = $this->uploaderFactory->create($params); + $saveDir = $this->uploadDir->getAbsolutePath(self::STORAGE_DIR); + $result = $uploader->save($saveDir); + $this->file = $result['path'] . $result['file']; + return $this->file; + } + + /** + * @return string + */ + public function getFile() + { + return $this->file; + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..6c77e80 --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +Magento 2 Spreadsheet Parser +============= + +Facts +----- +Parse XLSX, XLSM and CSV Files from Excel + +Requirements +------------ +- PHP >= 7.0.* +- Magento >= 2.1.* + +Compatibility +------------- +- Magento >= 2.1 + +Usage +----- +Load Uploader and Parser via DI, so they can be used in your method. + +```php +$file = $this->uploader->upload(['fileId' => \Magento\ImportExport\Model\Import::FIELD_NAME_SOURCE_FILE]); +$data = $this->parser->readFile($file); +foreach ($data as $index => $values) { + var_dump($index, $values); +} +``` + +Support +------- +If you have any issues with this extension, open an issue on [GitHub](https://github.com/staempfli/magento2-module-spreadsheet/issues). + +Contribution +------------ +Any contribution is highly appreciated. The best way to contribute code is to open a [pull request on GitHub](https://help.github.com/articles/using-pull-requests). + +Developer +--------- +Staempfli Webteam and all other [contributors](https://github.com/staempfli/magento2-module-spreadsheet/contributors) + +License +------- +[Open Software License ("OSL") v. 3.0](https://opensource.org/licenses/OSL-3.0) + +Copyright +--------- +(c) 2016, Stämpfli AG \ No newline at end of file diff --git a/Test/Unit/Model/Parser.php b/Test/Unit/Model/Parser.php new file mode 100644 index 0000000..cb66500 --- /dev/null +++ b/Test/Unit/Model/Parser.php @@ -0,0 +1,7 @@ + + + \ No newline at end of file diff --git a/registration.php b/registration.php new file mode 100644 index 0000000..a34e79b --- /dev/null +++ b/registration.php @@ -0,0 +1,6 @@ +