表操作

借助 GoogleSlides API,您可以在页面上创建和编辑表格。本单元中的示例 此页面显示了一些常见的表格操作 presentations.batchUpdate 方法。

这些示例使用以下变量:

  • PRESENTATION_ID - 表示您在哪里提供 演示文稿 ID。您可以 从演示文稿网址中发现此 ID 的值。
  • PAGE_ID - 表示您提供 page 对象的位置 ID。您可以使用 从网址或使用 API 读取请求获取此值。
  • TABLE_ID - 表示您提供网页的位置 指定元素对象 ID 正在使用的表。您可以为自己创建的元素指定此 ID (一些 限制) 也可以让幻灯片 API 自动创建一个元素 ID 可以通过 API 读取请求进行检索。

这些示例以 HTTP 请求的形式呈现,与语言无关。学习内容 如何使用 Google API 实现不同语言的批量更新 请参阅添加形状和 文本

创建表

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

此表格有八行五列。请注意,使用幻灯片的 会忽略作为sizetransform elementProperties。 相反,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_IDrowIndexcolumnIndex cellLocation 从零开始。

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

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 指定。受影响的单元格位于 显示第五行和第三列内的 rowIndexcolumnIndex cellLocation 从零开始。

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

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 方法设置表格元素的标题行格式, tableRange, 由 TABLE_ID 指定。然后,它会使用 TableCellProperties 方法将标题行的背景颜色设为黑色。

下面的每个请求都使用 UpdateTextStyleRequest 方法将标题行某个单元格中的文本格式设为粗体、白色 18-pt Cambria 字体中的 textRange。您 则需要对标题中的其他每个单元格重复此请求。

location 中的 rowIndexcolumnIndex 以及 cellLocation 从零开始。

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

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 指定。rowIndexcellLocation 中的 columnIndex 从零开始。

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

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
      }
    }
  ]
}