From 584334cdda376c341c9b620114a2d35a974ed47a Mon Sep 17 00:00:00 2001 From: Pedro Axelrud Date: Wed, 8 Jan 2014 12:29:50 -0200 Subject: [PATCH] Exporting link url when converting spreadsheet to csv If the celltype was a link, the behaviour was raising an unhandled celltype exception. I believe the best solution for this case (at least for my use case) is exporting the url of the link. --- lib/roo/base.rb | 2 ++ test/files/link.csv | 1 + test/test_roo.rb | 14 +++++++++++++- 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 test/files/link.csv diff --git a/lib/roo/base.rb b/lib/roo/base.rb index 554dd136..86ed2322 100644 --- a/lib/roo/base.rb +++ b/lib/roo/base.rb @@ -778,6 +778,8 @@ def cell_to_csv(row, col, sheet) onecell.to_s when :time Roo::Base.integer_to_timestring(onecell) + when :link + %{"#{onecell.url.gsub(/"/,'""')}"} else raise "unhandled celltype #{celltype(row,col,sheet)}" end || "" diff --git a/test/files/link.csv b/test/files/link.csv new file mode 100644 index 00000000..344f52d7 --- /dev/null +++ b/test/files/link.csv @@ -0,0 +1 @@ +"http://www.google.com" diff --git a/test/test_roo.rb b/test/test_roo.rb index 3bfdfde8..6fe57e9c 100644 --- a/test/test_roo.rb +++ b/test/test_roo.rb @@ -1113,7 +1113,19 @@ def test_boolean_to_csv end end end - + def test_link_to_csv + with_each_spreadsheet(:name=>'link',:format=>:excel) do |oo| + Dir.mktmpdir do |tempdir| + csv_output = File.join(tempdir,'link.csv') + assert oo.to_csv(csv_output) + assert File.exists?(csv_output) +puts `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}` + assert_equal "", `diff --strip-trailing-cr #{TESTDIR}/link.csv #{csv_output}` + # --strip-trailing-cr is needed because the test-file use 0A and + # the test on an windows box generates 0D 0A as line endings + end + end + end def test_date_time_yaml with_each_spreadsheet(:name=>'time-test') do |oo| expected =