Created
November 7, 2017 20:04
-
-
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!)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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