資料表作業

Google Slides 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 欄。請注意,Slide 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 指定。rowIndexcellLocation 中的 columnIndex 都是以零為基礎。

以下是刪除資料表列或欄的要求通訊協定:

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 指定。受影響的儲存格位於第五列和第三欄rowIndexcellLocation 中的 columnIndex 都是以零為基礎。

以下是編輯資料表資料的要求通訊協定:

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 白色字型。然後,您必須為標頭中的每個額外儲存格重複這項要求。

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 指定。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
      }
    }
  ]
}