-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexcel2html.rb
388 lines (304 loc) · 9.19 KB
/
excel2html.rb
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env ruby
require 'rubyXL'
require 'rubyXL/convenience_methods'
require 'securerandom'
MARKERS = {
'F79646' => 'marker-orange',
'E46C0A' => 'marker-orange',
'FFC000' => 'marker-orange',
'E6B9B8' => 'marker-orange',
'D99694' => 'marker-orange',
'FF9900' => 'marker-orange',
'996633' => 'marker-brown',
'984807' => 'marker-brown',
'948A54' => 'marker-brown',
'CC9900' => 'marker-orange',
'CC6600' => 'marker-orange',
'4A452A' => 'marker-terra-cota',
'FFFF00' => 'marker-yellow',
'008080' => 'marker-green',
'006600' => 'marker-green',
'009900' => 'marker-green',
'00B050' => 'marker-green',
'92D050' => 'marker-green',
'9BBB59' => 'marker-green',
'77933C' => 'marker-green',
'4F6228' => 'marker-green',
'0000FF' => 'marker-blue',
'0070C0' => 'marker-blue',
'00B0F0' => 'marker-blue',
'4BACC6' => 'marker-blue',
'558ED5' => 'marker-blue',
'B7DEE8' => 'marker-blue',
'93CDDD' => 'marker-blue',
'31859C' => 'marker-dark-blue',
'4F81BD' => 'marker-dark-blue',
'1F497D' => 'marker-dark-blue',
'376092' => 'marker-dark-blue',
'002060' => 'marker-dark-blue',
'10253F' => 'marker-dark-blue',
'17375E' => 'marker-dark-blue',
'215968' => 'marker-dark-blue',
'254061' => 'marker-dark-blue',
'6600FF' => 'marker-purple',
'7030A0' => 'marker-purple',
'8064A2' => 'marker-purple',
'B3A2C7' => 'marker-purple',
'CCC1DA' => 'marker-purple',
'604A7B' => 'marker-purple',
'9900FF' => 'marker-purple',
'9933FF' => 'marker-purple',
'FF66CC' => 'marker-pink',
'FF00FF' => 'marker-pink',
'C00000' => 'marker-red',
'FF0000' => 'marker-red',
'C0504D' => 'marker-red',
'953735' => 'marker-brique',
'632523' => 'marker-brique',
'808080' => 'marker-grey',
'A6A6A6' => 'marker-grey',
'BFBFBF' => 'marker-grey',
'D9D9D9' => 'marker-grey',
}
# FIX limitation in RubyXL
module RubyXL
module ColorConvenienceMethods
def get_rgb(workbook)
if rgb then
return rgb
elsif theme then
theme_color = workbook.theme.get_theme_color(theme)
rgb_color = theme_color && theme_color.a_srgb_clr
color_value = rgb_color && rgb_color.val
# FIX - Handle system colors
unless color_value then
rgb_color = theme_color && theme_color.a_sys_clr
color_value = rgb_color && rgb_color.last_clr
end
# END FIX
return nil if color_value.nil?
RubyXL::RgbColor.parse(color_value).to_hls.apply_tint(tint).to_rgb.to_s
end
end
end
end
module RubyXL
class Worksheet
def images
@images
end
def images=(array)
@images = Hash.new{|h,k| h[k]=[]}
array.each { |img|
# Fix image format (.wmf is not valid, but .emf is)
img.format = img.format.gsub(/wmf/i, 'emf')
from = img.anchor._from
to = img.anchor.to if img.anchor.kind_of?(TwoCellAnchor)
# Images in merged ranges will be ignored, except the ones in the top-left corner.
# So we reassign merged images to the top-left cell.
self.merged_cells.each { |mcell|
row_range, col_range = mcell.ref.row_range, mcell.ref.col_range
if row_range.member?(from.row) && col_range.member?(from.col)
unless row_range.first == from.row && col_range.first == from.col
# TODO: Use some average, like the barycenter, instead of top-left corner
dr = from.row - row_range.first
dc = from.col - col_range.first
from.row -= dr
from.col -= dc
to.row -= dr if to
to.col -= dc if to
end
end
}
@images[[from.row, from.col]].append(img)
}
end
end
end
def font_color(cell)
# Instead of convenience method: cell.font_color, which is buggy
cell.get_cell_font.color
end
def rgb(color, cell)
return nil if color.nil?
rgb = if color.is_a? String
color
else
color.get_rgb(cell.worksheet.workbook)
end
rgb = rgb.upcase unless rgb.nil?
if rgb =~ /^(FF)?000000$/ || rgb =~ /0D0D0D/
nil
elsif rgb =~ /^(.{2,2})(.{6,6})$/
$2
else
rgb
end
end
def fill(cell)
color = cell.fill_color
rgb = rgb(color, cell)
if rgb && rgb !~ /FFFFFF/
" style='background-color:##{rgb};'"
else
''
end
end
def marker(rgb)
return nil if rgb.nil?
marker = MARKERS[rgb.upcase]
puts ">>> marker missing for: #{rgb}" if marker.nil?
marker
end
def styles(element)
{
b: element && element.b && (element.b != false),
i: element && element.i && (element.i != false),
u: element && element.u && (element.u != false),
strike: element && element.strike && (element.strike != false),
}
end
def header_to_html()
"""
<table>
<tbody>
"""
end
def footer_to_html()
"""
</tbody>
</table>
"""
end
def worksheet_to_html(worksheet)
s = header_to_html()
s << rows_to_html(worksheet)
s << footer_to_html()
end
def rows_to_html(worksheet)
s = ''
worksheet.each_with_index { |row, i|
unless row.nil?
if row.cells.count > 0
s << "<tr>\n"
row.cells.each_with_index { |cell, j|
if cell
s << cell_to_html(cell) unless omit?(cell)
else
s << "<td></td>\n"
end
}
s << "</tr>\n"
end
end
}
s
end
def cell_to_html(cell)
s = "<td#{span(cell)}#{fill(cell)}>"
# puts "Cell(#{cell.row}, #{cell.column})"
# s << "<span style='font-size: 8px;'>(#{cell.row}, #{cell.column})</span><br>\n"
s << value_to_html(cell)
if defined?(TwoCellAnchor)
s << image_to_html(cell)
end
s << "</td>\n"
end
def value_to_html(cell)
s = ''
defaults = styles(cell.get_cell_font)
if cell.value_container.nil?
s << ''
elsif cell.datatype != 's'
s << cell.value_container.value
else
shared_strings = cell.worksheet.workbook.shared_strings_container
rich_text = shared_strings[cell.raw_value.to_i]
if rich_text.r.count > 0
rich_text.r.each { |run|
s << run_to_html(
cell,
run.t.value,
run.r_pr ? run.r_pr.color : font_color(cell),
styles(run.r_pr),
defaults
)
}
else
s << run_to_html(
cell,
cell.value,
font_color(cell),
{},
defaults
)
end
end
s.gsub("\n", "<br>\n").gsub(/\n( )+/) { |match| "\n#{' ' * match.length}"}
end
def run_to_html(cell, value, color, locals, defaults)
s = ''
s << '<b>' if locals[:b] || defaults[:b]
s << '<i>' if locals[:i] || defaults[:i]
s << '<u>' if locals[:u] || defaults[:u]
s << '<strike>' if locals[:strike] || defaults[:strike]
if color
rgb = rgb(color, cell)
marker = marker(rgb)
s << "<mark class='#{marker}'>" unless marker.nil?
s << value
s << '</mark>' unless marker.nil?
else
s << value
end
s << '</b>' if locals[:b] || defaults[:b]
s << '</i>' if locals[:i] || defaults[:i]
s << '</u>' if locals[:u] || defaults[:u]
s << '</strike>' if locals[:strike] || defaults[:strike]
locals.each{|k, v| defaults.delete(k) unless v.nil? }
s
end
def image_to_html(cell)
s = ''
ws = cell.worksheet
ws.images[[cell.row, cell.column]].each_with_index { |img, i|
path = '.'
basename = SecureRandom.uuid #img._id
ext = img.format
File.open("#{path}/#{basename}.#{ext}", 'wb') { |f|
f.write(img.ref.getvalue())
}
if ext == 'emf'
`/usr/bin/inkscape -z --export-plain-svg=#{basename}.svg --file #{basename}.#{ext} && rm #{basename}.#{ext}`
ext = 'svg'
end
s << "<img src='#{path}/#{basename}.#{ext}' style='width:300px;'>"
}
s
end
def merged?(cell)
cell.worksheet.merged_cells.each { |mcell|
return true if mcell.ref.row_range.member?(cell.row) && mcell.ref.col_range.member?(cell.column)
}
false
end
def omit?(cell)
cell.worksheet.merged_cells.each { |mcell|
if mcell.ref.row_range.member?(cell.row) && mcell.ref.col_range.member?(cell.column)
if mcell.ref.row_range.first == cell.row && mcell.ref.col_range.first == cell.column
return false
else
return true
end
end
}
false
end
def span(cell)
cell.worksheet.merged_cells.each { |mcell|
if mcell.ref.row_range.first == cell.row && mcell.ref.col_range.first == cell.column
return " colspan='#{mcell.ref.col_range.size}' rowspan='#{mcell.ref.row_range.size}'"
end
}
''
end