Google スプレッドシートを REST API で操作

Google スプレッドシートREST API で操作します。

API の利用には 前回 と同様にリフレッシュトークンを使います。

はじめに

Google スプレッドシートAPI を使うには、Google アカウントで API の利用を承認する際に scopehttps://spreadsheets.google.com/feeds/ と指定します。 (手順は前回を参照)

アクセストークンの取得

リフレッシュトークンからアクセストークンを REST API で取得するには、 https://www.googleapis.com/oauth2/v3/tokenclient_id=<クライアントID>&client_secret=<クライアントシークレット>&grant_type=refresh_token&refresh_token=<リフレッシュトークン> を POST します。

実行例(cURL
$ curl -d "client_id=xxxxx.apps.googleusercontent.com&client_secret=SarzR・・・&grant_type=refresh_token&refresh_token=1/iiM・・・" https://www.googleapis.com/oauth2/v3/token

{
 "access_token": "ya26.pw・・・",
 "token_type": "Bearer",
 "expires_in": 3600
}

取得したアクセストークンは HTTP ヘッダーで指定します。

アクセストークンの指定例 (HTTP ヘッダー)
Authorization: Bearer ya26.pw・・・

(1) スプレッドシートの一覧を取得

スプレッドシートの一覧を取得するには https://spreadsheets.google.com/feeds/spreadsheets/private/full へ GET します。

スプレッドシート取得例(cURL
$ curl -H "Authorization: Bearer ya26.pw・・・" https://spreadsheets.google.com/feeds/spreadsheets/private/full

<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns='http://www.w3.org/2005/Atom'xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>https://spreadsheets.google.com/feeds/spreadsheets/private/full</id>・・・

処理結果(XML)の内容は以下の通りです。

XML 結果例

<feed>
  ・・・
  <entry>
    <id>https://spreadsheets.google.com/feeds/spreadsheets/private/full/1E0R・・・</id>
    <updated>2015-07-01T16:30:20.027Z</updated>
    <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#spreadsheet"/>
    <title type="text">sample</title>
    <content type="text">sample</content>
    <link rel="http://schemas.google.com/spreadsheets/2006#worksheetsfeed" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full"/>
    <link rel="alternate" type="text/html" href="https://docs.google.com/spreadsheets/d/1E0R・・・/edit"/>
    <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/spreadsheets/private/full/1E0R・・・"/>
    <author>
      ・・・
    </author>
  </entry>
  ・・・
</feed>

(2) スプレッドシート内のシートの一覧を取得

シートの一覧を取得するには https://spreadsheets.google.com/feeds/worksheets/${key}/private/full へ GET します。

${key} の値は (1) で取得した XML の entry/id 要素から取得できます。 (https://spreadsheets.google.com/feeds/spreadsheets/private/full/${key}

シート取得例(cURL
$ curl -H "Authorization: Bearer ya26.pw・・・" https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gs='http://schemas.google.com/spreadsheets/2006'><id>https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full</id>・・・

処理結果(XML)の内容は以下の通りです。

XML 結果例

<feed>
  ・・・
  <entry>
    <id>https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full/od6</id>
    <updated>2015-07-01T16:30:20.010Z</updated>
    <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#worksheet"/>
    <title type="text">シート1</title>
    <content type="text">シート1</content>
    <link rel="http://schemas.google.com/spreadsheets/2006#listfeed" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/list/1E0R・・・/od6/private/full"/>
    <link rel="http://schemas.google.com/spreadsheets/2006#cellsfeed" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full"/>
    <link rel="http://schemas.google.com/visualization/2008#visualizationApi" type="application/atom+xml" href="https://docs.google.com/spreadsheets/d/1E0R・・・/gviz/tq?gid=0"/>
    <link rel="http://schemas.google.com/spreadsheets/2006#exportcsv" type="text/csv" href="https://docs.google.com/spreadsheets/d/1E0R・・・/export?gid=0&format=csv"/>
    <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full/od6"/>
    <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/worksheets/1E0R・・・/private/full/od6/6cl・・・"/>
    <gs:colCount>26</gs:colCount>
    <gs:rowCount>1000</gs:rowCount>
  </entry>
  ・・・
</feed>

なお、シートの一覧を取得する URL は、(1) の XML から以下のような XPath 式で取り出す事も可能です。

XPath
//entry[title = '<スプレッドシートのタイトル>']/link[@rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']/@href

(3) シート内のセルを取得

セルを取得するには https://spreadsheets.google.com/feeds/cells/${key}/${worksheetId}/private/full へ GET します。

${worksheetId} は (2) で取得した XML の entry/id 要素から取得できます。 (https://spreadsheets.google.com/feeds/worksheets/${key}/private/full/${worksheetId}

セル取得例(cURL
$ curl -H "Authorization: Bearer ya26.pw・・・" https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full

<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gs='http://schemas.google.com/spreadsheets/2006'><id>https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full</id>・・・

処理結果(XML)の内容は以下の通りです。

XML 結果例

<feed>
  ・・・
  <entry>
    <id>https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C1</id>
    <updated>2015-07-01T16:30:20.010Z</updated>
    <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#cell"/>
    <title type="text">A1</title>
    <content type="text">aaa</content>
    <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C1"/>
    <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C1/kgy・・・"/>
    <gs:cell row="1" col="1" inputValue="aaa">aaa</gs:cell>
  </entry>
  ・・・
</feed>

なお、セルの一覧を取得する URL は、(2) の XML から以下のような XPath 式で取り出す事も可能です。

XPath
//entry[title = '<シートのタイトル>']/link[@rel='http://schemas.google.com/spreadsheets/2006#cellsfeed']/@href

(4) セルを登録・更新

https://spreadsheets.google.com/feeds/cells/${key}/${worksheetId}/private/full へ以下のようなフォーマットの XML を POST する事でセルを登録 (更新も可) できます。

注意点として、Content-Type を application/atom+xml と指定する必要があります。

セル登録 XML フォーマット例
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">
  <id>https://spreadsheets.google.com/feeds/cells/${key}/${worksheetId}/private/full/R${行}C${列}</id>
  <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/${key}/${worksheetId}/private/full/R${行}C${列}"/>
  <gs:cell row="${行}" col="${列}" inputValue="${値}"/>
</entry>

今回は以下の XML ファイルを使って E1 セルへ sample123 という文字を登録してみます。

e1.xml
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:gs="http://schemas.google.com/spreadsheets/2006">
  <id>https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5</id>
  <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5" />
  <gs:cell row="1" col="5" inputValue="sample123" />
</entry>
セル登録例(cURL
$ curl -X POST -H "Authorization: Bearer ya26.pw・・・" -H "Content-Type: application/atom+xml" -d @e1.xml https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full

<?xml version='1.0' encoding='UTF-8'?><entry xmlns='http://www.w3.org/2005/Atom' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gs='http://schemas.google.com/spreadsheets/2006'><id>https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5</id>・・・

処理結果(XML)の内容は以下の通りです。

XML 結果例

<entry>
  <id>https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5</id>
  <updated>2015-07-01T16:30:20.010Z</updated>
  <category scheme="http://schemas.google.com/spreadsheets/2006" term="http://schemas.google.com/spreadsheets/2006#cell"/>
  <title type="text">E1</title>
  <content type="text">sample123</content>
  <link rel="self" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5"/>
  <link rel="edit" type="application/atom+xml" href="https://spreadsheets.google.com/feeds/cells/1E0R・・・/od6/private/full/R1C5/jikzt4"/>
  <gs:cell row="1" col="5" inputValue="sample123">sample123</gs:cell>
</entry>

link[@rel='edit']/@hrefjikzt4 はそのセルのバージョン番号です。