-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.php
More file actions
203 lines (179 loc) · 5.14 KB
/
Copy pathconvert.php
File metadata and controls
203 lines (179 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/Paris');
require_once("Classes/PHPExcel.php");
/** Include PHPExcel_IOFactory */
require_once dirname(__FILE__) . '/Classes/PHPExcel/IOFactory.php';
function endswith($string, $test) {
$strlen = strlen($string);
$testlen = strlen($test);
if ($testlen > $strlen) return false;
return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}
function buildLanguageArray(&$sheet)
{
$lang = array();
$x = 1;
$y = 1;
while(true)
{
$value = $sheet->getCellByColumnAndRow($x, $y)->getValue();
if($value == NULL)
break;
//var_dump($value);
$dataType = PHPExcel_Cell_DataType::dataTypeForValue($value);
//var_dump($dataType);
$lang[$value] = array('x' => $x);
$x++;
}
return $lang;
}
function getCellInfo(&$sheet, $x, $y)
{
$style = $sheet->getStyleByColumnAndRow($x, $y);
$cell_info = new stdClass();
$cell_info->value = $sheet->getCellByColumnAndRow($x, $y)->getValue();
$cell_info->bold = $style->getFont()->getBold();
$cell_info->color = $style->getFont()->getColor()->getRGB();
$cell_info->dataType = PHPExcel_Cell_DataType::dataTypeForValue($cell_info->value);
if($cell_info->dataType == "inlineStr")
{
$cell_info->value = $cell_info->value->getPlainText();
//var_dump($cell_info->value);
}
return $cell_info;
}
function buildSectionKeysArray(&$sheet, &$languages)
{
$keys = array();
$x = 0;
$y = 1;
$count = 0;
$section_name = NULL;
while(true)
{
$cell_info = getCellInfo($sheet, $x, $y);
if($cell_info->value == NULL && $sheet->getCellByColumnAndRow($x, $y+1)->getValue() == NULL)
{
//printf("Stopped scanning at two subsequent blank cells\n");
break;
}
if($cell_info->value != NULL)
{
if($cell_info->bold)
{
//printf("Found new section %s\n", $cell_info->value);
$section_name = $cell_info->value;
}
else if($section_name != NULL)
{
//printf("\tKey: %s\n", $cell_info->value);
$keys[$section_name][] = array('key' => $cell_info->value, 'row' => $y);
}
//var_dump($cell_info);
$count++;
}
$y++;
}
printf("Scanned %d rows\n", $count);
return $keys;
}
function printUsage()
{
printf("Usage:\n\tconvert <filename> (JSON or XLSX file)\n\nif given a JSON file it converts to XLSX (Excel2007) and the other way around\n\n");
}
function convertJSONToExcel($src_filename, $dst_filename)
{
$workbook = new PHPExcel();
$sheet = $workbook->getSheet();
$jsondata = json_decode(file_get_contents($src_filename), true);
//print_r($jsondata);
$x = 1;
foreach($jsondata as $locale => $lang)
{
printf("Converting language %s...\n", $locale);
$y = 1;
$sheet->getCellByColumnAndRow($x, $y)->setValue($locale);
foreach($lang as $section_name => $section)
{
$y++;
$sheet->getCellByColumnAndRow(0, $y)->setValue($section_name);
$sheet->getStyleByColumnAndRow(0, $y)->getFont()->setBold(true);
$y++;
foreach($section as $key => $value)
{
$y++;
$sheet->getCellByColumnAndRow($x, $y)->setValue($value);
//if(getCellInfo($sheet, 1, $y)->value == NULL)
$sheet->getCellByColumnAndRow(0, $y)->setValue($key);
}
$y++;
}
$sheet->getColumnDimension($sheet->getCellByColumnAndRow($x, 1)->getColumn())->setAutoSize(true);
$x++;
}
$sheet->freezePaneByColumnAndRow(count($jsondata)+1, 2);
$objWriter = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$objWriter->save($dst_filename);
}
function convertExcelToJSON($src_filename, $dst_filename)
{
$workbook = PHPExcel_IOFactory::load($src_filename);
$sheet = $workbook->getSheet();
printf("Sheet name: %s\n", $sheet->getTitle());
$languages = buildLanguageArray($sheet);
//print_r($languages);
$keys = buildSectionKeysArray($sheet, $languages);
$lang_json = array();
foreach ($languages as $name => $lang)
{
printf("Processing %s...\n", $name);
$lang_json[$name] = array();
foreach($keys as $section_name => $section)
{
$lang_json[$name][$section_name] = array();
foreach($section as $val)
{
$ci = getCellInfo($sheet, $lang['x'], $val['row']);
$lang_json[$name][$section_name][$val['key']] = $ci->value;
}
}
}
foreach ($lang_json as $name => $lang)
{
$json = json_encode($lang, JSON_PRETTY_PRINT | JSON_FORCE_OBJECT);
$fname = $dst_filename . "_" . $name . ".json";
printf("Writing language file %s...\n", $fname);
file_put_contents($fname, $json);
}
}
printf("ExcelStack convert 1.0 - by Bison Montana the Lord of Salmon\n\n");
if($argc != 2)
{
printUsage();
exit(0);
}
$filename = $argv[1];
if(!file_exists($filename))
{
printf("%s file does not exist\n", $filename);
exit(1);
}
if(endswith(strtolower($filename), ".json"))
{
$dst_filename = substr_replace($filename, "xlsx", -4);
printf("Converting JSON file $filename to excel format $dst_filename\n");
convertJSONToExcel($filename, $dst_filename);
}
else if(endswith(strtolower($filename), ".xlsx"))
{
printf("Converting Excel file $filename to json format\n");
$dst_filename = substr_replace($filename, "", -5);
convertExcelToJSON($filename, $dst_filename);
}
else
{
printf("File format not recognized (suffix must be either .json or .xlsx)\n");
}