Class: OLEWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/WriteExcel/olewriter.rb

Constant Summary collapse

MaxSize =

Not meant for public consumption

7087104
BlockSize =

Use Spreadsheet::WriteExcel::Big to exceed this

4096
BlockDiv =
512
ListBlocks =
127

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ OLEWriter

Accept an IO or IO-like object or a filename (as a String)



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/WriteExcel/olewriter.rb', line 28

def initialize(arg)
  if arg.kind_of?(String)
    @io = File.open(arg, "w")
  else
    @io = arg
  end
  @io.binmode

  @filehandle    = ""
  @fileclosed    = false
  @internal_fh   = 0
  @biff_only     = 0
  @size_allowed  = true
  @biff_size     = 0
  @book_size     = 0
  @big_blocks    = 0
  @list_blocks   = 0
  @root_start    = 0
  @block_count   = 4
end

Instance Attribute Details

#biff_onlyObject

Returns the value of attribute biff_only.



25
26
27
# File 'lib/WriteExcel/olewriter.rb', line 25

def biff_only
  @biff_only
end

#biff_sizeObject (readonly)

Returns the value of attribute biff_size.



23
24
25
# File 'lib/WriteExcel/olewriter.rb', line 23

def biff_size
  @biff_size
end

#big_blocksObject (readonly)

Returns the value of attribute big_blocks.



23
24
25
# File 'lib/WriteExcel/olewriter.rb', line 23

def big_blocks
  @big_blocks
end

#book_sizeObject (readonly)

Returns the value of attribute book_size.



23
24
25
# File 'lib/WriteExcel/olewriter.rb', line 23

def book_size
  @book_size
end

#internal_fhObject

Returns the value of attribute internal_fh.



25
26
27
# File 'lib/WriteExcel/olewriter.rb', line 25

def internal_fh
  @internal_fh
end

#list_blocksObject (readonly)

Returns the value of attribute list_blocks.



23
24
25
# File 'lib/WriteExcel/olewriter.rb', line 23

def list_blocks
  @list_blocks
end

#root_startObject (readonly)

Returns the value of attribute root_start.



24
25
26
# File 'lib/WriteExcel/olewriter.rb', line 24

def root_start
  @root_start
end

#size_allowedObject (readonly)

Returns the value of attribute size_allowed.



24
25
26
# File 'lib/WriteExcel/olewriter.rb', line 24

def size_allowed
  @size_allowed
end

Class Method Details

.open(arg) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/WriteExcel/olewriter.rb', line 77

def self.open(arg)
  if block_given?
    ole = self.new(arg)
    result = yield(ole)
    ole.close
    result
  else
    self.new(arg)
  end
end

Instance Method Details

#_initializeObject

_initialize()

Create a new filehandle or use the provided filehandle.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/WriteExcel/olewriter.rb', line 57

def _initialize
  olefile = @olefilename

  # If the filename is a reference it is assumed that it is a valid
  # filehandle, if not we create a filehandle.
  #

  # Create a new file, open for writing
  fh = open(olefile, "wb")

  # Workbook.pm also checks this but something may have happened since
  # then.
  raise "Can't open olefile. It may be in use or protected.\n" unless fh

  @internal_fh = 1

  # Store filehandle
  @filehandle = fh
end

#calculate_sizesObject

_calculate_sizes()

Calculate various sizes needed for the OLE stream



135
136
137
138
139
# File 'lib/WriteExcel/olewriter.rb', line 135

def calculate_sizes
  @big_blocks  = (@book_size.to_f/BlockDiv.to_f).ceil
  @list_blocks = (@big_blocks / ListBlocks) + 1
  @root_start  = @big_blocks
end

#closeObject

close()

Write root entry, big block list and close the filehandle. This routine is used to explicitly close the open filehandle without having to wait for DESTROY.



149
150
151
152
153
154
155
156
157
158
159
# File 'lib/WriteExcel/olewriter.rb', line 149

def close
  if @size_allowed == true
#print "write_padding"
    write_padding          if @biff_only == 0
#print "write_property_storage"
    write_property_storage if @biff_only == 0
#print "write_big_block_depot"
    write_big_block_depot  if @biff_only == 0
  end
  @io.close
end

#set_size(size = BlockSize) ⇒ Object

set_size($biffsize)

Set the size of the data to be written to the OLE stream

$big_blocks = (109 depot block x (128 -1 marker word)
              - (1 x end words)) = 13842
$maxsize    = $big_blocks * 512 bytes = 7087104


113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/WriteExcel/olewriter.rb', line 113

def set_size(size = BlockSize)
  if size > MaxSize
    return @size_allowed = false
  end

  @biff_size = size

  if biff_size > BlockSize
    @book_size = size
  else
    @book_size = BlockSize
  end

  @size_allowed = true
end

#write(data) ⇒ Object

write($data)

Write BIFF data to OLE file.



