Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aschyiel/6c60878f28a743a993d0fba06fdde39c to your computer and use it in GitHub Desktop.
Save aschyiel/6c60878f28a743a993d0fba06fdde39c to your computer and use it in GitHub Desktop.
How to insert newlines into strings when working with excel in ruby (Focus on Science, Morty!)
POOR_MANS_LINE_BREAK = '__br__'
# How to insert newlines into strings when working with excel in ruby.
#
# NOTE: There are probably better ways of doing this, but this works for me. -uly, oct 2017
#
# See https://stackoverflow.com/questions/14856501/substitute-a-comma-with-a-break-link-in-a-cell
# GOTCHA: Careful to guard against blank strings; breaks excel and will result in a "repair".
def insert_newlines_into_excel_value_if_any(s)
s = "=SUBSTITUTE(\"#{ s.gsub(/\n/, POOR_MANS_LINE_BREAK) }\", \"#{ POOR_MANS_LINE_BREAK }\", CHAR(13))" if s =~ /\n/
s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment