Skip to content

Commit

Permalink
adding cleanup methods for cleaning strings and numbers, remove $ and…
Browse files Browse the repository at this point in the history
… , from numbers and convert & into & for strings. Excel does not

like those characters
  • Loading branch information
Michael Alletto committed Apr 26, 2011
1 parent 0b29257 commit 1533876
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions lib/simple_xlsx/sheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def add_row arry, header = false
end
arry.each_with_index do |data_hash, col_ndx|
if header
ccontent = "<is><t>#{data_hash[:value]}</t></is>"
ccontent = "<is><t>#{Sheet.clean_string(data_hash[:value])}</t></is>"
else
kind, ccontent, cstyle = Sheet.format_field_and_type_and_style data_hash
end
Expand All @@ -86,10 +86,10 @@ def self.format_field_and_type_and_style data_hash
if data_hash[:value].blank?
[:inlineStr, "", 3]
else
[:inlineStr, "<is><t>#{data_hash[:value]}</t></is>", 3]
[:inlineStr, "<is><t>#{Sheet.clean_string(data_hash[:value])}</t></is>", 3]
end
elsif data_hash[:type] == "Number"
[:n, "<v>#{data_hash[:value]}</v>", 6]
[:n, "<v>#{Sheet.clean_number(data_hash[:value])}</v>", 6]
elsif data_hash[:type] == "DateTime"
if data_hash[:value].blank?
[:inlineStr, "", 3]
Expand All @@ -102,12 +102,10 @@ def self.format_field_and_type_and_style data_hash
if data_hash[:value].blank?
[:n, "<v>#{data_hash[:value]}</v>", 2]
else
data_hash[:value].gsub!(/\$/, '')
data_hash[:value].gsub!(/\,/, '')
[:n, "<v>#{data_hash[:value]}</v>", 2]
[:n, "<v>#{Sheet.clean_number(data_hash[:value])}</v>", 2]
end
else
[:inlineStr, "<is><t>#{data_hash[:value]}</t></is>", 3]
[:inlineStr, "<is><t>#{Sheet.clean_string(data_hash[:value])}</t></is>", 3]
end
end

Expand Down Expand Up @@ -135,6 +133,16 @@ def self.column_index n
result << abc[result.empty? ? n : n - 1]
result.reverse.join
end

# use this to sub out values that excel doesn't like, for example & changing to &amp;
def self.clean_string(value)
value.gsub(/\&/, '&amp;')
end

def self.clean_number(value)
value.gsub!(/\$/, '')
value.gsub(/\,/, '')
end

end
end
2 changes: 1 addition & 1 deletion simple_xlsx_writer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ $LOAD_PATH.unshift 'lib'

Gem::Specification.new do |s|
s.name = "mumboe-simple_xlsx_writer"
s.version = '0.5.7'
s.version = '0.5.8'
s.platform = Gem::Platform::RUBY
s.authors = ["Dee Zsombor", "Justin Beck", "Michael Alletto"]
s.email = ["dbortz@gmail.com", "mikea@mumboe.com"]
Expand Down

0 comments on commit 1533876

Please sign in to comment.