2
votes

basically I have table (8 rows x 3 columns) within the Google slides presentation, that I want to change background color to via the API.

First item of my list of rgb color values:

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

My function to produce a request body:

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

When I send batch update to presentation I receive the following error:

HttpError: https://slides.googleapis.com/v1/presentations/1dzxYYPuqTM3VhwaR93Ep2jj_9Y2NCkSBsVBnmN6lcOs:batchUpdate?alt=json returned "Invalid JSON payload received. Unknown name "table_cell_backgroun_fill" at 'requests[0].update_table_cell_properties.table_cell_properties': Cannot find field.". Details: "[{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'requests[0].update_table_cell_properties.table_cell_properties', 'description': 'Invalid JSON payload received. Unknown name "table_cell_backgroun_fill" at \'requests[0].update_table_cell_properties.table_cell_properties\': Cannot find field.'}]}]">

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 ?

Thank you.

1

1 Answers

2
votes

How about this answer?

A1:

In this section, it explains about the reason of the error.

Modification points:

  • From the error message of Unknown name "table_cell_backgroun_fill" at 'requests[0].update_table_cell_properties.table_cell_properties', it is found that the property name of tableCellBackgrounFill is a spelling mistake. Please modify to tableCellBackgroundFill.
  • At updateTableCellProperties, the property of fields is required to be used. In your case, how about adding "fields": "tableCellBackgroundFill"? You can also use 'fields': '*'.

When these modifications are reflected to your request body, it becomes as follows.

Modified request body:

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
  }
 }
]
  • Before you use this script, please check the variables of color_list and 'obj_id',

A2:

In this section, it explains about the question 2 of 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 ?.

In your question, you say I have table (8 rows x 3 columns) at the top of your question. But at 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 ?, you say columns (1 to 2). I'm confused about this. So I would like to suppose as follows.

  • Your table has 8 rows and 2 columns.
  • You want to change the background color of all columns and rows with one color.

The sample request body is as follows.

Sample request body:

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"
    }
  }
]
  • rowIndex and columnIndex are the start cell.
    • "rowIndex": 0 and "columnIndex": 0 means the cell "A1".
  • rowSpan and columnSpan are the number of rows and columns.

    • "rowSpan": 8 and "columnSpan": 2 means 8 rows and 2 columns. By this, the background colors of cells "A1:B8" are changed.
    • If your table is 8 rows and 3 columns, and you want to change all cells, please set them as follows.
      • "rowIndex": 0, "columnIndex": 0 and "rowSpan": 8, "columnSpan": 3
  • If you want to change the special background color every cell, it is required to create the array of request body for each cell. Please be careful this.

Note:

  • This answer supposes that you have already been able to put and get values for Google Slides using Slides API.

References:

If I misunderstood your question and this didn't resolve your issue, I apologize.