I've been using this Google Sheets API code for a few months in Python to append data to a spreadsheet, but it recently broke. Can anyone see what is wrong with this?
body = {'values': [['2019-9-1', '41']]}
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='RAW', body=body, range='A:Z').execute()
It creates cells like this, with an apostrophe before each cell value:
+----+-----------+-----+
| | A | B |
+----+-----------+-----+
| 33 | '2019-9-1 | '41 |
+----+-----------+-----+
This is the result:
{'spreadsheetId': 'xxxxxxxxx', 'tableRange': 'data!A1:U32', 'updates': {'spreadsheetId': 'xxxxxxxxxx', 'updatedRange': 'data!A33:B33', 'updatedRows': 1, 'updatedColumns': 2, 'updatedCells': 2}}
Type the formula =char(039) in any cell. It will return the apostrophe character. To replace ' with an apostrophe character use the Find & Replace as below. Select the range.
2019-9-1
and 41
to the Spreadsheet as the date object and the number, respectively.
'2019-9-1
and '41
.If my understanding is correct, how about this modification?
I think that the reason of your issue is due to valueInputOption='RAW'
. When RAW
is set to valueInputOption
, the official document says as follows.
The values the user has entered will not be parsed and will be stored as-is.
By this, 2019-9-1
and 41
are put as the string type. So '
is added to the top of character.
In order to put the values of 2019-9-1
and 41
as the date object and the number, please modify as follows.
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='RAW', body=body, range='A:Z').execute()
To:
result = service.spreadsheets().values().append(spreadsheetId=SPREADSHEET_ID,valueInputOption='USER_ENTERED', body=body, range='A:Z').execute()
When USER_ENTERED
is set to valueInputOption
, the official document says as follows.
The values will be parsed as if the user typed them into the UI. Numbers will stay as numbers, but strings may be converted to numbers, dates, etc. following the same rules that are applied when entering text into a cell via the Google Sheets UI.
If I misunderstood your question and this was not the result you want, I apologize.
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