Class: OLEWriter

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

Overview

:nodoc:

Constant Summary collapse

MaxSize =

Not meant for public consumption

7087104
BlockSize =

Use 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)



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

def initialize(arg)
  if arg.respond_to?(:to_str)
    @io = File.open(arg, "w")
  else
    @io = arg
  end
  @io.binmode if @io.respond_to?(: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.



26
27
28
# File 'lib/writeexcel/olewriter.rb', line 26

def biff_only
  @biff_only
end

#biff_sizeObject (readonly)

Returns the value of attribute biff_size.



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

def biff_size
  @biff_size
end

#big_blocksObject (readonly)

Returns the value of attribute big_blocks.



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

def big_blocks
  @big_blocks
end

#book_sizeObject (readonly)

Returns the value of attribute book_size.



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

def book_size
  @book_size
end

#internal_fhObject

Returns the value of attribute internal_fh.



26
27
28
# File 'lib/writeexcel/olewriter.rb', line 26

def internal_fh
  @internal_fh
end

#list_blocksObject (readonly)

Returns the value of attribute list_blocks.



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

def list_blocks
  @list_blocks
end

#root_startObject (readonly)

Returns the value of attribute root_start.



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

def root_start
  @root_start
end

#size_allowedObject (readonly)

Returns the value of attribute size_allowed.



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

def size_allowed
  @size_allowed
end

Class Method Details

.open(arg) ⇒ Object



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

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.



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

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



131
132
133
134
135
# File 'lib/writeexcel/olewriter.rb', line 131

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.



145
146
147
148
149
150
151
152
# File 'lib/writeexcel/olewriter.rb', line 145

def close
  if @size_allowed == true
    write_padding          if @biff_only == 0
    write_property_storage if @biff_only == 0
    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


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/writeexcel/olewriter.rb', line 109

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.



95
96
97
# File 'lib/writeexcel/olewriter.rb', line 95

def write(data)
  @io.write(data)
end

#write_big_block_depotObject

_write_big_block_depot()

Write big block depot.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/writeexcel/olewriter.rb', line 208

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.



160
161
162
163
164
165
166
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
# File 'lib/writeexcel/olewriter.rb', line 160

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



302
303
304
305
306
307
308
309
310
# File 'lib/writeexcel/olewriter.rb', line 302

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



252
253
254
255
256
257
258
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
# File 'lib/writeexcel/olewriter.rb', line 252

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.bytesize * 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)
  write(zero * (64 - length)) if 64 - length >= 1
  write(pps_sizeofname)
  write(pps_type)
  write(pps_prev)
  write(pps_next)
  write(pps_dir)
  write(unknown * 5)
  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



237
238
239
240
241
242
243
244
# File 'lib/writeexcel/olewriter.rb', line 237

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