Class: Workbook

Inherits:
BIFFWriter show all
Defined in:
lib/spreadsheet/workbook.rb

Direct Known Subclasses

Spreadsheet::Excel

Constant Summary collapse

BOF =
11
EOF =
4
SheetName =
"Sheet"

Constants inherited from BIFFWriter

BIFFWriter::BIFF_Version, BIFFWriter::BigEndian

Instance Attribute Summary collapse

Attributes inherited from BIFFWriter

#byte_order, #data, #datasize

Instance Method Summary collapse

Methods inherited from BIFFWriter

#append, #prepend, #store_bof, #store_eof

Constructor Details

#initialize(file) ⇒ Workbook

Returns a new instance of Workbook.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/spreadsheet/workbook.rb', line 9

def initialize(file)
   super

   @file       = file
   @format     = Format.new

   @active_sheet = 0
   @first_sheet  = 0
   @biffsize     = 0
   @date_system  = 1900
   @xf_index     = 16

   @worksheets = []
   @formats    = []

   @url_format = add_format(:color=>"blue", :underline=>1)
end

Instance Attribute Details

#date_systemObject

Returns the value of attribute date_system.



6
7
8
# File 'lib/spreadsheet/workbook.rb', line 6

def date_system
  @date_system
end

#formatsObject (readonly)

Returns the value of attribute formats.



7
8
9
# File 'lib/spreadsheet/workbook.rb', line 7

def formats
  @formats
end

#worksheetsObject (readonly)

Returns the value of attribute worksheets.



7
8
9
# File 'lib/spreadsheet/workbook.rb', line 7

def worksheets
  @worksheets
end

#xf_indexObject (readonly)

Returns the value of attribute xf_index.



7
8
9
# File 'lib/spreadsheet/workbook.rb', line 7

def xf_index
  @xf_index
end

Instance Method Details

#add_format(*args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/spreadsheet/workbook.rb', line 31

def add_format(*args)
   if args[0].kind_of?(Hash)
      f = Format.new(args[0], @xf_index)
   elsif args[0].nil?
      f = Format.new
   else
      raise TypeError unless args[0].kind_of?(Format)
      f = args[0]
      f.xf_index = @xf_index
   end
   @xf_index += 1
   @formats.push(f)
   return f
end

#add_worksheet(name = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/spreadsheet/workbook.rb', line 46

def add_worksheet(name=nil)
   index = @worksheets.length

   if name.nil?
      name = SheetName + (index + 1).to_s
   end
   
   args = [name,index, @active_sheet, @first_sheet, @url_format]
   ws = Worksheet.new(*args)
   @worksheets[index] = ws
   return ws
end

#calc_sheet_offsetsObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spreadsheet/workbook.rb', line 59

def calc_sheet_offsets
   offset = @datasize
   @worksheets.each{ |sheet|
      offset += BOF + sheet.name.length
   }

   offset += EOF

   @worksheets.each{ |sheet|
      sheet.offset = offset
      offset += sheet.datasize
   }

   @biffsize = offset
end

#closeObject



27
28
29
# File 'lib/spreadsheet/workbook.rb', line 27

def close
   store_workbook
end

#store_all_fontsObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/spreadsheet/workbook.rb', line 131

def store_all_fonts
   font = @format.font_biff
   for n in 1..5
      append(font)
   end

   fonts = Hash.new(0)
   index = 6
   key = @format.font_key
   fonts[key] = 0

   @formats.each{ |format|
      key = format.font_key
      if fonts.has_key?(key)
         format.font_index = fonts[key]
      else
         fonts[key] = index
         format.font_index = index
         index += 1
         append(format.font_biff)
      end
   }
end

#store_all_num_formatsObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/spreadsheet/workbook.rb', line 174

def store_all_num_formats
   index = 164

   num_formats_hash = {}
   num_formats_array = []

   @formats.each{ |format|
      num_format = format.num_format
      next if num_format.kind_of?(Numeric)
      if num_formats_hash.has_key?(num_format)
         format.num_format = num_formats_hash[num_format]
      else
         num_formats_hash[num_format] = index
         format.num_format = index
         num_formats_array.push(num_format)
         index += 1
      end
   }

   index = 164
   num_formats_array.each{ |num_format|
      store_num_format(num_format,index)
      index += 1
   }
