2

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.

Simas
  • 642
  • 8
  • 15

1 Answers1

3

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.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you Tanaike. This solves it. Thanks for spotting the typo as well. What do you mean by "please be careful this" when change the background color for every cell? This is actually the real objective that I want to achieve. :) – Simas Sep 20 '19 at 12:01
  • 1
    @Simas Thank you for replying. I'm glad your issue was resolved. About "please be careful this", if you want to change each background colors for each cell, `"rowSpan": 8` and `"columnSpan": 2` cannot be used, and it is required to create the request body for each cell. When `"rowSpan": 8` and `"columnSpan": 2` is used, all cells in the range become the same color. So I added it. – Tanaike Sep 20 '19 at 23:36
  • 1
    I see your point, I have approached it the same way as you have described, creating request body for each cell in the table. Thank you. – Simas Sep 22 '19 at 09:11
  • @Tanaike is there a way we can use hexcolor instead of rgbcolor? – user3422637 Jun 07 '22 at 16:23
  • @user3422637 About `is there a way we can use hexcolor instead of rgbcolor?`, I think that when Slides API is used, in the current stage, it is required to convert the hex color to the RGB color. I apologize for this. – Tanaike Jun 07 '22 at 22:53