Class: Spreadshoot
- Inherits:
-
Object
- Object
- Spreadshoot
- Defined in:
- lib/spreadshoot.rb,
lib/spreadshoot/version.rb
Defined Under Namespace
Classes: Cell, Row, Table, Worksheet
Constant Summary collapse
- VERSION =
"0.0.5"
Instance Method Summary collapse
-
#border(borders) ⇒ Object
gets the shared index of a border set (one or more of :top, :bottom, :left, :right).
-
#dump ⇒ Object
Dumps main XMLs to the stdout (for debugging purposes).
- #font(options) ⇒ Object
-
#initialize(options = {}) {|_self| ... } ⇒ Spreadshoot
constructor
Create a new sheet, with given default formatting options.
-
#save(filename) ⇒ Object
Saves the spreadsheet to an XLSX file with a given name.
-
#ss_index(string) ⇒ Object
Gets the shared index of a given string.
-
#style(options = {}) ⇒ Object
gets the shared index of a cell style (given by options hash).
-
#worksheet(title, options = {}, &block) ⇒ Object
Create a new worksheet within a spreadsheet.
Constructor Details
#initialize(options = {}) {|_self| ... } ⇒ Spreadshoot
Create a new sheet, with given default formatting options
9 10 11 12 13 14 15 16 17 |
# File 'lib/spreadshoot.rb', line 9 def initialize = {}, &block @worksheets = [] @ss = {} @borders = {} @fonts = {} @styles = {} @components = Set.new yield(self) end |
Instance Method Details
#border(borders) ⇒ Object
gets the shared index of a border set (one or more of :top, :bottom, :left, :right)
30 31 32 33 34 35 36 37 38 |
# File 'lib/spreadshoot.rb', line 30 def border borders borders = [borders] unless borders.is_a?(Array) borders.sort! unless i = @borders[borders] i = @borders.length @borders[borders] = i end i end |
#dump ⇒ Object
Dumps main XMLs to the stdout (for debugging purposes)
129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/spreadshoot.rb', line 129 def dump puts @xml @worksheets.each do |ws| puts '===' puts ws end puts '===' puts shared_strings puts '===' puts styles end |
#font(options) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/spreadshoot.rb', line 40 def font unless i = @fonts[] i = @fonts.length @fonts[] = i end i end |
#save(filename) ⇒ Object
Saves the spreadsheet to an XLSX file with a given name
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 |
# File 'lib/spreadshoot.rb', line 89 def save filename dir = File.join(File.dirname(filename), "spreadshoot-%06x/" % rand(256**3)) FileUtils.rm_rf(dir) FileUtils.mkdir_p(dir) FileUtils.mkdir_p(File.join(dir, '_rels')) FileUtils.mkdir_p(File.join(dir, 'xl', 'worksheets')) FileUtils.mkdir_p(File.join(dir, 'xl', '_rels')) File.open(File.join(dir, '[Content_Types].xml'), 'w') do |f| f.write content_types end File.open(File.join(dir, '_rels', '.rels'), 'w') do |f| f.write rels end File.open(File.join(dir, 'xl', 'workbook.xml'), 'w') do |f| f.write workbook end @worksheets.each_with_index do |ws, i| File.open(File.join(dir, 'xl', 'worksheets', "sheet#{i+1}.xml"), 'w') do |f| f.write(ws) end end File.open(File.join(dir, 'xl', 'sharedStrings.xml'), 'w') do |f| f.write shared_strings end if @components.member?(:ss) File.open(File.join(dir, 'xl', 'styles.xml'), 'w') do |f| f.write styles end File.open(File.join(dir, 'xl', '_rels', 'workbook.xml.rels'), 'w') do |f| f.write xl_rels end filename = File.absolute_path(filename) FileUtils.chdir(dir) File.delete(filename) if File.exists?(filename) # zip the result puts `zip -r #{filename} ./` FileUtils.rm_rf(dir) end |
#ss_index(string) ⇒ Object
Gets the shared index of a given string
20 21 22 23 24 25 26 27 |
# File 'lib/spreadshoot.rb', line 20 def ss_index string @components << :ss unless i = @ss[string] i = @ss.length @ss[string] = i end i end |
#style(options = {}) ⇒ Object
gets the shared index of a cell style (given by options hash)
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 |
# File 'lib/spreadshoot.rb', line 49 def style = {} font = {} style = .each_with_object({}) do |(option, value), acc| case option when :border acc[:border] = self.border(value) when :align acc[:align] = value when :bold, :italic, :font font[option] = value when :format acc[:format] = case value when :date 14 when :percent 10 when :currency 7 else value end end end style[:font] = self.font(font) unless font.empty? return nil if style.empty? unless i = @styles[style] i = @styles.length @styles[style] = i end i end |