I am trying to make a new slide that contains a table with some data in it using the Google Slides API. I'm getting an invalid JSON payload error.
What i've tried to do is create a function that makes the new table request.
def make_table_obj(self, data):
'''make a table object to be added to a slide'''
keys = [key for key in data[0].keys()]
return {
"objectId": gen_id(),
"pageType": "SLIDE",
"pageElements": [
{"elementGroup": {
"table": {
"rows": len(data),
"columns": len(data[0].keys()),
"tableRows": [
[
{
"text": data[i][keys[k]],
"location": {"rowIndex": i, "columnIndex": k}
} for k in range(int(len(keys)))
]
for i in range(int(len(data)))
]}
}}]
}
Here's some sample data to help you help me
[{'_id': 'Customer Service',
'metric1': 239.0,
'metric2': 1875.0},
{'_id': 'Order',
'metric1': 2846.0,
'metric2': 5171.0},
{'_id': 'Checkout',
'metric1': 1789.0,
'metric2': 2446.0}]
The function produces the request that I want(I think), but I am getting this error.
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://slides.googleapis.com/v1/presentations/<presentationId>:batchUpdate?alt=json returned "Invalid value at 'requests[1].create_shape.shape_type' (type.googleapis.com/google.apps.slides.v1.Shape.Type), "Table"
Invalid JSON payload received. Unknown name "rows" at 'requests[1].create_shape.element_properties': Cannot find field.
Invalid JSON payload received. Unknown name "columns" at 'requests[1].create_shape.element_properties': Cannot find field.
Invalid JSON payload received. Unknown name "tableRows" at 'requests[1].create_shape.element_properties': Cannot find field.". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'requests[1].create_shape.shape_type', 'description': 'Invalid value at \'requests[1].create_shape.shape_type\' (type.googleapis.com/google.apps.slides.v1.Shape.Type), "Table"'}, {'field': 'requests[1].create_shape.element_properties', 'description': 'Invalid JSON payload received. Unknown name "rows" at \'requests[1].create_shape.element_properties\': Cannot find field.'}, {'field': 'requests[1].create_shape.element_properties', 'description': 'Invalid JSON payload received. Unknown name "columns" at \'requests[1].create_shape.element_properties\': Cannot find field.'}, {'field': 'requests[1].create_shape.element_properties', 'description': 'Invalid JSON payload received. Unknown name "tableRows" at \'requests[1].create_shape.element_properties\': Cannot find field.'}]}]">
Here's the full request that I am sending.
[
{
"objectId": "id-1617143529-776043",
"pageType": "SLIDE",
"pageElements": [
{
"elementGroup": {
"table": {
"rows": 23,
"columns": 3,
"tableRows": [
[
{
"text": "Customer Service",
"location": {
"rowIndex": 0,
"columnIndex": 0
}
},
{
"text": 239.0,
"location": {
"rowIndex": 0,
"columnIndex": 1
}
},
{
"text": 1875.0,
"location": {
"rowIndex": 0,
"columnIndex": 2
}
}
],
[
{
"text": "Order",
"location": {
"rowIndex": 1,
"columnIndex": 0
}
},
{
"text": 2846.0,
"location": {
"rowIndex": 1,
"columnIndex": 1
}
},
{
"text": 5171.0,
"location": {
"rowIndex": 1,
"columnIndex": 2
}
}
],
[
{
"text": "Checkout",
"location": {
"rowIndex": 2,
"columnIndex": 0
}
},
{
"text": 1789.0,
"location": {
"rowIndex": 2,
"columnIndex": 1
}
},
{
"text": 2446.0,
"location": {
"rowIndex": 2,
"columnIndex": 2
}
}
],
]
}
}
}
]
}
]
Thanks in advance if you can help, I know this question is a bit long.
You should use Table Operations for creating and editing table data.
Example Request Body:
Note: The request below will create a new slide with id id-1617139878-856039
and insert a table with data in it.
{
"requests": [
{
"createSlide": {
"objectId": "id-1617139878-856039",
"insertionIndex": 9,
"slideLayoutReference": {
"predefinedLayout": "TITLE"
}
}
},
{
"createTable": {
"objectId": "123456",
"elementProperties": {
"pageObjectId": "id-1617139878-856039"
},
"rows": 3,
"columns": 3
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 0,
"columnIndex": 0
},
"text": "Customer Service"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 0,
"columnIndex": 1
},
"text": "239.0"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 0,
"columnIndex": 2
},
"text": "1875.0"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 1,
"columnIndex": 0
},
"text": "Order"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 1,
"columnIndex": 1
},
"text": "2846.0"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 1,
"columnIndex": 2
},
"text": "2846.0"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 2,
"columnIndex": 0
},
"text": "Checkout"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 2,
"columnIndex": 1
},
"text": "1789.0"
}
},
{
"insertText": {
"objectId": "123456",
"cellLocation": {
"rowIndex": 2,
"columnIndex": 2
},
"text": "2446.0"
}
}
]
}
Output:
I tested the request here: Google Slide API batchUpdate