Class: Dullard::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/dullard/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workbook, name, id, index) ⇒ Sheet

Returns a new instance of Sheet.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/dullard/reader.rb', line 198

def initialize(workbook, name, id, index)
  @workbook = workbook
  @name = name
  @id = id
  @index = index
  begin
    @file = @workbook.zipfs.file.open(path) if @workbook.zipfs.file.exist?(path)
  rescue Zip::Error => e
    raise Dullard::Error, "Couldn't open sheet #{index}: #{e.message}"
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



197
198
199
# File 'lib/dullard/reader.rb', line 197

def name
  @name
end

#workbookObject (readonly)

Returns the value of attribute workbook.



197
198
199
# File 'lib/dullard/reader.rb', line 197

def workbook
  @workbook
end

Class Method Details

.column_name_to_index(name) ⇒ Object



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

def self.column_name_to_index(name)
  if not @column_names_to_indices
    @column_names_to_indices = {}
    self.column_names.each_with_index do |name, i|
      @column_names_to_indices[name] = i
    end
  end
  @column_names_to_indices[name]
end

.column_namesObject

Returns A to ZZZ.



288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/dullard/reader.rb', line 288

def self.column_names
  if @column_names
    @column_names
  else
    proc = Proc.new do |prev|
      ("#{prev}A".."#{prev}Z").to_a
    end
    x = proc.call("")
    y = x.map(&proc).flatten
    z = y.map(&proc).flatten
    @column_names = x + y + z
  end
end

Instance Method Details

#process_row(cell_map) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/dullard/reader.rb', line 272

def process_row(cell_map)
  max = cell_map.keys.map {|c| self.class.column_name_to_index c }.max
  row = []
  self.class.column_names[0..max].each do |col|
    if self.class.column_name_to_index(col) > max
      break
    else
      row << cell_map[col]
    end
  end
  row
end

#row_countObject



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/dullard/reader.rb', line 312

def row_count
  if defined? @row_count
    @row_count
  elsif @file
    @file.rewind
    Nokogiri::XML::Reader(@file).each do |node|
      if node.node_type == Nokogiri::XML::Reader::TYPE_ELEMENT
        case node.name
        when 'dimension'
          if ref = node.attributes["ref"]
            break @row_count = ref.scan(/\d+$/).first.to_i
          end
        when 'sheetData'
          break @row_count = nil
        end
      end
    end
  end
end

#rowsObject



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/dullard/reader.rb', line 214

def rows
  Enumerator.new(row_count) do |y|
    next unless @file
    @file.rewind
    shared = false
    row = nil
    cell_map = nil # Map of column letter to cell value for a row
    column = nil
    cell_type = nil
    Nokogiri::XML::Reader(@file).each do |node|
      case node.node_type
      when Nokogiri::XML::Reader::TYPE_ELEMENT
        case node.name
        when "row"
          cell_map = {}
          next
        when 'c'
          node_type = node.attributes['t']
          cell_index = node.attributes['r']
          if !cell_index
            raise Dullard::Error, 'Invalid spreadsheet XML.'
          end

          if node_type != 's' && node_type != 'b'
            cell_format_index = node.attributes['s'].to_i
            cell_type = @workbook.format2type(@workbook.attribute2format(cell_format_index))
          end

          column = cell_index.delete('0-9')
          shared = (node_type == 's')
          next
        end
      when Nokogiri::XML::Reader::TYPE_END_ELEMENT
        if node.name == 'row'
          y << process_row(cell_map)
        end
        next
      end

      if node.value
        value = (shared ? string_lookup(value.to_i) : value)
        case cell_type
          when :datetime
          when :time
          when :date
            value = (Dullard::OOXMLEpoch + node.value.to_f)
          when :float
            value = node.value.to_f
          else
            # leave as string
        end
        cell_type = nil
        cell_map[column] = value
      end
    end
  end
end

#string_lookup(i) ⇒ Object



210
211
212
# File 'lib/dullard/reader.rb', line 210

def string_lookup(i)
  @workbook.string_table[i] || (raise Dullard::Error, 'File invalid, invalid string table.')
end