Class: Spreadshoot

Inherits:
Object
  • Object
show all
Defined in:
lib/spreadshoot.rb,
lib/spreadshoot/version.rb

Defined Under Namespace

Classes: Cell, Row, Table, Worksheet

Constant Summary collapse

VERSION =
"0.0.6"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|_self| ... } ⇒ Spreadshoot

Create a new sheet, with given default formatting options

Yields:

  • (_self)

Yield Parameters:

  • _self (Spreadshoot)

    the object that the method was called on



9
10
11
12
13
14
15
16
17
# File 'lib/spreadshoot.rb', line 9

def initialize options = {}, &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

#dumpObject

Dumps main XMLs to the stdout (for debugging purposes)



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/spreadshoot.rb', line 139

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 options
  unless i = @fonts[options]
    i = @fonts.length
    @fonts[options] = i
  end
  i
end

#save(filename) ⇒ Object

Saves the spreadsheet to an XLSX file with a given name



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
# File 'lib/spreadshoot.rb', line 97

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)
  current_dir = FileUtils.pwd
  FileUtils.chdir(dir)
  File.delete(filename) if File.exists?(filename)
  # zip the result
  puts `zip -r #{filename} ./`
  FileUtils.chdir(current_dir)
  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
81
82
83
84
85
86
87
88
# File 'lib/spreadshoot.rb', line 49

def style options = {}
  font = {}
  style = options.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 :percent_rounded
                       9
                     when :currency
                       7
                     when :thousands_separated_decimals
                       4
                     when :thousands_separated
                       3
                     when :two_decimals
                       2
                     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

#worksheet(title, options = {}, &block) ⇒ Object

Create a new worksheet within a spreadsheet



91
92
93
94
# File 'lib/spreadshoot.rb', line 91

def worksheet title, options = {}, &block
  ws = Worksheet.new(self, title, options, &block)
  @worksheets << ws
end