94
95
96
97
98
# File 'lib/WriteExcel/olewriter.rb', line 94

def write(data)
#print "ole write\n"
#print data.unpack('C*').map! {|c| sprintf("%02X", c) }.join(' ') + "\n\n"
  @io.write(data)
end

#write_big_block_depotObject

_write_big_block_depot()

Write big block depot.



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/WriteExcel/olewriter.rb', line 215

def write_big_block_depot
  num_blocks   = @big_blocks
  num_lists    = @list_blocks
  total_blocks = num_lists * 128
  used_blocks  = num_blocks + num_lists + 2

  marker          = [-3].pack("V")
  end_of_chain    = [-2].pack("V")
  unused          = [-1].pack("V")

  1.upto(num_blocks-1){|n|
    write([n].pack("V"))
  }

  write end_of_chain
  write end_of_chain

  1.upto(num_lists){ write(marker) }

  used_blocks.upto(total_blocks){ write(unused) }

end

#write_headerObject

write_header()

Write OLE header block.



167
168
169
170
171
172
173
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
199
200
201
202
203
204
205
206
207
# File 'lib/WriteExcel/olewriter.rb', line 167

def write_header
  return if @biff_only == 1
  calculate_sizes
  root_start = @root_start
  num_lists  = @list_blocks

  id              = [0xD0CF11E0, 0xA1B11AE1].pack("NN")
  unknown1        = [0x00, 0x00, 0x00, 0x00].pack("VVVV")
  unknown2        = [0x3E, 0x03].pack("vv")
  unknown3        = [-2].pack("v")
  unknown4        = [0x09].pack("v")
  unknown5        = [0x06, 0x00, 0x00].pack("VVV")
  num_bbd_blocks  = [num_lists].pack("V")
  root_startblock = [root_start].pack("V")
  unknown6        = [0x00, 0x1000].pack("VV")
  sbd_startblock  = [-2].pack("V")
  unknown7        = [0x00, -2 ,0x00].pack("VVV")

  write(id)
  write(unknown1)
  write(unknown2)
  write(unknown3)
  write(unknown4)
  write(unknown5)
  write(num_bbd_blocks)
  write(root_startblock)
  write(unknown6)
  write(sbd_startblock)
  write(unknown7)

  unused = [-1].pack("V")

  1.upto(num_lists){
    root_start += 1
    write([root_start].pack("V"))
  }

  num_lists.upto(108){
    write(unused)
  }
end

#write_paddingObject

_write_padding()

Pad the end of the file



313
314
315
316
317
318
319
320
321
# File 'lib/WriteExcel/olewriter.rb', line 313

def write_padding
  min_size = 512
  min_size = BlockSize if @biff_size < BlockSize

  if @biff_size % min_size != 0
    padding = min_size - (@biff_size % min_size)
    write("\0" * padding)
  end
end

#write_pps(name, type, dir, start, size) ⇒ Object

_write_pps()

Write property sheet in property storage



259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/WriteExcel/olewriter.rb', line 259

def write_pps(name, type, dir, start, size)
  length = 0
  ord_name = []
  unless name.empty?
    name = name + "\0"
    ord_name = name.unpack("c*")
    length = name.length * 2
  end

  rawname        = ord_name.pack("v*")
  zero           = [0].pack("C")

  pps_sizeofname = [length].pack("v")   #0x40
  pps_type       = [type].pack("v")     #0x42
  pps_prev       = [-1].pack("V")       #0x44
  pps_next       = [-1].pack("V")       #0x48
  pps_dir        = [dir].pack("V")      #0x4c

  unknown = [0].pack("V")

  pps_ts1s       = [0].pack("V")        #0x64
  pps_ts1d       = [0].pack("V")        #0x68
  pps_ts2s       = [0].pack("V")        #0x6c
  pps_ts2d       = [0].pack("V")        #0x70
  pps_sb         = [start].pack("V")    #0x74
  pps_size       = [size].pack("V")     #0x78

  write(rawname)
  for n in 1..64-length
    write(zero)
  end
  write(pps_sizeofname)
  write(pps_type)
  write(pps_prev)
  write(pps_next)
  write(pps_dir)
  for n in 1..5
    write(unknown)
  end
  write(pps_ts1s)
  write(pps_ts1d)
  write(pps_ts2s)
  write(pps_ts2d)
  write(pps_sb)
  write(pps_size)
  write(unknown)
end

#write_property_storageObject

_write_property_storage()

Write property storage. TODO: add summary sheets



244
245
246
247
248
249
250
251
# File 'lib/WriteExcel/olewriter.rb', line 244

def write_property_storage

  #########  name         type  dir start size
  write_pps('Root Entry', 0x05,  1,   -2, 0x00)
  write_pps('Workbook',   0x02, -1, 0x00, @book_size)
  write_pps("",           0x00, -1, 0x00, 0x0000)
  write_pps("",           0x00, -1, 0x00, 0x0000)
end