Google Slides API 可讓您在頁面上建立及編輯表格。本頁的範例說明如何使用 presentations.batchUpdate
方法執行一些常見的表格作業。
這些範例使用下列變數:
- PRESENTATION_ID:指出您提供簡報 ID 的位置。您可以從簡報網址找出這個 ID 的值。
- PAGE_ID:表示您提供網頁物件 ID 的位置。您可以從網址或透過 API 讀取要求,擷取這個值。
- TABLE_ID:指出您要為使用的表格提供網頁元素物件 ID 的位置。您可以為建立的元素指定這個 ID (但有部分限制),也可以讓 Slides API 自動建立 ID。元素 ID 可透過 API 讀取要求擷取。
這些範例以 HTTP 要求的形式呈現,因此不限語言。如要瞭解如何使用 Google API 用戶端程式庫,以不同語言實作批次更新,請參閱「新增圖案和文字」。
建立資料表
下列 presentations.batchUpdate
程式碼範例說明如何使用 CreateTableRequest
方法,將表格新增至 PAGE_ID 指定的投影片。
這個表格有八列和五欄。請注意,簡報 API 會忽略以 elementProperties
形式提供的任何 size
或 transform
欄位。而是會盡可能在投影片上建立大致置中的表格,並根據指定的列數和欄數調整大小。
以下是建立資料表的請求通訊協定:
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
內的 rowIndex
和 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 } } } ] }
編輯資料表資料
下列程式碼範例說明如何使用 DeleteTextRequest
方法,移除 textRange
內儲存格中的所有文字。presentations.batchUpdate
然後使用 InsertTextRequest
方法,將其取代為新文字「Kangaroo」。
資料表是由 TABLE_ID 指定。受影響的儲存格位於第五列和第三欄。cellLocation
內的 rowIndex
和 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
方法,將標題列中一個儲存格的文字格式設為粗體、白色 18 號 Cambria 字型,並位於 textRange
內。然後,您需要為標頭中的每個額外儲存格重複執行這項要求。
location
和 cellLocation
內的 rowIndex
和 columnIndex
都是以零為基準。
以下是格式化資料表標題列的要求通訊協定:
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.... ] }
更新後,格式化的標題列如下所示:
插入表格列或欄
下列程式碼範例說明如何使用 InsertTableRowsRequest
方法,在第六列下方新增三列。presentations.batchUpdate
然後使用 InsertTableColumnsRequest
方法,在同一個表格的第四欄左側新增兩欄。
資料表是由 TABLE_ID 指定。cellLocation
中的 rowIndex
和 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 } } ] }