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.



166
167
168
169
170
171
172
# File 'lib/dullard/reader.rb', line 166

def initialize(workbook, name, id, index)
  @workbook = workbook
  @name = name
  @id = id
  @index = index
  @file = @workbook.zipfs.file.open(path) if @workbook.zipfs.file.exist?(path)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



165
166
167
# File 'lib/dullard/reader.rb', line 165

def name
  @name
end

#workbookObject (readonly)

Returns the value of attribute workbook.



165
166
167
# File 'lib/dullard/reader.rb', line 165

def workbook
  @workbook
end

Class Method Details

.column_namesObject

Returns A to ZZZ.



241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/dullard/reader.rb', line 241

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

#row_countObject



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/dullard/reader.rb', line 255

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



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
208
209
210
211
212
213
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
# File 'lib/dullard/reader.rb', line 178

def rows
  Enumerator.new(row_count) do |y|
    next unless @file
    @file.rewind
    shared = false
    row = nil
    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"
          row = []
          column = 0
          next
        when "c"
          if node.attributes['t'] != 's' && node.attributes['t'] != 'b'
            cell_format_index = node.attributes['s'].to_i
            cell_type = @workbook.format2type(@workbook.attribute2format(cell_format_index))
          end

          rcolumn = node.attributes["r"]
          if rcolumn
            rcolumn.delete!("0-9")
            while column < self.class.column_names.size and rcolumn != self.class.column_names[column]
              row << nil
              column += 1
            end
          end
          shared = (node.attribute("t") == "s")
          column += 1
          next
        end
      when Nokogiri::XML::Reader::TYPE_END_ELEMENT
        if node.name == "row"
          y << row
          next
        end
      end
      value = node.value

      if value
        case cell_type
          when :datetime
          when :time
          when :date
            value = (DateTime.new(1899,12,30) + value.to_f)
          when :percentage # ? TODO
          when :float
            value = value.to_f
          else
            # leave as string
        end
        cell_type = nil

        row << (shared ? string_lookup(value.to_i) : value)
      end
    end
  end
end

#string_lookup(i) ⇒ Object



174
175
176
# File 'lib/dullard/reader.rb', line 174

def string_lookup(i)
  @workbook.string_table[i]
end