Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

allow CSV to accept hash of options for use during csv import

update readme to show example of options
  • Loading branch information...
commit b1b83e5f2494642033cbf269330f80f80dafdf0c 1 parent 0fc7f37
@chrisgogreen chrisgogreen authored
View
18 .project
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>roo</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>com.aptana.ide.core.unifiedBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>com.aptana.ruby.core.rubynature</nature>
+ <nature>com.aptana.projects.webnature</nature>
+ </natures>
+</projectDescription>
View
32 README.markdown
@@ -24,6 +24,7 @@ file but not the formula itself)
s = Google.new("myspreadsheetkey_at_google") # creates an Google Spreadsheet instance
s = Excelx.new("myspreadsheet.xlsx") # creates an Excel Spreadsheet instance for Excel .xlsx files
+
s.default_sheet = s.sheets.first # first sheet in the spreadsheet file will be used
# s.sheet is an array which holds the names of the sheets within
@@ -52,6 +53,37 @@ file but not the formula itself)
s.font(1,1).bold?
s.font(1,1).italic?
s.font(1,1).underline?
+
+
+
+### Setting Options for .csv files
+
+ s = Csv.new("myspreadsheet.xlsx", options={}) # creates a csv Spreadsheet instance for .csv files
+
+ # options should be a hash and can include any of the options used in the documentation at:
+ # http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV.html#method-c-foreach
+ # this means you should be able to specify encoding and all other options.
+
+ # these are some of the default options offered by CSV
+
+ options = { :row_sep => :auto,
+ :quote_char => '"',
+ :field_size_limit => nil,
+ :converters => nil,
+ :unconverted_fields => nil,
+ :headers => false,
+ :return_headers => false,
+ :header_converters => nil,
+ :skip_blanks => false,
+ :force_quotes => false }
+
+
+
+
+
+
+
+
see http://roo.rubyforge.org for a more complete tutorial
View
5 lib/roo.rb
@@ -4,7 +4,8 @@ module Roo
class Spreadsheet
class << self
- def open(file)
+ def open(file, opts={})
+ options = opts
file = File === file ? file.path : file
case File.extname(file)
when '.xls'
@@ -18,7 +19,7 @@ def open(file)
when ''
Roo::Google.new(file)
when '.csv'
- Roo::Csv.new(file)
+ Roo::Csv.new(file, packed=nil, file_warning=:error, tmpdir=nil, options=Hash.new)
else
raise ArgumentError, "Don't know how to open file #{file}"
end
View
5 lib/roo/csv.rb
@@ -9,7 +9,7 @@
# types you have to do it yourself.
class Roo::Csv < Roo::GenericSpreadsheet
- def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil)
+ def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil, options=Hash.new)
@filename = filename
@cell = Hash.new
@cell_type = Hash.new
@@ -18,6 +18,7 @@ def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil)
@last_row = Hash.new
@first_column = Hash.new
@last_column = Hash.new
+ @options = options
end
# Returns an array with the names of the sheets. In Csv class there is only
@@ -66,7 +67,7 @@ def read_cells(sheet=nil)
@first_column[sheet] = 1
@last_column[sheet] = 1
rownum = 1
- CSV.foreach(@filename) do |row|
+ CSV.foreach(@filename, @options) do |row|
row.each_with_index do |elem,i|
@cell[[rownum,i+1]] = cell_postprocessing rownum,i+1, elem
@cell_type[[rownum,i+1]] = celltype_class @cell[[rownum,i+1]]
View
4 lib/roo/generic_spreadsheet.rb
@@ -36,7 +36,7 @@ def self.split_coord(s)
public
- def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil)
+ def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil, options=Hash.new)
@cell = Hash.new{|h,k| h[k] = {}}
@cell_type = Hash.new{|h,k| h[k] = {}}
@cells_read = {}
@@ -53,6 +53,8 @@ def initialize(filename, packed=nil, file_warning=:error, tmpdir=nil)
@default_sheet = self.sheets.first
@formula = {}
@header_line = 1
+
+ @options = options
end
# sets the working sheet in the document
Please sign in to comment.
Something went wrong with that request. Please try again.