end

#store_all_xfsObject



200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/spreadsheet/workbook.rb', line 200

def store_all_xfs
   xf = @format.xf_biff(0xFFF5)
   for n in 1..15
      append(xf)
   end

   xf = @format.xf_biff(0x0001)
   append(xf)
      
   @formats.each{ |format|
      xf = format.xf_biff(0x0001)
      append(xf)
   }
end

#store_boundsheet(sheet_name, offset) ⇒ Object



231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/spreadsheet/workbook.rb', line 231

def store_boundsheet(sheet_name, offset)
   name   = 0x0085
   length = 0x07 + sheet_name.length

   grbit = 0x0000
   cch   = sheet_name.length

   header = [name, length].pack("vv")
   data   = [offset, grbit, cch].pack("VvC")

   append(header, data, sheet_name)
end

#store_date_systemObject



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/spreadsheet/workbook.rb', line 255

def store_date_system
   record = 0x0022
   length = 0x0002
   
   f1904 = 0
   f1904 = 1 if @date_system == 1904

   header = [record, length].pack("vv")
   data   = [f1904].pack("v")

   append(header, data)
end

#store_num_format(format, ifmt) ⇒ Object



244
245
246
247
248
249
250
251
252
253
# File 'lib/spreadsheet/workbook.rb', line 244

def store_num_format(format, ifmt)
   record = 0x041E
   cch    = format.length
   length = 0x03 + cch

   header = [record, length].pack("vv")
   data   = [ifmt, cch].pack("vC")

   append(header, data, format)
end

#store_ole_fileObject



97
98
99
100
101
102
103
104
105
106
# File 'lib/spreadsheet/workbook.rb', line 97

def store_ole_file
   OLEWriter.open(@file){ |ole|
      ole.set_size(@biffsize)
      ole.write_header
      ole.print(@data)
      @worksheets.each{ |sheet|
         ole.print(sheet.data)
      }
   }
end

#store_styleObject Also known as: store_all_styles



215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/spreadsheet/workbook.rb', line 215

def store_style
   record = 0x0293
   length = 0x0004

   ixfe    = 0x8000
   builtin = 0x00
   iLevel  = 0xff

   header = [record, length].pack("vv")
   data   = [ixfe, builtin, iLevel].pack("vCC")

   append(header, data)
end

#store_window1Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/spreadsheet/workbook.rb', line 108

def store_window1
   record    = 0x003D
   length    = 0x0012

   xWn       = 0x0000
   yWn       = 0x0000
   dxWn      = 0x25BC
   dyWn      = 0x1572

   grbit     = 0x0038
   ctabsel   = 0x0001
   wTabRatio = 0x0258

   itabFirst = @first_sheet
   itabCur   = @active_sheet

   header = [record,length].pack("vv")
   fields = [xWn,yWn,dxWn,dyWn,grbit,itabCur,itabFirst,ctabsel,wTabRatio]
   data   = fields.pack("vvvvvvvvv")

   append(header,data)
end

#store_workbookObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spreadsheet/workbook.rb', line 75

def store_workbook
   @worksheets.each{ |sheet|
      sheet.close
   }

   store_bof(0x0005)
   store_window1
   store_date_system
   store_all_fonts
   store_all_num_formats
   store_all_xfs
   store_all_styles
   calc_sheet_offsets

   @worksheets.each{ |sheet|
      store_boundsheet(sheet.name, sheet.offset)
   }

   store_eof
   store_ole_file
end

#store_xf(style) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/spreadsheet/workbook.rb', line 155

def store_xf(style)
   name   = 0x00E0
   length = 0x0010

   ifnt      = 0x0000
   ifmt      = 0x0000
   align     = 0x0020
   icv       = 0x20C0
   fill      = 0x0000
   brd_line  = 0x0000
   brd_color = 0x0000

   header = [name, length].pack("vv")
   fields = [ifnt,ifmt,style,align,icv,fill,brd_line,brd_color]
   data   = fields.pack("vvvvvvvv")

   append(header, data);
end