Trying to create a spreadsheet in a specific location on google drive using the python API, but the docs on how to do this leave out any concrete examples of what exactly the request body would look like.
Ultimately, have a pandas dataframe with a header and single row of values and trying to create a spreadsheet in google drive that looks like...
------------------
header1 | value1 |
------------------
header2 | value2 |
------------------
header3 | value3 |
------------------
header4 | value4 |
------------------
header5 | value5 |
------------------
where rows 1,2, and 5 are protected.
Some specific questions I have when looking at the docs for this kind of request's JSON body (that have stopped me so far from doing an initial test are) are...
- What can I use as a
spreadsheetId
? What happens with naming conflicts? The docs don't seem to have any info / example on what this is or can be. - How would I know what
spreadsheetUrl
to use? Is there a default? What does this even mean / look like exactly? - Some subsections a the request body seem to want redundant info (eg.
sheetId
). Is this necessary / can I just leave blank? - How can I know / specify where in google drive this sheet is going to be created (I have a particular dir path where I want to create these spreadsheets)?
This is the request body that I have constructed so far (Note, here I'm just trying to get data into the spreadsheet in the conventional table format of row1=header, row2=values)...
df = <some dataframe>
HEADER = list(df.columns)
VALUES = df.values.tolist()[0] # first row of values from dataframe
# building request body for API spreadsheets.create request
request_body = {
"spreadsheetId": VALUES[0], # this is a UUID for the data row in the pandas dataframe
"properties": {
"title": f"{DATANAME}__{VALUES[0]}",
},
"sheets": [
{
"properties": {
"sheetId": 1,
"title": DATANAME
},
"data": [
{
"startRow": 1,
"startColumn": 1,
"rowData": [
{
"values": [
HEADER, # list if values
VALUES # list of values
]
}
],
}
],
"protectedRanges": [
{
"protectedRangeId": integer, # what even is this?
"range": {
{
"sheetId": 1, # do I really need to specify the sheetId again here?
"startRowIndex": 1,
"endRowIndex": 3,
"startColumnIndex": 1,
"endColumnIndex": 3
}
},
"description": "This is a metadata field and should not be edited",
"warningOnly": False,
},
{
"protectedRangeId": integer, # what even is this?
"range": {
{
"sheetId": 1,
"startRowIndex": 5,
"endRowIndex": 6,
"startColumnIndex": 5,
"endColumnIndex": 6
}
},
"description": "This is a metadata field and should not be edited",
"warningOnly": False,
}
]
}
],
"spreadsheetUrl": string,
}
Could anyone with more experience here show me an example of what an actual working request body would look like in this case (and anything wrong with what I've constructed so far)? As well as answer some of the more specific question above?
Are there any other places to see more exmaples / info that the linked-on docs?