Class: Workbook

Inherits:
Object
  • Object
show all
Defined in:
lib/surpass/workbook.rb

Constant Summary collapse

MACROS =
{
    'Consolidate_Area' => 0x00,
    'Auto_Open' => 0x01,
    'Auto_Close' => 0x02,
    'Extract' => 0x03,
    'Database' => 0x04,
    'Criteria' => 0x05,
    'Print_Area' => 0x06,
    'Print_Titles' => 0x07, # in the docs it says Pint_Titles, I think its a mistake
    'Recorder' => 0x08,
    'Data_Form' => 0x09,
    'Auto_Activate' => 0x0A,
    'Auto_Deactivate' => 0x0B,
    'Sheet_Title' => 0x0C,
    '_FilterDatabase' => 0x0D
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = nil) ⇒ Workbook

Returns a new instance of Workbook.



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
89
90
91
92
93
94
95
# File 'lib/surpass/workbook.rb', line 60

def initialize(filename = nil)
  @owner = 'None'
  @wnd_protect = 0
  @obj_protect = 0
  @protect = 0
  @backup_on_save = 0

  @hpos_twips = 0x01E0
  @vpos_twips = 0x005A
  @width_twips = 0x3FCF
  @height_twips = 0x2A4E

  @active_sheet = 0
  @first_tab_index = 0
  @selected_tabs = 0x01
  @tab_width_twips = 0x0258

  @wnd_hidden = false
  @wnd_mini = false
  @hscroll_visible = true
  @vscroll_visible = true
  @tabs_visible = true

  @styles = ::StyleCollection.new

  @dates_1904 = false
  @use_cell_values = true

  @sst = SharedStringTable.new

  @worksheets = []
  @names = []
  @refs = []
  
  @filename = filename
end

Instance Attribute Details

#active_sheetObject

Returns the value of attribute active_sheet.



47
48
49
# File 'lib/surpass/workbook.rb', line 47

def active_sheet
  @active_sheet
end

#backup_on_saveObject

Returns the value of attribute backup_on_save.



24
25
26
# File 'lib/surpass/workbook.rb', line 24

def backup_on_save
  @backup_on_save
end

#country_codeObject

Returns the value of attribute country_code.



20
21
22
# File 'lib/surpass/workbook.rb', line 20

def country_code
  @country_code
end

#default_styleObject (readonly)

Returns the value of attribute default_style.



58
59
60
# File 'lib/surpass/workbook.rb', line 58

def default_style
  @default_style
end

#height_twipsObject

Returns the value of attribute height_twips.



42
43
44
# File 'lib/surpass/workbook.rb', line 42

def height_twips
  @height_twips
end

#obj_protectObject

Returns the value of attribute obj_protect.



22
23
24
# File 'lib/surpass/workbook.rb', line 22

def obj_protect
  @obj_protect
end

#ownerObject

Returns the value of attribute owner.



19
20
21
# File 'lib/surpass/workbook.rb', line 19

def owner
  @owner
end

#protectObject

Returns the value of attribute protect.



23
24
25
# File 'lib/surpass/workbook.rb', line 23

def protect
  @protect
end

#sstObject

Returns the value of attribute sst.



26
27
28
# File 'lib/surpass/workbook.rb', line 26

def sst
  @sst
end

#stylesObject

Returns the value of attribute styles.



25
26
27
# File 'lib/surpass/workbook.rb', line 25

def styles
  @styles
end

#tab_width_twipsObject

Returns the value of attribute tab_width_twips.



53
54
55
# File 'lib/surpass/workbook.rb', line 53

def tab_width_twips
  @tab_width_twips
end

#vpos_twipsObject

Returns the value of attribute vpos_twips.



32
33
34
# File 'lib/surpass/workbook.rb', line 32

def vpos_twips
  @vpos_twips
end

#width_twipsObject

Returns the value of attribute width_twips.



37
38
39
# File 'lib/surpass/workbook.rb', line 37

def width_twips
  @width_twips
end

#wnd_protectObject

Returns the value of attribute wnd_protect.



21
22
23
# File 'lib/surpass/workbook.rb', line 21

def wnd_protect
  @wnd_protect
end

Instance Method Details

#add_sheet(name = nil) ⇒ Object



97
98
99
100
101
102
# File 'lib/surpass/workbook.rb', line 97

def add_sheet(name = nil)
  name ||= "Sheet#{@worksheets.length + 1}"
  s = Worksheet.new(name, self)
  @worksheets << s
  s
end

#dataObject



196
197
198
199
# File 'lib/surpass/workbook.rb', line 196

def data
  doc = ExcelDocument.new
  doc.data(to_biff).read
end

#hpos_twips=(value) ⇒ Object



28
29
30
# File 'lib/surpass/workbook.rb', line 28

def hpos_twips=(value)
  @hpos_twips = value & 0xFFFF
end


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/surpass/workbook.rb', line 104

