使用谷歌幻灯片 API 创建表格

问题描述

我正在尝试使用 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.'},'description': 'Invalid JSON payload received. UnkNown name "columns" at \'requests[1].create_shape.element_properties\': Cannot find field.'},'description': 'Invalid JSON payload received. UnkNown name "tableRows" at \'requests[1].create_shape.element_properties\': Cannot find field.'}]}]">

这是我发送的完整请求。

[
 {
  "objectId": "id-1617143529-776043","pageElements": [
   {
    "elementGroup": {
     "table": {
      "rows": 23,"columns": 3,"tableRows": [
       [
        {
         "text": "Customer Service","location": {
          "rowIndex": 0,"columnIndex": 0
         }
        },{
         "text": 239.0,"columnIndex": 1
         }
        },{
         "text": 1875.0,"columnIndex": 2
         }
        }
       ],[
        {
         "text": "Order","location": {
          "rowIndex": 1,{
         "text": 2846.0,{
         "text": 5171.0,[
        {
         "text": "Checkout","location": {
          "rowIndex": 2,{
         "text": 1789.0,{
         "text": 2446.0,]
     }
    }
   }
  ]
 }
]

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

解决方法

您应该使用 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"
      }
    },"columnIndex": 1
        },"text": "239.0"
      }
    },"columnIndex": 2
        },"text": "1875.0"
      }
    },"cellLocation": {
          "rowIndex": 1,"text": "Order"
      }
    },"text": "2846.0"
      }
    },"cellLocation": {
          "rowIndex": 2,"text": "Checkout"
      }
    },"text": "1789.0"
      }
    },"text": "2446.0"
      }
    }
  ]
}

输出:

enter image description here

我在这里测试了请求:Google Slide API batchUpdate

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...