diff options
| author | 2017-06-12 03:37:11 -0400 | |
|---|---|---|
| committer | 2017-06-12 03:37:11 -0400 | |
| commit | 8280a21a23d44aa90177e2bc041d0b8dc8556f4b (patch) | |
| tree | dadef7ee085c0e990a5070bd41b6a5b98c97f4fd /lib/roo/excelx/styles.rb | |
Import Upstream version 2.7.1upstream/2.7.1
Diffstat (limited to 'lib/roo/excelx/styles.rb')
| -rw-r--r-- | lib/roo/excelx/styles.rb | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/roo/excelx/styles.rb b/lib/roo/excelx/styles.rb new file mode 100644 index 0000000..87f1713 --- /dev/null +++ b/lib/roo/excelx/styles.rb @@ -0,0 +1,64 @@ +require 'roo/font' +require 'roo/excelx/extractor' + +module Roo + class Excelx + class Styles < Excelx::Extractor + # convert internal excelx attribute to a format + def style_format(style) + id = num_fmt_ids[style.to_i] + num_fmts[id] || Excelx::Format::STANDARD_FORMATS[id.to_i] + end + + def definitions + @definitions ||= extract_definitions + end + + private + + def num_fmt_ids + @num_fmt_ids ||= extract_num_fmt_ids + end + + def num_fmts + @num_fmts ||= extract_num_fmts + end + + def fonts + @fonts ||= extract_fonts + end + + def extract_definitions + doc.xpath('//cellXfs').flat_map do |xfs| + xfs.children.map do |xf| + fonts[xf['fontId'].to_i] + end + end + end + + def extract_fonts + doc.xpath('//fonts/font').map do |font_el| + Font.new.tap do |font| + font.bold = !font_el.xpath('./b').empty? + font.italic = !font_el.xpath('./i').empty? + font.underline = !font_el.xpath('./u').empty? + end + end + end + + def extract_num_fmt_ids + doc.xpath('//cellXfs').flat_map do |xfs| + xfs.children.map do |xf| + xf['numFmtId'] + end + end.compact + end + + def extract_num_fmts + Hash[doc.xpath('//numFmt').map do |num_fmt| + [num_fmt['numFmtId'], num_fmt['formatCode']] + end] + end + end + end +end |
