資料表作業

Google Slides API 可讓您建立和編輯頁面上的表格。 本頁列出了一些常見的資料表作業 presentations.batchUpdate敬上 方法。

這些範例使用下列變數:

  • PRESENTATION_ID:代表您在何處提供 簡報 ID:你可以 從簡報網址找出這個 ID 的值。
  • PAGE_ID:指示您在提供網頁物件的位置。 ID您可以擷取 該網址的值,或是使用 API 讀取要求。
  • TABLE_ID:表示您提供頁面 元素物件 ID 找出您需要的表格您可以為自己建立的元素指定此 ID (有部分 限制) 或允許 Slides API 自動建立投影片。元素 ID 可透過 API 讀取要求擷取。

這些範例會顯示為中立語言的 HTTP 要求。學習 如何使用 Google API 實作不同語言版本的批次更新 用戶端程式庫,請參閱新增形狀和 文字

建立表格

下列 presentations.batchUpdate敬上 程式碼範例顯示如何使用 CreateTableRequest 方法,將表格新增至 PAGE_ID 指定的投影片。

這個資料表有 8 列和 5 欄。請注意, Slides API 會忽略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 是 0 的基礎。

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

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 指定。受影響的儲存格位於 第五列和第三欄rowIndex 和其中的 columnIndex cellLocation 是從 0 開始計算

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

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 柬埔寨字型 textRange。個人中心 您必須針對標頭中的每個儲存格重複這項要求

rowIndexlocation 中的 columnIndexcellLocation 是以 0 為基礎。

以下是設定表格標題列格式的要求通訊協定:

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 是以 0 為基礎。

下方是插入資料表列或欄的要求通訊協定:

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