I have a google sheet with data, but it has one problem daily I have to put data(same type of data), someone knows how to append data in a google sheet using python, help me.
I have that type of result and that is in string
print(time, " ", todayMaxProfit, " ", todayMaxLoss, " ", pl, " ", len(
orderList), " First pair sum:- ", int(orderList[0][4]+orderList[1][4]))
"2021-08-18 15:00:00 [1451, '2021-08-18 11:07:00'] [-10203, '2021-08-18 14:45:00'] -6900 2 First pair sum:- 234"
I want to append data at last.
Follow the quickstart to get set up. Make sure you follow all the steps exactly! You will need to do this for every project you start that uses the API so you might as well follow the instructions here. Make sure you get the expected output before moving on!
You can then modify the quickstart to make getting the service
a separate function:
def getService():
creds = None
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.json', 'w') as token:
token.write(creds.to_json())
return build('sheets', 'v4', credentials=creds)
service = getService()
appendValues(service)
values = [
[time, todayMaxProfit, todayMaxLoss, pl, len(orderList), int(orderList[0][4]+orderList[1][4])]
]
body = {'values': values}
result = service.spreadsheets().values().append(
spreadsheetId="13rdolwpUD4h4RTuEgn1QbtgPMpJiZGMFubdh4loAfNQ", range="Sheet1!A1",
valueInputOption="RAW", body=body).execute()
Note that the format the values
has to be in is a two dimensional list.:
[
[A1, B1, C1],
[A2, B2, C2]
]
Using the append
method, will simply add the row, as-is to the end of the sheet. The append
takes a few arguments:
id
of the spreadsheet that you want to append values toA1
to the bottom, you can just leave this as A1
, or maybe C5
, if you have headers or spaces. The idea is to point the API to the collection of data that you want to append to.append
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With