Class: Pangrid::ExcelXSLX

Inherits:
Plugin
  • Object
show all
Defined in:
lib/pangrid/plugins/excel.rb

Constant Summary collapse

DESCRIPTION =
"Excel writer. Useful to upload to google sheets."
STYLES =

styles

{
  :black => {:bg_color => "00"},
  :white => {
    :bg_color => "FF", :fg_color => "00",
    :alignment => { :horizontal=> :right },
    :border => Axlsx::STYLE_THIN_BORDER
  }
}
SUPERSCRIPTS =

cobble a cell number together out of unicode superscript chars

%w( ¹ ² ³      )

Constants inherited from Plugin

Plugin::FAILED, Plugin::MISSING_DEPS, Plugin::REGISTRY

Instance Method Summary collapse

Methods inherited from Plugin

class_to_name, get, inherited, list_all, load_all, load_plugin

Methods included from PluginUtils

#check

Instance Method Details

#format_number(n) ⇒ Object



30
31
32
# File 'lib/pangrid/plugins/excel.rb', line 30

def format_number(n)
  n.to_s.split(//).map {|c| SUPERSCRIPTS[c.to_i]}.join("").rjust(3)
end

#write(xw) ⇒ Object



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
# File 'lib/pangrid/plugins/excel.rb', line 34

def write(xw)
  xw.number
  rows = xw.to_array(:black => " ", :null => " ") {|c|
    format_number(c.number) + " " +
    # We can insert the entire word for rebuses
    c.solution
  }

  styles = xw.to_array(:black => :black, :null => :white) {
    :white
  }

  p = Axlsx::Package.new
  wb = p.workbook

  # styles
  wb.styles do |s|
    xstyles = {}
    STYLES.map {|k, v| xstyles[k] = s.add_style v}
    wb.add_worksheet(:name => "Crossword") do |sheet|
      rowstyles = styles.map {|r|
        r.map {|c| xstyles[c]}
      }
      rows.zip(rowstyles).each {|r, s|
        sheet.add_row r, :style => s
      }
    end
  end

  out = p.to_stream(true)
  check("Spreadsheet did not validate") { out }
  out.read
end