Adam Griffiths
Adam Griffiths

Reputation: 782

Construct string to paste into multiple cells in google sheets

I am creating a Desktop app that contains my own grids of data, I want users to be able to easily paste from my grid into a google sheet. How can I construct a string that when put into the clipboard, can be pasted directly into Google Sheets to enter data into multiple cells?

For example I have this CSV string as a variable in Python:

1,2,3,4

If I paste that string into google sheets it gets pasted as 1 cell with the value 1,2,3,4, where I want it pasted as 4 cells in the same row.

Upvotes: 3

Views: 1166

Answers (2)

Adam Griffiths
Adam Griffiths

Reputation: 782

As @doubleunary suggested (and I had already tried), tabs or '\t' in python are the answer.

However, I made the mistake of outputting the line to copy to a text box and allowing the user to highlight it and copy/paste themselves. The text box automatically converted the tabs to spaces, breaking the copying and pasting.

The solution was to insert the string to be copied directly into the clipboard using pyperclip:

import pyperclip
pyperclip.copy('1\t2\t3\t4')

Upvotes: 1

doubleunary
doubleunary

Reputation: 19185

Use tab (ASCII 9) as horizontal separator and newline (ASCII 10) as line separator.

Upvotes: 0

Related Questions