資料表作業

Google 簡報 API 可讓您建立及編輯網頁上的表格。本頁的範例將說明一些使用 presentations.batchUpdate 方法的常見資料表作業。

這些範例使用下列變數:

  • PRESENTATION_ID:指出您提供呈現 ID 的位置。您可以透過顯示網址探索這個 ID 的值。
  • PAGE_ID:指出您要提供頁面物件 ID 的位置。您可以從網址或使用 API 讀取要求擷取這個值。
  • TABLE_ID:指出您要為目前使用的資料表提供頁面元素物件 ID 的位置。您可以為自己建立的元素 (設有部分限制) 指定此 ID,或讓 Slide API 自動建立。您可以透過 API 讀取要求擷取元素 ID。

以下範例是以 HTTP 要求為不受語言限制。如要瞭解如何使用 Google API 用戶端程式庫,以不同語言實作批次更新,請參閱「新增形狀和文字」一文。

建立資料表

以下 presentations.batchUpdate 程式碼範例說明如何使用 CreateTableRequest 方法,將資料表新增至 PAGE_ID 指定的投影片。

這個表格有八列和五欄,請注意,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 指定。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 柬埔寨字型。您需要對標頭中的每個額外儲存格重複這項要求。

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