Browse files

Merge pull request #102 from niclasnilsson/master

Add capability to specify separator to to_csv
  • Loading branch information...
2 parents 636541d + f5df7d7 commit 7d403e3aee53a5e80568b865a8f00ec66b99adfb @Empact Empact committed Feb 3, 2014
Showing with 12 additions and 5 deletions.
  1. +5 −5 lib/roo/base.rb
  2. +7 −0 test/test_generic_spreadsheet.rb
View
10 lib/roo/base.rb
@@ -183,16 +183,16 @@ def to_yaml(prefix={}, from_row=nil, from_column=nil, to_row=nil, to_column=nil,
end
# write the current spreadsheet to stdout or into a file
- def to_csv(filename=nil,sheet=nil)
+ def to_csv(filename=nil,sheet=nil,separator=',')
sheet ||= @default_sheet
if filename
File.open(filename,"w") do |file|
- write_csv_content(file,sheet)
+ write_csv_content(file,sheet,separator)
end
return true
else
sio = StringIO.new
- write_csv_content(sio,sheet)
+ write_csv_content(sio,sheet,separator)
sio.rewind
return sio.read
end
@@ -728,12 +728,12 @@ def process_zipfile_packed(zip, tmpdir, path='')
# Write all cells to the csv file. File can be a filename or nil. If the this
# parameter is nil the output goes to STDOUT
- def write_csv_content(file=nil,sheet=nil)
+ def write_csv_content(file=nil,sheet=nil,separator=',')
file ||= STDOUT
if first_row(sheet) # sheet is not empty
1.upto(last_row(sheet)) do |row|
1.upto(last_column(sheet)) do |col|
- file.print(",") if col > 1
+ file.print(separator) if col > 1
file.print cell_to_csv(row,col,sheet)
end
file.print("\n")
View
7 test/test_generic_spreadsheet.rb
@@ -174,6 +174,10 @@ def test_to_yaml
def test_to_csv
assert_equal expected_csv,@oo.to_csv
end
+
+ def test_to_csv_with_separator
+ assert_equal expected_csv_with_semicolons,@oo.to_csv(nil, nil, ';')
+ end
protected
def setup_test_sheet(workbook=nil)
workbook ||= @oo
@@ -256,4 +260,7 @@ def set_cells_read(workbook)
def expected_csv
",,,,,,\n,,,,,,\n,,,,,,\n,,,,,,\n1961-11-21,,,,,,\n,,,,,,\n,,,,,,\n,,\"thisisc8\",,,,\"thisisg8\"\n,,,,,,\n,,,,,,\n,,,,,,\n41,42,43,44,45,,\n,,,,,,\n,,,,,,\n,,43,44,45,,\n,,\"dreiundvierzig\",\"vierundvierzig\",\"fuenfundvierzig\",,\n"
end
+ def expected_csv_with_semicolons
+ expected_csv.gsub /\,/, ';'
+ end
end

0 comments on commit 7d403e3

Please sign in to comment.