Methods

Class/Module Index [+]

Quicksearch

Statsample::Excel

Public Class Methods

read(filename, opts=Hash.new) click to toggle source

Returns a dataset based on a xls file USE:

ds = Statsample::Excel.read("test.xls")
# File lib/statsample/converters.rb, line 185
def read(filename, opts=Hash.new)
  require 'spreadsheet'
  opts_default={
    :worksheet_id=>0, 
    :ignore_lines=>0, 
    :empty=>['']
  }
  
  opts=opts_default.merge opts
  
  worksheet_id=opts[:worksheet_id]
  ignore_lines=opts[:ignore_lines]
  empty=opts[:empty]
  
  first_row=true
  fields=[]
  fields_data={}
  ds=nil
  line_number=0
  book = Spreadsheet.open filename
  sheet= book.worksheet worksheet_id
  sheet.each do |row|
    begin
      dates=[]
      row.formats.each_index{|i|
        if !row.formats[i].nil? and row.formats[i].number_format=="DD/MM/YYYY"
          dates.push(i)
        end
      }
      line_number+=1
      next if(line_number<=ignore_lines)
      
      preprocess_row(row,dates)
      if first_row
        fields=extract_fields(row)
        ds=Statsample::Dataset.new(fields)
        first_row=false
      else
        rowa=process_row(row,empty)
        (fields.size - rowa.size).times {
          rowa << nil
        }
        ds.add_case(rowa,false)
      end
    rescue => e
      error="#{e.to_s}\nError on Line # #{line_number}:#{row.join(",")}"
      raise
    end
  end
  convert_to_scale_and_date(ds, fields)
  ds.update_valid_data
  fields.each {|f|
    ds[f].name=f
  }
  ds.name=filename
  ds
end
write(dataset,filename) click to toggle source

Write a Excel spreadsheet based on a dataset

  • TODO: Format nicely date values

# File lib/statsample/converters.rb, line 144
def write(dataset,filename)
  require 'spreadsheet'
  book = Spreadsheet::Workbook.new
  sheet = book.create_worksheet
  format = Spreadsheet::Format.new :color => :blue,
                     :weight => :bold
  sheet.row(0).concat(dataset.fields.map {|i| i.dup}) # Unfreeze strings
  sheet.row(0).default_format = format
  i=1
  dataset.each_array{|row|
    sheet.row(i).concat(row)
    i+=1
  }
  book.write(filename)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.