> ## Documentation Index
> Fetch the complete documentation index at: https://www.integrate.io/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# ETL: Google Sheetからデータを取得する

> Integrate.io ETLのRest APIコンポーネントを使ってGoogle SheetのシートIDとセル範囲を指定し、batchGetエンドポイント経由でデータを取得する方法と認証設定について解説します。

## Rest API コンポーネントの設定

### 認証（Authentication）の設定

あらかじめ接続画面で作成したGoogle Sheetの認証を選択する。

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-1.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=e16283572d2e59f0f7b15ca4b4b59b25" alt="restapi-part02-jp image 1" width="1956" height="908" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-1.webp" />
</Frame>

### URLの指定方法

**URL:**
`https://sheets.googleapis.com/v4/spreadsheets/シートID/values:batchGet?ranges=セル範囲&majorDimension=ROWS`

* **シートID:**
  Google SheetのURLからシートIDを取得

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-2.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=324806e3dee88c213806335aae02e680" alt="restapi-part02-jp image 2" width="1878" height="1262" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-2.webp" />
</Frame>

* **セル範囲:**
  例）A1:G1000 (A列の1行目からG列の1000行目まで）

### Responseの設定

「\$..values\[\*]」と入力

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-3.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=a40b2b6f68726c62dbc84ea6ba95233c" alt="restapi-part02-jp image 3" width="1956" height="908" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-3.webp" />
</Frame>

### フィールドの選択

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-4.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=4f4e1c44039cbc015eb1fde411ecd5e0" alt="restapi-part02-jp image 4" width="1928" height="1270" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-4.webp" />
</Frame>

## 取得したJSONのフラット化

### 変換1:BAG型からTuple型に変換する

Selectコンポーネントを使用し、BagToTuple関数を使いTuple型に変換する

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-5.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=d3ed7daff52067d63597638c65fa5933" alt="restapi-part02-jp image 5" width="2084" height="1270" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-5.webp" />
</Frame>

### 変換2:各列を配列番号を指定し、それぞれ取得する

それぞれの列を配列番号（\$0）を指定し取得するとともに、Chararray関数で文字列型に変換する

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-6.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=e9a6afbe32adcaec19b38f5a5d8d89dd" alt="restapi-part02-jp image 6" width="2084" height="1270" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-6.webp" />
</Frame>

### ヘッダ行をフィルターで除外

1行目にヘッダ行が含まれている場合、フィルタで除外する（Rest API側のセルの範囲指定でヘッダ行を除外する方法もあり。）
（例）id列に「Id」という文字が含まれていないデータのみ抽出

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-7.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=d3d5c281835fe26d7cb4deebeb6bf87d" alt="restapi-part02-jp image 7" width="1872" height="1188" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-7.webp" />
</Frame>

## ロード先のコンポーネントをセットし、ジョブを実行して確認

最後にデータベース、DWH、ストレージなどにロードすれば完了です。

### パッケージ全体図

例）Snowflakeがロード先の場合、以下のようなフローになります。

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-8.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=31e55e0c3a8aa7e2c46dfac7e2fb3803" alt="restapi-part02-jp image 8" width="2168" height="1448" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-8.webp" />
</Frame>

### データが正しく反映されたことを確認

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-9.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=9e9d4aacdd2e4dcb8e596d238bc939cf" alt="restapi-part02-jp image 9" width="2310" height="512" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-9.webp" />
</Frame>

## Google Sheetへの書き込みも可能です

Xpelntyでは、Selectコンポーネントの中でCurlリクエストを書くことで、Google Sheetへの書き込みが可能です。

<Frame>
  <img src="https://mintcdn.com/integrateio/u3KZjgbItgbZsh3u/images/japanese-knowledge-base/restapi-part02-jp/image-10.webp?fit=max&auto=format&n=u3KZjgbItgbZsh3u&q=85&s=911a7b79551f7fac7a9ce52df586face" alt="restapi-part02-jp image 10" width="2026" height="1434" data-path="images/japanese-knowledge-base/restapi-part02-jp/image-10.webp" />
</Frame>

**書き込みリクエスト例：**
`CCurl(CONCAT('https://sheets.googleapis.com/v4/spreadsheets/', '$spreadsheetId', '/values:batchUpdate'),'POST','{"Accept":"application/json"}',CONCAT('{"valueInputOption":"RAW","data":[{"range":"','$range','","majorDimension": "ROWS","values": [', BagToString(data, ','), ',]}]}'), '$connection_id')`

**上記リクエストで使用している変数について**

* `$spreadsheetId`：GoogleSheetのIDを指定
* `$connection_id`：XplentyのコネクションIDを指定 （コネクション画面で確認することができます。）
  例：GOOGLESHEETS\_CONNECTION\_9215
* `$range`：シート名およびセル範囲を指定。
  例：campaign!A1:N100000
