Google Sheets – Reformatting Text Strings with Google Apps Script

google sheetsgoogle-apps-script

I have a spreadsheet in Google Sheets; one column tracks hashtags that were used in blog posts on a website. The posts are written on the website, and then the hash tags are copied from the website and pasted onto the sheet.

The problem is that the hash tags are formatted on the website in a strange way (they cannot be input as plain text because the designer hated making things easy), so when they are copied to the clipboard, they have no spaces like this:

#sex#drugs#rocknroll

However, I need to format them with a space in front of the #:

#sex #drugs #rocknroll

Right now I'm manually inserting the spaces, but sometimes there are 20 hash tags per post, so that will be painful long-term. Is there a way to automatically insert the space in Google sheets, or using Apps Script?

Best Answer

It seems what you want is a very simple Replace # with # (hash with space hash).

If to skip a space at the start an alternative is the following formula:

=trim(substitute(A1,"#"," #"))

An alternative for skipping a space at the start, without an additional column, is to use a regular expression in Find/Replace:

Find: (.)(#)
Replace: $1 $2