【问题标题】:create a table using google slides API使用 google slides API 创建表格
【发布时间】:2021-03-30 17:45:34
【问题描述】:

我正在尝试使用 Google Slides API 制作一张包含包含一些数据的表格的新幻灯片。 我收到了无效的 JSON 负载错误。

我试图做的是创建一个发出新表请求的函数。

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
         }
        }
       ],
      ]
     }
    }
   }
  ]
 }
]

如果您能提供帮助,请提前感谢,我知道这个问题有点长。

【问题讨论】:

    标签: python google-slides-api


    【解决方案1】:

    您应该使用Table Operations 来创建和编辑表格数据。

    请求正文示例:

    注意: 下面的请求将创建一个 ID 为 id-1617139878-856039 的新幻灯片,并在其中插入一个包含数据的表格。

    {
      "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

    【讨论】:

      【解决方案2】:

      在这上面花了很多时间之后......我通过首先使用 API 创建幻灯片,让它在我添加表格的地方工作。但是,当我尝试从 Apps 脚本运行相同的功能(有效)时,我收到此错误

      GoogleJsonResponseException:对 slides.presentations.batchUpdate 的 API 调用失败并出现错误:无效请求 [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 Script 传递到 Slides API,而是从 API 调用幻灯片并根据幻灯片列表中的位置定位幻灯片。它返回了完全相同的 objectId (facepalm),但由于某种原因,它只是更喜欢这种方式。哎呀。但它奏效了。它将表格插入到我从 Apps 脚本创建的现有幻灯片上。

      
      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)
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-10
        • 1970-01-01
        • 2018-08-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多