diff --git a/lib/phlex/csv.rb b/lib/phlex/csv.rb index 41ccafcf..22f1363a 100644 --- a/lib/phlex/csv.rb +++ b/lib/phlex/csv.rb @@ -82,9 +82,13 @@ def render(renderable) end def escape(value) - value = value.to_s + value = value.to_s.dup + value.strip! - if value.include?('"') || value.include?(",") || value.include?("\n") + if value.start_with?("=") || value.start_with?("+") || value.start_with?("-") || value.start_with?("@") + # Prefix a tab to prevent Excel and Google Docs from interpreting the value as a formula + %("\t#{value.gsub('"', '""')}") + elsif value.include?('"') || value.include?(",") || value.include?("\n") %("#{value.gsub('"', '""')}") else value