-
Notifications
You must be signed in to change notification settings - Fork 0
/
old_convjson.pl
82 lines (61 loc) · 1.76 KB
/
old_convjson.pl
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
#!/usr/bin/perl
# convert language JSON files into one workbook
# very rigid, needs args...
use strict;
use warnings;
use v5.16;
use JSON qw (decode_json);
use Data::Dumper;
use utf8;
# cpan mods
use Excel::Writer::XLSX;
# globals
my $workbook;
# args - JSON file name without extension
sub read_json_file {
my $jsonf = shift;
my $jsonf_txt;
my $fh;
open $fh, '<', "$jsonf.json" or die "Cannot read '$jsonf': $!\n";
$jsonf_txt .= $_ while <$fh>;
my $json = decode_json($jsonf_txt);
#say Dumper($json);
my %json_hsh = %{$json};
close $fh;
#say "KEYS:" . keys (%json_hsh);
#say Dumper(%json_hsh);
return \%json_hsh;
}
# args - worksheet name, hashref of json file
# works on global object
sub write_worksheet {
my $ws_name = shift;
my $hashref = shift;
my %json_hsh = %{$hashref};
my $worksheet = $workbook->add_worksheet($ws_name);
my ($row, $col);
$row = $col = 0;
$worksheet->write( $row, $col, 'UK English');
$col = 1;
$worksheet->write( $row, $col, 'Welsh' );
$row = 1;
for my $key (keys %json_hsh) {
$col = 0;
$worksheet->write($row, $col, $key);
$col = 1;
$worksheet->write($row, $col, $json_hsh{$key});
$row++;
}
}
my $excelfile = "welsh.xlsx";
$workbook = Excel::Writer::XLSX->new( $excelfile );
die "Problems creating new Excel file: $!" unless defined $workbook;
my %common = %{ read_json_file("common") };
write_worksheet("common", \%common);
my %glossary = %{ read_json_file("glossary") };
write_worksheet("glossary", \%glossary);
my %profile = %{ read_json_file("profile") };
write_worksheet("profile", \%profile);
my %timetables = %{ read_json_file("timetables") };
write_worksheet("timetables", \%timetables);
$workbook->close();