Skip to content

Commit

Permalink
Added parser option to skip check on filename
Browse files Browse the repository at this point in the history
  • Loading branch information
pic committed Mar 9, 2012
1 parent 9443ddf commit ff1bf06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
20 changes: 14 additions & 6 deletions lib/rubyXL/parser.rb
Expand Up @@ -40,9 +40,13 @@ def Parser.convert_to_index(cell_string)

# data_only allows only the sheet data to be parsed, so as to speed up parsing
# However, using this option will result in date-formatted cells being interpreted as numbers
def Parser.parse(file_path, data_only=false)
@data_only = data_only
files = Parser.decompress(file_path)
def Parser.parse(file_path, opts = {})

# options handling
@data_only = !!opts[:data_only]
skip_filename_check = !!opts[:skip_filename_check]

files = Parser.decompress(file_path, skip_filename_check)
wb = Parser.fill_workbook(file_path, files)

if(files['sharedString'] != nil)
Expand Down Expand Up @@ -295,14 +299,18 @@ def Parser.fill_worksheet(wb,i,files,shared_strings)
end
end

def Parser.decompress(file_path)
def Parser.decompress(file_path, skip_filename_check = false)
#ensures it is an xlsx/xlsm file
if(file_path =~ /(.+)\.xls(x|m)/)
dir_path = $1.to_s
else
raise 'Not .xlsx or .xlsm excel file'
if skip_filename_check
dir_path = file_path
else
raise 'Not .xlsx or .xlsm excel file'
end
end

dir_path = File.join(File.dirname(dir_path), make_safe_name(Time.now.to_s))
#copies excel file to zip file in same directory
zip_path = dir_path + '.zip'
Expand Down
12 changes: 11 additions & 1 deletion spec/lib/parser_spec.rb
Expand Up @@ -32,8 +32,18 @@
lambda {@workbook2 = RubyXL::Parser.parse(@time_str+".xls")}.should raise_error
end

it 'should not cause an error if an xlsx or xlsm workbook is not passed but the skip_filename_check option is used' do
filename = @time_str
FileUtils.cp(@file, filename)

lambda {@workbook2 = RubyXL::Parser.parse(filename)}.should raise_error
lambda {@workbook2 = RubyXL::Parser.parse(filename, :skip_filename_check => true)}.should_not raise_error

File.delete(filename)
end

it 'should only read the data and not any of the styles (for the sake of speed) when passed true' do
@workbook2 = RubyXL::Parser.parse(@file, true)
@workbook2 = RubyXL::Parser.parse(@file, :data_only => true)

@workbook2.worksheets.size.should == @workbook.worksheets.size
@workbook2[0].sheet_data.should == @workbook[0].sheet_data
Expand Down

0 comments on commit ff1bf06

Please sign in to comment.