表操作

您可以通过 GoogleSlides API 在页面上创建和编辑表格。本页面上的示例演示了使用 presentations.batchUpdate 方法的一些常见表操作。

这些示例使用以下变量:

  • PRESENTATION_ID - 表示您提供演示文稿 ID 的位置。您可以从呈现网址中找到此 ID 的值。
  • PAGE_ID - 表示您提供页面对象 ID 的位置。您可以从网址或使用 API 读取请求检索此属性的值。
  • TABLE_ID - 指示您为正在处理的表提供页面元素对象 ID 的位置。您可以为自己创建的元素指定此 ID(存在一些限制),也可以允许 Slides API 自动创建一个。您可以通过 API 读取请求检索元素 ID。

为了确保不考虑语言,这些示例以 HTTP 请求的形式呈现。如需了解如何使用 Google API 客户端库实现不同语言的批量更新,请参阅添加形状和文本

创建表

以下 presentations.batchUpdate 代码示例展示了如何使用 CreateTableRequest 方法将表格添加到 PAGE_ID 指定的幻灯片中。

此表格包含 8 行 5 列。请注意,Slides API 会忽略作为 elementProperties 的一部分提供的任何 sizetransform 字段。相反,API 会创建一个大致位于幻灯片中心的表格,其大小可以容纳指定的行数和列数(如果可能)。

以下是创建表的请求协议:

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

删除表格中的行或列

以下 presentations.batchUpdate 代码示例展示了如何使用 DeleteTableRowRequest 方法移除第六行。然后,使用 DeleteTableColumnRequest 方法移除第四列。该表由 TABLE_ID 指定。cellLocation 中的 rowIndexcolumnIndex 均从零开始。

以下是删除表格行或列的请求协议:

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

修改表数据

以下 presentations.batchUpdate 代码示例展示了如何使用 DeleteTextRequest 方法移除 textRange 中某个单元格中的所有文本。然后,它会使用 InsertTextRequest 方法将其替换为新文本“Kangaroo”。

该表由 TABLE_ID 指定。受影响的单元格位于第五行第三列。cellLocation 中的 rowIndexcolumnIndex 均从零开始。

以下是修改表数据的请求协议:

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

设置表格标题行的格式

以下 presentations.batchUpdate 代码示例展示了如何使用 UpdateTableCellPropertiesRequest 方法在 TABLE_ID 指定的 tableRange 内设置表元素标题行的格式。然后,它会使用 TableCellProperties 方法将标题行的背景颜色设置为黑色。

以下每个请求都使用 UpdateTextStyleRequest 方法在 textRange 中将标题行一个单元格中的文本格式设置为粗体的白色 18-pt 坎 bria 字体。然后,您需要对标头中的每个额外单元格重复此请求。

locationcellLocation 中的 rowIndexcolumnIndex 都是从零开始的。

以下是用于设置表格标题行格式的请求协议:

POST https://slides.googleapis.com/v1/presentations/PRESENTATION_ID:batchUpdate
{
  "requests": [
    {
      "updateTableCellProperties": {
        "objectId": TABLE_ID,
        "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": TABLE_ID,
        "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....
  ]
}

完成这些更新后,格式化的标题行将如下所示:

设置标题行方案结果的格式。

插入表格中的行或列

以下 presentations.batchUpdate 代码示例展示了如何使用 InsertTableRowsRequest 方法在第六行下方添加三行。然后,使用 InsertTableColumnsRequest 方法在同一表中第四列的左侧添加两列。

该表由 TABLE_ID 指定。cellLocation 中的 rowIndexcolumnIndex 均从零开始。

以下是插入表格行或列的请求协议:

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