Class: OLEWriter

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

Constant Summary collapse

MaxSize =

Not meant for public consumption

7087104
BlockSize =
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)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/spreadsheet/olewriter.rb', line 20

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

   @biff_only     = false
   @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_sizeObject (readonly)

Returns the value of attribute biff_size.



16
17
18
# File 'lib/spreadsheet/olewriter.rb', line 16

def biff_size
  @biff_size
end

#big_blocksObject (readonly)

Returns the value of attribute big_blocks.



16
17
18
# File 'lib/spreadsheet/olewriter.rb', line 16

def big_blocks
  @big_blocks
end

#book_sizeObject (readonly)

Returns the value of attribute book_size.



16
17
18
# File 'lib/spreadsheet/olewriter.rb', line 16

def book_size
  @book_size
end

#list_blocksObject (readonly)

Returns the value of attribute list_blocks.



16
17
18
# File 'lib/spreadsheet/olewriter.rb', line 16

def list_blocks
  @list_blocks
end

#root_startObject (readonly)

Returns the value of attribute root_start.



17
18
19
# File 'lib/spreadsheet/olewriter.rb', line 17

def root_start
  @root_start
end

#size_allowedObject (readonly)

Returns the value of attribute size_allowed.



17
18
19
# File 'lib/spreadsheet/olewriter.rb', line 17

def size_allowed
  @size_allowed
end

Class Method Details

.open(arg) ⇒ Object

Imitate IO.open behavior



39
40
41
42
43
44
45
46
47
48
# File 'lib/spreadsheet/olewriter.rb', line 39

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

#calculate_sizesObject

Calculate various sizes needed for the OLE stream



79
80
81
82
83
# File 'lib/spreadsheet/olewriter.rb', line 79

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

Write root entry, big block list and close the filehandle.



86
87
88
89
90
91
92
93
# File 'lib/spreadsheet/olewriter.rb', line 86

def close
   if @size_allowed == true
      write_padding
      write_property_storage
      write_big_block_depot
   end
   @io.close
end


54
55
56
# File 'lib/spreadsheet/olewriter.rb', line 54

def print(*args, &block)
  @io.print(*args, &block)
end

#set_size(size = BlockSize) ⇒ Object

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

  • (1 x end words)) = 13842

MaxSize = @big_blocks * 512 bytes = 7087104

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spreadsheet/olewriter.rb', line 64

def set_size(size = BlockSize)
   raise MaxSizeError if size > MaxSize

   @biff_size = size

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

   @size_allowed = true
end

#write(*args, &block) ⇒ Object

Delegate ‘write’ and ‘print’ to the internal IO object.



51
52
53
# File 'lib/spreadsheet/olewriter.rb', line 51

def write(*args, &block)
  @io.write(*args, &block)
end

#write_big_block_depotObject

Write a big block depot



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/spreadsheet/olewriter.rb', line 122

def write_big_block_depot
   total_blocks = @list_blocks * 128
   used_blocks  = @big_blocks + @list_blocks + 2
   
   marker = [-3].pack("V")
   eoc    = [-2].pack("V")
   unused = [-1].pack("V")

   num_blocks = @big_blocks - 1

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

   write eoc
   write eoc

   1.upto(@list_blocks){ write(marker) }

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

#write_headerObject

Write the OLE header block



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/spreadsheet/olewriter.rb', line 96

def write_header
   return if @biff_only == true
   calculate_sizes
   root_start = @root_start

   write([0xD0CF11E0, 0xA1B11AE1].pack("NN"))
   write([0x00, 0x00, 0x00, 0x00].pack("VVVV"))
   write([0x3E, 0x03, -2, 0x09].pack("vvvv"))
   write([0x06, 0x00, 0x00].pack("VVV"))
   write([@list_blocks, root_start].pack("VV"))
   write([0x00, 0x1000,-2].pack("VVV"))
   write([0x00, -2 ,0x00].pack("VVV"))

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

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

   @list_blocks.upto(108){
      write(unused)
   }
end

#write_paddingObject

Pad the end of the file



187
188
189
190
191
192
193
194
195
# File 'lib/spreadsheet/olewriter.rb', line 187

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 property sheet in property storage



154
155
156
157
158
159
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
# File 'lib/spreadsheet/olewriter.rb', line 154

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

   zero = [0].pack("C")
   unknown = [0].pack("V")
   
   write(ord_name.pack("v*"))

   for n in 1..64-length
      write(zero)
   end

   write([length,type,-1,-1,dir].pack("vvVVV"))

   for n in 1..5
      write(unknown)
   end
   
   for n in 1..4
      write([0].pack("V"))
   end

   write([start,size].pack("VV"))
   write(unknown)
end

#write_property_storageObject

Write property storage



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

def write_property_storage
   write_pps('Root Entry', 0x05,  1,   -2, 0x00)
   write_pps('Book',       0x02, -1, 0x00, @book_size)
   write_pps("",           0x00, -1, 0x00, 0x0000)
   write_pps("",           0x00, -1, 0x00, 0x0000)
end