使用 google 个幻灯片 API 创建一个 table
create a table using google slides API
我正在尝试使用 Google 幻灯片 API 制作一张包含 table 的新幻灯片,其中包含一些数据。
我遇到无效的 JSON 负载错误。
我尝试做的是创建一个函数来发出新的 table 请求。
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)))
]}
}}]
}
这里有一些示例数据可以帮助你帮助我
[{'_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}]
该函数产生了我想要的请求(我认为),但我收到了这个错误。
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.'}]}]">
这是我要发送的完整请求。
[
{
"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
}
}
],
]
}
}
}
]
}
]
如果您能提供帮助,请提前致谢,我知道这个问题有点长。
您应该使用 Table Operations 创建和编辑 table 数据。
示例请求正文:
注意: 下面的请求将创建一个 ID 为 id-1617139878-856039
的新幻灯片,并在其中插入一个包含数据的 table。
{
"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"
}
}
]
}
输出:
我在这里测试了请求:Google Slide API batchUpdate
在这上面花了很多时间之后...我首先使用 API 创建幻灯片,在我添加 table 的地方开始工作。但是,当我尝试从 Apps 脚本 运行 相同的功能(有效)时,我收到此错误
GoogleJsonResponseException:API 调用 slides.presentations.batchUpdate 失败,出现错误:无效请求[0].createTable:找不到页面 (SLIDES_API1632982083_36)。
function pleaseApiMySlides() {
const pageId = Utilities.getUuid();
const requests = {
"title": "alice also made this"
};
Slides.Presentations.create({
"title": "alice made this"
});
var preso = Slides.Presentations.create(requests)
makeSlides(preso.presentationId);
}
function makeSlides(slidesId){
const preso = Slides.Presentations.get(slidesId);
console.log(preso.title);
const oid = Utilities.getUuid();
const requests = [{
'createSlide': {
'objectId': oid,
'slideLayoutReference': {
'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
}
}
}];
Slides.Presentations.batchUpdate({'requests': requests},slidesId);
makeTables(slidesId,oid);
}
function makeTables(slidesId,slideObj){
console.log(slideObj)
const oid = Utilities.getUuid();
const requests = [{
'createTable':{
"objectId": oid,
"rows": 3,
"columns": 2,
"elementProperties": {
"pageObjectId": slideObj,
}
}
}];
Slides.Presentations.batchUpdate({'requests': requests},slidesId);
}
我创建了一个新函数,而不是仅仅将幻灯片的 objectId 从 Apps 脚本传递到幻灯片 API 我从 API 调用幻灯片并根据位置定位幻灯片幻灯片列表。它返回了完全相同的 objectId (facepalm),但出于某种原因,它只是更喜欢这种方式。哎呀。但它奏效了。它在我从 Apps 脚本创建的现有幻灯片上插入了 table。
function whatSlides(){
const slidesId = '1W-u8h2mQQB4XwJit9bp5XMU5GUuICDyTGSZQPSmlXT0/';
const sloj = 'SLIDES_API2007014672_36'
const preso = Slides.Presentations.get(slidesId);
var theSlides = preso.slides;
var objId = theSlides[8].objectId;
makeTables(preso.presentationId,objId)
}
我正在尝试使用 Google 幻灯片 API 制作一张包含 table 的新幻灯片,其中包含一些数据。 我遇到无效的 JSON 负载错误。
我尝试做的是创建一个函数来发出新的 table 请求。
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)))
]}
}}]
}
这里有一些示例数据可以帮助你帮助我
[{'_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}]
该函数产生了我想要的请求(我认为),但我收到了这个错误。
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.'}]}]">
这是我要发送的完整请求。
[
{
"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
}
}
],
]
}
}
}
]
}
]
如果您能提供帮助,请提前致谢,我知道这个问题有点长。
您应该使用 Table Operations 创建和编辑 table 数据。
示例请求正文:
注意: 下面的请求将创建一个 ID 为 id-1617139878-856039
的新幻灯片,并在其中插入一个包含数据的 table。
{
"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"
}
}
]
}
输出:
我在这里测试了请求:Google Slide API batchUpdate
在这上面花了很多时间之后...我首先使用 API 创建幻灯片,在我添加 table 的地方开始工作。但是,当我尝试从 Apps 脚本 运行 相同的功能(有效)时,我收到此错误
GoogleJsonResponseException:API 调用 slides.presentations.batchUpdate 失败,出现错误:无效请求[0].createTable:找不到页面 (SLIDES_API1632982083_36)。
function pleaseApiMySlides() {
const pageId = Utilities.getUuid();
const requests = {
"title": "alice also made this"
};
Slides.Presentations.create({
"title": "alice made this"
});
var preso = Slides.Presentations.create(requests)
makeSlides(preso.presentationId);
}
function makeSlides(slidesId){
const preso = Slides.Presentations.get(slidesId);
console.log(preso.title);
const oid = Utilities.getUuid();
const requests = [{
'createSlide': {
'objectId': oid,
'slideLayoutReference': {
'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'
}
}
}];
Slides.Presentations.batchUpdate({'requests': requests},slidesId);
makeTables(slidesId,oid);
}
function makeTables(slidesId,slideObj){
console.log(slideObj)
const oid = Utilities.getUuid();
const requests = [{
'createTable':{
"objectId": oid,
"rows": 3,
"columns": 2,
"elementProperties": {
"pageObjectId": slideObj,
}
}
}];
Slides.Presentations.batchUpdate({'requests': requests},slidesId);
}
我创建了一个新函数,而不是仅仅将幻灯片的 objectId 从 Apps 脚本传递到幻灯片 API 我从 API 调用幻灯片并根据位置定位幻灯片幻灯片列表。它返回了完全相同的 objectId (facepalm),但出于某种原因,它只是更喜欢这种方式。哎呀。但它奏效了。它在我从 Apps 脚本创建的现有幻灯片上插入了 table。
function whatSlides(){
const slidesId = '1W-u8h2mQQB4XwJit9bp5XMU5GUuICDyTGSZQPSmlXT0/';
const sloj = 'SLIDES_API2007014672_36'
const preso = Slides.Presentations.get(slidesId);
var theSlides = preso.slides;
var objId = theSlides[8].objectId;
makeTables(preso.presentationId,objId)
}