Table Operations

The Slides API allows you to create and edit tables on pages. The examples on this page show some common table operations that can be achieved with the API.

These examples use the following variables:

  • presentationId — indicates where you provide the presentation ID. You can discover the value for this ID from the presentation URL.
  • pageId — indicates where you provide the page object ID. You can retrieve the value for this from the URL or by using an API read request.
  • tableId — indicates where you provide the page element object ID for a table you are working with. You can specify this ID for elements you create (with some restrictions) or allow the API to automatically create one; element IDs can be retrieved through an API read request.

Create a table

The following presentations.batchUpdate request adds a new table to a slide specified by pageId. This table has 8 rows and 5 columns. Note that the API will ignore any size or transform fields provided as part of the elementProperties — instead, the API creates a table that is roughly centered on the slide and is sized to accommodate the specified number of rows and columns, if possible.

The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.

POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{
  "requests": [
    {
      "createTable": {
        "objectId": tableId,
        "elementProperties": {
          "pageObjectId": pageId,
        },
        "rows": 8,
        "columns": 5
      }
    }
  ]
}

Delete table rows or columns

The following presentations.batchUpdate request removes the 6th row and the 4th column from the table specified by tableId.

The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.

POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{
  "requests": [
    {
      "deleteTableRow": {
        "tableObjectId": tableId,
        "cellLocation": {
          "rowIndex": 5
        }
      }
    },
    {
      "deleteTableColumn": {
        "tableObjectId": tableId,
        "cellLocation": {
          "columnIndex": 3
        }
      }
    }
  ]
}

Edit table data

The following presentations.batchUpdate request removes all the text in a cell and then replaces it with new text. The table is identified by tableId, and the cell affected is in the 5th row and 3rd column.

The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.

POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{
  "requests": [
    {
      "deleteText": {
        "objectId": tableId,
        "cellLocation": {
          "rowIndex": 4,
          "columnIndex": 2
        },
        "textRange": {
          "type": "ALL",
        }
      }
    },
    {
      "insertText": {
        "objectId": tableId,
        "cellLocation": {
          "rowIndex": 4,
          "columnIndex": 2
        },
        "text": "Kangaroo",
        "insertionIndex": 0
      }
    }
  ]
}

Format a table header row

The following presentations.batchUpdate request formats the header row of a table element indicated by tableId. The first request in the batch sets the background color of the header row to black. Each following request sets the text format in one cell of the header row to bold white 18-pt Cambria font.

The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.

POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{
  "requests": [
    {
      "updateTableCellProperties": {
        "objectId": tableId,
        "tableRange": {
          "location": {
            "rowIndex": 0,
            "columnIndex": 0
          },
          "rowSpan": 1,
          "columnSpan": 3
        },
        "tableCellProperties": {
          "tableCellBackgroundFill": {
            "solidFill": {
              "color": {
                "rgbColor": {
                  "red": 0.0,
                  "green": 0.0,
                  "blue": 0.0
                }
              }
            }
          }
        },
        "fields": "tableCellBackgroundFill.solidFill.color"
      }
    },
    {
      "updateTextStyle": {
        "objectId": tableId,
        "cellLocation": {
          "rowIndex": 0,
          "columnIndex": 0
        },
        "style": {
          "foregroundColor": {
            "opaqueColor": {
              "rgbColor": {
                "red": 1.0,
                "green": 1.0,
                "blue": 1.0
              }
            }
          },
          "bold": true,
          "fontFamily": "Cambria",
          "fontSize": {
            "magnitude": 18,
            "unit": "PT"
          }
        },
        "textRange": {
          "type": "ALL"
        },
        "fields": "foregroundColor,bold,fontFamily,fontSize"
      }
    },
    // Repeat the above request for each additional cell in the header row....
  ]
}

This request formats a header row to look like this:

Format header row recipe result

Insert table rows or columns

The following presentations.batchUpdate request adds three rows below the 6th row in the table specified by tableId. A second request then adds two columns to the left of the 4th column in the same table.

The request protocol is shown below. The Add Text and Shapes guide shows an example that implements a batch update in different languages using the Google API client libraries.

POST https://slides.googleapis.com/v1/presentations/presentationId:batchUpdate
{
  "requests": [
    {
      "insertTableRows": {
        "tableObjectId": tableId,
        "cellLocation": {
          "rowIndex": 5
        },
        "insertBelow": true,
        "number": 3
      }
    },
    {
      "insertTableColumns": {
        "tableObjectId": tableId,
        "cellLocation": {
          "columnIndex": 3
        },
        "insertRight": false,
        "number": 2
      }
    }
  ]
}