Skip to content

Commit 5154b8c

Browse files
authored
Merge pull request #1 from degordian/master
Replaced PHPExcel with PhpSpreadsheet
2 parents 5fed83c + 4990293 commit 5154b8c

File tree

3 files changed

+48
-45
lines changed

3 files changed

+48
-45
lines changed

ExcelMessageController.php

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
<?php
2+
23
namespace codemix\excelmessage;
34

45
use Yii;
56
use yii\console\Exception;
67
use yii\console\Controller;
78
use yii\helpers\Console;
89
use yii\helpers\VarDumper;
9-
use \PHPExcel;
10-
use \PHPExcel_Worksheet;
11-
use \PHPExcel_IOFactory;
12-
use \PHPExcel_Cell_DataType;
10+
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
11+
use \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
12+
use \PhpOffice\PhpSpreadsheet\IOFactory as PhpspreadsheetIOFactory;
13+
use \PhpOffice\PhpSpreadsheet\Cell\DataType as CellDataType;
1314

1415
/**
1516
* Export new translations to Excel files from PHP message files and update PHP
@@ -60,7 +61,7 @@ class ExcelMessageController extends Controller
6061
public function options($actionID)
6162
{
6263
$options = ['color', 'languages', 'categories', 'ignoreLanguages', 'ignoreCategories'];
63-
if ($actionID==='export') {
64+
if ($actionID === 'export') {
6465
$options[] = 'lineHeight';
6566
}
6667
return $options;
@@ -101,8 +102,10 @@ public function actionExport($configFile, $excelDir, $type = 'new')
101102
}
102103
$this->stdout("Reading $file ... ", Console::FG_GREEN);
103104
$existing = require($file);
104-
if ($type==='new') {
105-
$existing = array_filter($existing, function ($v) { return $v===''; });
105+
if ($type === 'new') {
106+
$existing = array_filter($existing, function ($v) {
107+
return $v === '';
108+
});
106109
}
107110
foreach ($existing as $source => $translation) {
108111
if (!isset($messages[$language])) {
@@ -116,7 +119,7 @@ public function actionExport($configFile, $excelDir, $type = 'new')
116119
$this->stdout("Done.\n", Console::FG_GREEN);
117120
}
118121
}
119-
if (count($messages)!==0) {
122+
if (count($messages) !== 0) {
120123
$this->writeToExcelFiles($messages, $excelDir);
121124
} else {
122125
$this->stdout("No new translations found\n", Console::FG_GREEN);
@@ -145,23 +148,23 @@ public function actionImport($configFile, $excelDir, $extension = 'xlsx')
145148
{
146149
$config = $this->checkArgs($configFile, $excelDir);
147150
$messages = [];
148-
foreach (glob(rtrim($excelDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.'*.'.$extension) as $file) {
151+
foreach (glob(rtrim($excelDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '*.' . $extension) as $file) {
149152
$language = pathinfo($file, PATHINFO_FILENAME);
150153
if (!$this->languageIncluded($language)) {
151154
$this->stdout("Skipping language $language.\n", Console::FG_YELLOW);
152155
continue;
153156
}
154-
$excel = PHPExcel_IOFactory::load($file);
157+
$excel = PhpspreadsheetIOFactory::load($file);
155158
foreach ($excel->getSheetNames() as $category) {
156159
if (!$this->categoryIncluded($category)) {
157160
$this->stdout("Skipping category $category.\n", Console::FG_YELLOW);
158161
continue;
159162
}
160163
$sheet = $excel->getSheetByName($category);
161164
$row = 2;
162-
while (($source = $sheet->getCellByColumnAndRow(0,$row)->getValue())!==null) {
165+
while (($source = $sheet->getCellByColumnAndRow(0, $row)->getValue()) !== null) {
163166
$translation = (string)$sheet->getCellByColumnAndRow(1, $row)->getValue();
164-
if (trim($translation)!=='') {
167+
if (trim($translation) !== '') {
165168
if (!isset($messages[$language])) {
166169
$messages[$language] = [];
167170
}
@@ -200,7 +203,7 @@ protected function checkArgs($configFile, $excelDir)
200203
'format' => 'php',
201204
], require($configFile));
202205

203-
if (empty($config['format']) || $config['format']!=='php') {
206+
if (empty($config['format']) || $config['format'] !== 'php') {
204207
throw new Exception('Format must be "php".');
205208
}
206209
if (!isset($config['messagePath'])) {
@@ -223,39 +226,39 @@ protected function checkArgs($configFile, $excelDir)
223226
protected function writeToExcelFiles($messages, $excelDir)
224227
{
225228
foreach ($messages as $language => $categories) {
226-
$file = rtrim($excelDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.$language.'.xlsx';
229+
$file = rtrim($excelDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $language . '.xlsx';
227230
$this->stdout("Writing Excel file for $language to $file ... ", Console::FG_GREEN);
228-
$excel = new PHPExcel();
231+
$excel = new Spreadsheet();
229232
$index = 0;
230233
foreach ($categories as $category => $sources) {
231-
$sheet = new PHPExcel_Worksheet($excel, $category);
234+
$sheet = new Worksheet($excel, $category);
232235
$excel->addSheet($sheet, $index++);
233236
$sheet->getColumnDimension('A')->setWidth(60);
234237
$sheet->getColumnDimension('B')->setWidth(60);
235-
$sheet->setCellValue('A1', 'Source', PHPExcel_Cell_DataType::TYPE_STRING);
236-
$sheet->setCellValue('B1', 'Translation', PHPExcel_Cell_DataType::TYPE_STRING);
238+
$sheet->setCellValue('A1', 'Source', CellDataType::TYPE_STRING);
239+
$sheet->setCellValue('B1', 'Translation', CellDataType::TYPE_STRING);
237240
$sheet->getStyle('A1:B1')->applyFromArray([
238241
'font' => [
239242
'bold' => true,
240243
]
241244
]);
242245
$row = 2;
243246
foreach ($sources as $message => $translation) {
244-
$sheet->setCellValue('A'.$row, $message);
245-
$sheet->getStyle('A'.$row)->getAlignment()->setWrapText(true);
246-
if ($translation!=='') {
247-
$sheet->setCellValue('B'.$row, $translation);
248-
$sheet->getStyle('B'.$row)->getAlignment()->setWrapText(true);
247+
$sheet->setCellValue('A' . $row, $message);
248+
$sheet->getStyle('A' . $row)->getAlignment()->setWrapText(true);
249+
if ($translation !== '') {
250+
$sheet->setCellValue('B' . $row, $translation);
251+
$sheet->getStyle('B' . $row)->getAlignment()->setWrapText(true);
249252
}
250253
// This does not work with LibreOffice Calc, see:
251254
// https://github.com/PHPOffice/PHPExcel/issues/588
252-
$sheet->getRowDimension($row)->setRowHeight($this->lineHeight===null ? -1 : $this->lineHeight);
255+
$sheet->getRowDimension($row)->setRowHeight($this->lineHeight === null ? -1 : $this->lineHeight);
253256
$row++;
254257
}
255258
}
256259
$excel->removeSheetByIndex($index);
257260
$excel->setActiveSheetIndex(0);
258-
$writer = PHPExcel_IOFactory::createWriter($excel, "Excel2007");
261+
$writer = PhpspreadsheetIOFactory::createWriter($excel, "Xlsx");
259262
$writer->save($file);
260263
$this->stdout("Done.\n", Console::FG_GREEN);
261264
}
@@ -273,7 +276,7 @@ protected function updateMessageFiles($messages, $config)
273276
$this->stdout("Updating translations for $language\n", Console::FG_GREEN);
274277
$dir = $config['messagePath'] . DIRECTORY_SEPARATOR . $language;
275278
foreach ($categories as $category => $translations) {
276-
$file = $dir.DIRECTORY_SEPARATOR.$category.'.php';
279+
$file = $dir . DIRECTORY_SEPARATOR . $category . '.php';
277280
if (!file_exists($file)) {
278281
$this->stdout("Category '$category' not found for language '$language' ($file) - Skipping", Console::FG_RED);
279282
}
@@ -282,16 +285,18 @@ protected function updateMessageFiles($messages, $config)
282285
foreach ($translations as $message => $translation) {
283286
if (!array_key_exists($message, $existingMessages)) {
284287
$this->stdout('Skipping (removed): ', Console::FG_YELLOW);
285-
$this->stdout($message."\n");
286-
} elseif ($existingMessages[$message]!=='') {
288+
$this->stdout($message . "\n");
289+
} elseif ($existingMessages[$message] !== '') {
287290
$this->stdout('Skipping (exists): ', Console::FG_YELLOW);
288-
$this->stdout($message."\n");
291+
$this->stdout($message . "\n");
289292
} else {
290293
$existingMessages[$message] = $translation;
291294
}
292295
}
293296
ksort($existingMessages);
294-
$emptyMessages = array_filter($existingMessages, function ($v) { return $v===''; });
297+
$emptyMessages = array_filter($existingMessages, function ($v) {
298+
return $v === '';
299+
});
295300
$translatedMessages = array_filter($existingMessages, 'strlen');
296301
$array = VarDumper::export($emptyMessages + $translatedMessages);
297302

@@ -330,8 +335,8 @@ protected function updateMessageFiles($messages, $config)
330335
*/
331336
protected function languageIncluded($language)
332337
{
333-
if ($this->languages===null) {
334-
return $this->ignoreLanguages===null ? true : !in_array($language, explode(',', $this->ignoreLanguages));
338+
if ($this->languages === null) {
339+
return $this->ignoreLanguages === null ? true : !in_array($language, explode(',', $this->ignoreLanguages));
335340
} else {
336341
return in_array($language, explode(',', $this->languages));
337342
}
@@ -343,8 +348,8 @@ protected function languageIncluded($language)
343348
*/
344349
protected function categoryIncluded($category)
345350
{
346-
if ($this->categories===null) {
347-
return $this->ignoreCategories===null ? true : !in_array($category, explode(',', $this->ignoreCategories));
351+
if ($this->categories === null) {
352+
return $this->ignoreCategories === null ? true : !in_array($category, explode(',', $this->ignoreCategories));
348353
} else {
349354
return in_array($category, explode(',', $this->categories));
350355
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ With this extension you can export messages from PHP message files to Excel,
1616
send them to your translators and read them back into your message files.
1717

1818
> **Note:** To read and write to and from Excel file, we use the excellent
19-
> [PHPExcel](https://github.com/PHPOffice/PHPExcel) package.
19+
> [PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) package.
2020
2121

2222
## Installation
@@ -75,7 +75,7 @@ This will add the new translations to your PHP message files. Yes it's
7575
really that simple.
7676

7777
You can also pass a third parameter with the file extension, the default is `xlsx`
78-
as used by Excel2007 files. PHPExcel should also autodetect other Excel formats
78+
as used by Excel 2007+ files. \PhpOffice\PhpSpreadsheet\Spreadsheet should also autodetect other Excel formats
7979

8080
> **Note:** The files must be provided in the same format as they where created by
8181
> the export:

composer.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
"keywords": ["yii2", "i18n", "excel"],
55
"type": "yii2-extension",
66
"license": "MIT",
7-
"authors": [
8-
{
9-
"name": "Michael Härtl",
10-
"email": "[email protected]"
11-
}
12-
],
7+
"authors": [{
8+
"name": "Michael Härtl",
9+
"email": "[email protected]"
10+
}],
1311
"require": {
14-
"yiisoft/yii2": "*",
15-
"phpoffice/phpexcel": "1.*"
12+
"yiisoft/yii2": "*",
13+
"phpoffice/phpspreadsheet": "^1.2"
1614
},
1715
"autoload": {
1816
"psr-4": {
1917
"codemix\\excelmessage\\": ""
2018
}
2119
}
22-
}
20+
}

0 commit comments

Comments
 (0)