-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert_excel_json.php
More file actions
115 lines (99 loc) · 2.73 KB
/
Copy pathconvert_excel_json.php
File metadata and controls
115 lines (99 loc) · 2.73 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
<?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 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);
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;
}
printf("ExcelStack convert 1.0 - by Bison Montana the Lord of Salmon\n\n");
$workbook = PHPExcel_IOFactory::load("eboks.mobile.resources.nodes.xlsx");
$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);
file_put_contents("lang_" . $name . ".json", $json);
}
//print_r($lang_json);