def print_area(sheetnum, rstart, rend, cstart, cend)
  if !sheetnum.is_a?(Integer)
    i = 0
    @worksheets.each_with_index do |w, i|
      sheetnum = i+1 if w.name === sheetnum
      break if sheetnum.is_a?(Integer)
    end
  end
  
  options = 0x0020 # see Options Flags for Name record
   
  # FIXME: this is just a bad hack, need to use Formula to make the rpn
  #~ rpn = Formula.Formula('').rpn()[2:] # minus the size field
  rpn = [0x3B, 0x0000, rstart, rend, cstart, cend].pack('Cv5')
  args = [options, 0x00, MACROS['Print_Area'], sheetnum, rpn]
  @names << NameRecord.new(*args).to_biff
end

#save(filename = nil) ⇒ Object



201
202
203
204
205
# File 'lib/surpass/workbook.rb', line 201

def save(filename = nil)
  @filename = filename unless filename.nil?
  doc = ExcelDocument.new
  doc.save(@filename, to_biff)
end

#to_biffObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
# File 'lib/surpass/workbook.rb', line 122

def to_biff
  raise "You cannot save a workbook with no worksheets" if @worksheets.empty?
  
  section_1_array = []
  section_1_array << Biff8BOFRecord.new(Biff8BOFRecord::BOOK_GLOBAL).to_biff
  section_1_array << InterfaceHeaderRecord.new.to_biff
  section_1_array << MMSRecord.new.to_biff
  section_1_array << InterfaceEndRecord.new.to_biff
  section_1_array << WriteAccessRecord.new(owner).to_biff
  section_1_array << CodepageBiff8Record.new.to_biff
  section_1_array << DSFRecord.new.to_biff
  section_1_array << TabIDRecord.new(@worksheets.length).to_biff
  section_1_array << FnGroupCountRecord.new.to_biff
  section_1_array << WindowProtectRecord.new(as_numeric(@wnd_protect)).to_biff
  section_1_array << ProtectRecord.new(as_numeric(@protect)).to_biff
  section_1_array << ObjectProtectRecord.new(as_numeric(@obj_protect)).to_biff
  section_1_array << PasswordRecord.new.to_biff
  section_1_array << Prot4RevRecord.new.to_biff
  section_1_array << Prot4RevPassRecord.new.to_biff
  section_1_array << BackupRecord.new(@backup_on_save).to_biff
  section_1_array << HideObjRecord.new.to_biff
  section_1_array << window_1_record
  section_1_array << DateModeRecord.new(@dates_1904).to_biff
  section_1_array << PrecisionRecord.new(@use_cell_values).to_biff
  section_1_array << RefreshAllRecord.new.to_biff
  section_1_array << BookBoolRecord.new.to_biff
  section_1_array << @styles.to_biff
  section_1_array << '' # Palette
  section_1_array << UseSelfsRecord.new.to_biff
  section_1 = section_1_array.join

  section_3_array = []
  section_3_array << CountryRecord.new(@country_code, @country_code).to_biff unless @country_code.nil?
  # section_3_array << InternalReferenceSupBookRecord.new(@worksheets.length).to_biff
  # section_3_array << ExternSheetRecord.new(@refs).to_biff
  # section_3_array << @names.collect {|n| n.to_biff}.join
  section_3_array << @sst.to_biff
  section_3 = section_3_array.join
  
  section_4 = '' # ExtSSTRecord
  section_5 = EOFRecord.new.to_biff
  
  @worksheets[@active_sheet].selected = true
  worksheet_biff_data = @worksheets.collect {|w| w.to_biff }
  worksheet_biff_data_lengths = worksheet_biff_data.collect {|w| w.length }
  section_6 = worksheet_biff_data.join
  
  # Need to know how long the bound sheet records will be
  boundsheet_data_lengths = @worksheets.collect {|w| BoundSheetRecord.new(0x00, w.visibility, w.name).to_biff.length }
  total_boundsheet_data_length = boundsheet_data_lengths.inject(0) {|sum, l| sum + l}
  start_position = section_1.length + total_boundsheet_data_length + section_3.length + section_4.length + section_5.length
  
  boundsheet_records = []
  @worksheets.each_with_index do |w, i|
    boundsheet_records << BoundSheetRecord.new(start_position, w.visibility, w.name).to_biff
    start_position += worksheet_biff_data_lengths[i]
  end
  
  section_2 = boundsheet_records.join
  section_1 + section_2 + section_3 + section_4 + section_5 + section_6
end

#window_1_recordObject



184
185
186
187
188
189
190
191
192
193
194
# File 'lib/surpass/workbook.rb', line 184

def window_1_record
  flags = 0
  flags |= (as_numeric(@wnd_hidden)) << 0
  flags |= (as_numeric(@wnd_mini)) << 1
  flags |= (as_numeric(@hscroll_visible)) << 3
  flags |= (as_numeric(@vscroll_visible)) << 4
  flags |= (as_numeric(@tabs_visible)) << 5
  
  args = [@hpos_twips, @vpos_twips, @width_twips, @height_twips, flags, @active_sheet, @first_tab_index, @selected_tabs, @tab_width_twips]
  Window1Record.new(*args).to_biff
end