資料表作業

簡報 API 可讓您在頁面上建立及編輯表格。本頁的範例說明瞭 API 可完成的一些常見資料表作業。

這些範例使用以下變數:

  • presentationId:指出您提供簡報 ID 的位置。您可以從簡報網址找到這個 ID 的值。
  • pageId:指出您提供頁面物件 ID 的位置。您可以透過網址或使用 API 讀取要求擷取此值。
  • tableId — 指出您為處理的資料表提供頁面元素物件 ID 的位置。您可以為自己建立的元素 (有一些限制) 指定這個 ID,或允許 API 自動建立元素 ID;您可以透過 API 讀取要求擷取元素 ID。

建立資料表

下列 presentations.BatchUpdate 要求會在 pageId 指定的投影片中新增資料表。這個資料表含有 8 個資料列和 5 個資料欄。請注意,API 會忽略 elementProperties 中提供的任何 sizetransform 欄位,反之,API 會建立一個資料表並略為聚焦於投影片,並會調整大小以配合指定的列數和欄數。

要求通訊協定如下所示。新增文字和形狀指南中的範例,說明如何使用 Google API 用戶端程式庫,以不同語言實作批次更新。

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

刪除表格列或欄

下列 presentations.BatchUpdate 要求會從 tableId 指定的資料表中移除第 6 個資料列和第 4 個資料欄。

要求通訊協定如下所示。新增文字和形狀指南中的範例,說明如何使用 Google API 用戶端程式庫,以不同語言實作批次更新。

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

編輯資料表資料

以下 presentations.BatchUpdate 要求會移除儲存格中的所有文字,然後替換成新文字。資料表由 tableId 識別,而受影響的儲存格位於第 5 列和第 3 欄。

要求通訊協定如下所示。新增文字和形狀指南中的範例,說明如何使用 Google API 用戶端程式庫,以不同語言實作批次更新。

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

設定表格標題列的格式

下列 presentations.BatchUpdate 要求會格式化 tableId 所指定資料表元素的標頭列。批次中的第一個要求會將標題列的背景顏色設為黑色。每個要求都會將標題列其中一個儲存格中的文字格式設定為粗體的白色 18-pt Cambria 字型。

要求通訊協定如下所示。「新增文字和形狀」指南中的範例說明如何使用 Google API 用戶端程式庫,以不同語言實作批次更新。

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

此要求會將標題列格式化,如下所示:

設定標題列食譜結果的格式

插入表格列或欄

以下 presentations.BatchUpdate 要求會在 tableId 指定的資料表的第 6 列下方新增三列。接著,第二個要求會在相同資料表的第 4 欄左側新增兩個資料欄。

要求通訊協定如下所示。新增文字和形狀指南中的範例,說明如何使用 Google API 用戶端程式庫,以不同語言實作批次更新。

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