I'm writing a python script to send data from a csv file to a Google Spreadsheet. I'm have a clear understanding of the basics need for this project, however I'm totally lost regarding formatting/parsing data from a csv file to the google sheet (I'm still learning python !).
So here is the first part of my code :
def add_todo():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)
spreadsheetId = 'spreadsheetidhere'
rangeName = 'A1:A'
# https://developers.google.com/sheets/guides/values#appending_values
values = {'values':[['Hello Saturn',],]}
result = service.spreadsheets().values().append(
spreadsheetId=spreadsheetId, range=rangeName,
valueInputOption='RAW',
body=values).execute()
if __name__ == '__main__':
add_todo()
I didn't write it myself and it's working perfectly.
Here is the second part of my code :
test = []
with open('data.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
test.append(row)
It's also working properly, even though I don't really know how I should mix it with the first part of my code. I've done this:
test = []
def add_todo():
with open('snap.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
test.append(row)
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
'version=v4')
service = discovery.build('sheets', 'v4', http=http,
discoveryServiceUrl=discoveryUrl)
spreadsheetId = '1UJGs-gJCIUDv5jg5-p0MPEqsokYu5k2MfJ8A5oBkpWs'
rangeName = 'A1:A'
# https://developers.google.com/sheets/guides/values#appending_values
values = {'values':[['Hello Saturn',],]}
result = service.spreadsheets().values().append(
spreadsheetId=spreadsheetId, range=rangeName,
valueInputOption='RAW',
body=values).execute()
if __name__ == '__main__':
add_todo()
As you can guess my main concern is how I should proceed to pass the data from the test array to the spreadsheet values values = {'values':[['Hello Saturn',],]}
I wanted to do something like this : values = {'values':[test]} but obviously it doesn't work.
To sum up, here is my csv file :
data1,data2,data3,data4,...
value1x,value2x,value3x,value4x,...
value1y,value2y,value3y,value4y,...
value1z,value2z,value3z,value4z,...
And here is what I want in my spreadsheet :
data1 data2 data3 data4 //col name
value1x value2x value3x value4x //row1
value1y value2y value3y value4y //row2
etc...
I'm totally lost between array, list, json format.