Module: Workbook::Writers::XlsxWriter
- Included in:
- Book
- Defined in:
- lib/workbook/writers/xlsx_writer.rb
Instance Method Summary collapse
- #format_to_xlsx_format(f) ⇒ Object
- #formats_to_xlsx_format ⇒ Object
- #init_xlsx_spreadsheet_template ⇒ Object
- #make_sure_f_is_a_workbook_format(f) ⇒ Object
-
#stream_xlsx(options = {}) ⇒ String
Generates an string ready to be streamed as XLSX.
-
#to_xlsx(options = {}) ⇒ Axlsx::Package
Generates an axlsx doc, ready for export to XLSX.
-
#write_to_xlsx(filename = "#{title}.xlsx", options = {}) ⇒ Object
Write the current workbook to Microsoft Excel’s XML format (using the Axlsx gem).
- #xlsx_sheet(a) ⇒ Object
- #xlsx_template ⇒ Object
Instance Method Details
#format_to_xlsx_format(f) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 99 def format_to_xlsx_format f f = make_sure_f_is_a_workbook_format f xlsfmt={} xlsfmt[:fg_color] = "FF#{f[:color].to_s.upcase}".gsub("#",'') if f[:color] xlsfmt[:b] = true if f[:font_weight].to_s == "bold" or f[:font_weight].to_i >= 600 or f[:"font_style"].to_s.match "oblique" xlsfmt[:i] = true if f[:font_style].to_s == "italic" xlsfmt[:u] = true if f[:text_decoration].to_s.match("underline") xlsfmt[:bg_color] = f[:background_color] if f[:background_color] xlsfmt[:format_code] = strftime_to_ms_format(f[:number_format]) if f[:number_format] xlsfmt[:font_name] = f[:font_family].split.first if f[:font_family] # xlsfmt[:family] = parse_font_family(f) if f[:font_family] f.add_raw init_xlsx_spreadsheet_template.workbook.styles.add_style(xlsfmt) f.add_raw xlsfmt return xlsfmt end |
#formats_to_xlsx_format ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 87 def formats_to_xlsx_format template.formats.each do |n,v| v.each do | t,s | format_to_xlsx_format(s) end end end |
#init_xlsx_spreadsheet_template ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 76 def init_xlsx_spreadsheet_template if self.xlsx_template.is_a? Axlsx::Package return self.xlsx_template else t = Axlsx::Package.new template.add_raw t # template.set_default_formats! return t end end |
#make_sure_f_is_a_workbook_format(f) ⇒ Object
95 96 97 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 95 def make_sure_f_is_a_workbook_format f f.is_a?(Workbook::Format) ? f : Workbook::Format.new({}, f) end |
#stream_xlsx(options = {}) ⇒ String
Generates an string ready to be streamed as XLSX
49 50 51 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 49 def stream_xlsx = {} to_xlsx().to_stream.string end |
#to_xlsx(options = {}) ⇒ Axlsx::Package
Generates an axlsx doc, ready for export to XLSX
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 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 16 def to_xlsx ={} formats_to_xlsx_format book = init_xlsx_spreadsheet_template.workbook book.worksheets.pop(book.worksheets.count - self.count) if book.worksheets and book.worksheets.count > self.count self.each_with_index do |s,si| xlsx_sheet = xlsx_sheet(si) xlsx_sheet.name = s.name s.table.each_with_index do |r, ri| xlsx_row = xlsx_sheet[ri] ? xlsx_sheet[ri] : xlsx_sheet.add_row xlsx_row.height = 16 xlsx_row_a = xlsx_row.to_ary r.each_with_index do |c, ci| xlsx_row.add_cell(c.value) unless xlsx_row_a[ci] xlsx_cell = xlsx_row_a[ci] xlsx_cell.value = c.value # if c.format? # format_to_xlsx_format(c.format) unless c.format.raws[Fixnum] # xlsx_cell.style = c.format.raws[Fixnum] # end end xlsx_sheet.send(:update_column_info, xlsx_row.cells, []) end (xlsx_sheet.rows.count - s.table.count).times do |time| xlsx_sheet.rows.pop end end init_xlsx_spreadsheet_template end |
#write_to_xlsx(filename = "#{title}.xlsx", options = {}) ⇒ Object
Write the current workbook to Microsoft Excel’s XML format (using the Axlsx gem)
57 58 59 60 61 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 57 def write_to_xlsx filename="#{title}.xlsx", ={} if to_xlsx().serialize(filename) return filename end end |
#xlsx_sheet(a) ⇒ Object
63 64 65 66 67 68 69 70 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 63 def xlsx_sheet a if xlsx_template.workbook.worksheets[a] return xlsx_template.workbook.worksheets[a] else xlsx_template.workbook.add_worksheet self.xlsx_sheet a end end |
#xlsx_template ⇒ Object
72 73 74 |
# File 'lib/workbook/writers/xlsx_writer.rb', line 72 def xlsx_template return template.raws[Axlsx::Package] end |