diff --git a/lib/roo/csv.rb b/lib/roo/csv.rb index cc44cc59..04a638ca 100644 --- a/lib/roo/csv.rb +++ b/lib/roo/csv.rb @@ -107,4 +107,14 @@ def read_cells(sheet = default_sheet) @last_column[sheet] -= 1 end end + + def clean_sheet(sheet) + read_cells(sheet) + + @cell.each_pair do |coord, value| + @cell[coord] = sanitize_value(value) if value.is_a?(::String) + end + + @cleaned[sheet] = true + end end diff --git a/lib/roo/excelx.rb b/lib/roo/excelx.rb index 82325e94..51630f18 100644 --- a/lib/roo/excelx.rb +++ b/lib/roo/excelx.rb @@ -487,6 +487,14 @@ def each_row_streaming(options={}) private + def clean_sheet(sheet) + @sheets_by_name[sheet].cells.each_pair do |coord, value| + @sheets_by_name[sheet].cells[coord] = sanitize_value(value) if value.is_a?(::String) + end + + @cleaned[sheet] = true + end + # Extracts all needed files from the zip file def process_zipfile(tmpdir, zipfilename) @sheet_files = [] diff --git a/spec/lib/roo/csv_spec.rb b/spec/lib/roo/csv_spec.rb index c9d3ad58..4d9f6c49 100644 --- a/spec/lib/roo/csv_spec.rb +++ b/spec/lib/roo/csv_spec.rb @@ -23,6 +23,20 @@ end end + describe '#parse_with_clean_option' do + subject do + csv.parse(options) + end + context 'with clean: true' do + let(:options) { {clean: true} } + let(:path) { 'test/files/parse_with_clean_option.csv' } + + it "doesn't blow up" do + expect { subject }.to_not raise_error + end + end + end + describe '#csv_options' do context 'when created with the csv_options option' do let(:options) do diff --git a/spec/lib/roo/excelx_spec.rb b/spec/lib/roo/excelx_spec.rb index 800d181f..8523c348 100644 --- a/spec/lib/roo/excelx_spec.rb +++ b/spec/lib/roo/excelx_spec.rb @@ -71,6 +71,20 @@ end end + describe '#parse_with_clean_option' do + let(:path) { 'test/files/parse_with_clean_option.xlsx' } + let(:options) { {clean: true} } + + context 'with clean: true' do + + it 'does not raise' do + expect do + xlsx.parse(options) + end.not_to raise_error + end + end + end + describe '#sheets' do let(:path) { 'test/files/numbers1.xlsx' } diff --git a/test/files/parse_with_clean_option.csv b/test/files/parse_with_clean_option.csv new file mode 100644 index 00000000..80058f7a --- /dev/null +++ b/test/files/parse_with_clean_option.csv @@ -0,0 +1 @@ +name Robert Eshleman \ No newline at end of file diff --git a/test/files/parse_with_clean_option.xlsx b/test/files/parse_with_clean_option.xlsx new file mode 100644 index 00000000..8ccd5afb Binary files /dev/null and b/test/files/parse_with_clean_option.xlsx differ