Search code examples
google-sheetsclipboardcopy-paste

How to format multiline text for easy pasting into a Google Spreadsheet?


I have multiline text like this:

- foo
- "bar"
- baz\n
- 'hello world'

I am copying this from ag-grid spreadsheet cells (it shouldn't matter that I'm using ag-grid for this question, but just know that I need to figure out how to format the text I get from that cell), which looks like this:

enter image description here

I tried formatting the text from that right column to be like this:

const copyText = (value?: string): string => {
  return `"${(value ?? '').replace(/"/gm, '\\"')}"`;
};

I get this when pasting it in the Google Spreadsheet:

enter image description here

How do I need to format the text so I can paste the multiple lines into a single spreadsheet cell? I added \n and " and ' characters in the multiline to just show that the multiline can contain any text basically, so it shouldn't mess up if they include quotes and backslashes and such. I want this basically:

enter image description here

When I copy from the ag-grid, the clipboard pastes this essentially:

L1\t"- foo
- \"bar\"
- baz\n
- 'hello world'
"

So what am I doing wrong in formatting.


Solution

  • You have to use double double quotes:

    L1\t"- foo
    - ""bar""
    - baz\n
    - 'hello world'
    "