谷歌幻灯片 API 更新表背景颜色



基本上我在Google幻灯片演示文稿中有表格(8行x 3列(,我想通过API将背景颜色更改为。

我的 rgb 颜色值列表的第一项:

cons_data_lst[0][1][-1]
>>> [0.5882353, 0.7764706, 0.4862745]

我生成请求正文的函数:

def update_table_cell_colors(color_list):    
req = [{
'updateTableCellProperties':{
'objectId': 'obj_id',
'tableRange': {
'location': {
'rowIndex': 1,
'columnIndex': 2,
},
'rowSpan': 1,
'columnSpan': 1,
},
'tableCellProperties':{
'tableCellBackgrounFill':{
'solidFill':{
'color':{
'rgbColor':{
'red': color_list[0],
'green': color_list[1],
'blue': color_list[2],
}
}
}}
}}} ]
return req

当我向演示文稿发送批量更新时,我收到以下错误:

HttpError: https://slides.googleapis.com/v1/presentations/1dzxYYPuqTM3VhwaR93Ep2jj_9Y2NCkSBsVBnmN6lcOs:batchUpdate?alt=json 返回"收到的 JSON 有效负载无效。未知名称 "table_cell_backgroun_fill" 在 'requests[0].update_table_cell_properties.table_cell_properties': 找不到字段。详细信息:"[{'@type': "type.googleapis.com/google.rpc.BadRequest"、"字段违规": [{'字段': 'requests[0].update_table_cell_properties.table_cell_properties', "描述":"收到的 JSON 有效负载无效。未知名称 "table_cell_backgroun_fill" 在 \'requests[0].update_table_cell_properties.table_cell_properties\': 找不到字段。'}]}]">

给定不同 rgb 颜色值的列表,如何创建请求正文以更新所有列(1 到 2(行(有 8 个(文本背景颜色?

谢谢。

这个答案怎么样?

答1:

在本节中,它解释了错误的原因。

修改点:

  • Unknown name "table_cell_backgroun_fill" at 'requests[0].update_table_cell_properties.table_cell_properties'的错误消息中发现tableCellBackgrounFill的属性名是拼写错误。请修改为tableCellBackgroundFill
  • updateTableCellPropertiesfields的财产是必需的。在您的情况下,如何添加"fields": "tableCellBackgroundFill"?您也可以使用'fields': '*'.

当这些修改反映到请求正文时,它将如下所示。

修改的请求正文:

req = [
{
'updateTableCellProperties': {
'objectId': 'obj_id',
'tableRange': {
'location': {
'rowIndex': 1,
'columnIndex': 2
},
'rowSpan': 1,
'columnSpan': 1
},
'tableCellProperties': {
'tableCellBackgroundFill': {  # Modified
'solidFill': {
'color': {
'rgbColor': {
'red': color_list[0],
'green': color_list[1],
'blue': color_list[2],
}
}
}
}
},
'fields': 'tableCellBackgroundFill'  # Added
}
}
]
  • 在使用此脚本之前,请检查color_list'obj_id'的变量,

答2:

在本节中,它解释了Given a list of different rgb color values, how can I create a request body to update all columns (1 to 2) row (there are 8) text background color ?的问题 2 .

在你的问题中,你说I have table (8 rows x 3 columns)在你的问题的顶部。但是在Given a list of different rgb color values, how can I create a request body to update all columns (1 to 2) row (there are 8) text background color ?,你说columns (1 to 2).我对此感到困惑。所以我想假设如下。

  • 您的表格有 8 行和 2 列。
  • 您想用一种颜色更改所有列和行的背景颜色。

示例请求正文如下所示。

示例请求正文:

req = [
{
"updateTableCellProperties": 
{
"objectId": "obj_id",
"tableRange": 
{
"location": 
{
"rowIndex": 0,
"columnIndex": 0
},
"rowSpan": 8,
"columnSpan": 2
},
"tableCellProperties": 
{
"tableCellBackgroundFill": 
{
"solidFill": 
{
"color": 
{
"rgbColor": 
{
"red": color_list[0],
"green": color_list[1],
"blue": color_list[2]
}
}
}
}
},
"fields": "tableCellBackgroundFill"
}
}
]
  • rowIndexcolumnIndex是起始单元格。
    • "rowIndex": 0"columnIndex": 0表示单元格"A1"。
  • rowSpancolumnSpan是行数和列数。

    • "rowSpan": 8"columnSpan": 2表示 8 行和 2 列。这样,单元格"A1:B8"的背景颜色就会改变。
    • 如果您的表格是8行3列,并且要更改所有单元格,请按如下方式设置它们。
      • "rowIndex": 0"columnIndex": 0"rowSpan": 8"columnSpan": 3
  • 如果要更改每个单元格的特殊背景颜色,则需要为每个单元格创建请求正文数组。请小心这一点。

注意:

  • 这个答案假设您已经能够使用幻灯片 API 放置和获取 Google 幻灯片的值。

引用:

  • 更新表单元格属性请求
  • 表单元格属性

如果我误解了您的问题并且这没有解决您的问题,我深表歉意。

最新更新