Google関連

Google Sheets API v4が登場しました。

Google Apps Developer Blog: New ways to keep data flowing between your apps and ours」にある通り、新しいGoogle Sheets API「v4」が登場しました。

The new Sheets API gives developers programmatic access to powerful features in the Sheets web and mobile interfaces, including charts and pivot tables. For example, developers can use Sheets as part of a rich workflow that pushes data from their app into Sheets and allows users to collaborate on that data before the updated data is pulled back into the original app, removing altogether the need to copy and paste.

Google Apps Developer Blog: New ways to keep data flowing between your apps and ours より

というのが新しいAPIのポイントのようです。

Google.Apis.Sheets.v4 Client Library

発表されて間が無いSheets API v4ですが、もうNugetで.NET用のライブラリが公開されています。

・NuGet Gallery | Google.Apis.Sheets.v4 Client Library
https://www.nuget.org/packages/Google.Apis.Sheets.v4/

今回はこのライブラリを試してみようと思います。

nuget install Google.Apis.Sheets.v4

下準備

APIを呼び出すにあたり、クライアント IDとクライアント シークレットが必要になるので、まずはそれらを準備します。

  1. Google API Consoleを開き、Google アカウントでログインします。
  2. アプリケーションの登録画面が表示されたら「新しいプロジェクトを作成」を選択し、「続行」ボタンをクリックします。
  3. Google_Sheets_API_v4_01

  4. APIが有効化されました画面が表示されたら「認証情報に進む」ボタンをクリックします。
  5. Google_Sheets_API_v4_02

  6. プロジェクトへの認証情報の追加画面が表示されたら「クライアント ID」をクリックします。
  7. Google_Sheets_API_v4_03

  8. クライアント IDの作成画面が表示されたら「同意画面を設定」ボタンをクリックします。
  9. Google_Sheets_API_v4_04

  10. 認証情報画面が表示されたら「ユーザーに表示するサービス名」等の項目を入力し、「保存」ボタンをクリックします。
  11. Google_Sheets_API_v4_05

  12. アプリケーションの種類を「その他」にし、名前を入力した後、「作成」ボタンをクリックします。
  13. Google_Sheets_API_v4_06

  14. クライアント IDとクライアント シークレットが表示されるので、これをメモ帳にでもコピーしておきます。
  15. Google_Sheets_API_v4_07

C#からのAPI呼び出し

クライアント IDとクライアント シークレットを取得出来たら準備完了、さっそく.NETからSheets APIを呼び出してみたいと思います。

※ クライアント IDとクライアント シークレット、シートIDは自分が取得したものに置き換えてください。
※ シートのIDはURLから取得することができます。

Google_Sheets_API_v4_08

/*
 * 以下参照
 *   Google.Apis,Google.Apis.Auth,Google.Apis.Auth.PlatformServices,
 *   Google.Apis.Core,Google.Apis.PlatformServices,Google.Apis.Sheets.v4
 */
using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;

namespace SheetsApiv4Sample
{
  class Program
  {
    static string[] Scopes = {SheetsService.Scope.SpreadsheetsReadonly};
    static string ApplicationName = "Google Sheets API .NET Quickstart";
    static string AppClientId = "(クライアント ID)";
    static string AppClientSecret = "(クライアント シークレット)";
    static string SpreadSheetId = "(操作対象となるシートのID)";
    
    public static void Main(string[] args)
    {
      UserCredential credential;
      string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
      credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
      credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets
        {
          ClientId = AppClientId,
          ClientSecret = AppClientSecret
        },
        Scopes,
        "user",
        CancellationToken.None,
        new FileDataStore(credPath, true)
      ).Result;
      //Console.WriteLine("Credential file saved to: " + credPath);
      
      var service = new SheetsService(new BaseClientService.Initializer()
      {
         HttpClientInitializer = credential,
         ApplicationName = ApplicationName,
      });
      
      //Sheet1のセルA1の値取得
      SpreadsheetsResource.ValuesResource.GetRequest req1 = service.Spreadsheets.Values.Get(SpreadSheetId, "Sheet1!A1");
      IList<IList<Object>> values = req1.Execute().Values;
      Console.WriteLine(values[0][0]);
      Console.ReadKey(true);
    }
  }
}

上記コードを実行すると認証画面が表示されるので、リクエストの許可を行います。

Google_Sheets_API_v4_09

Google_Sheets_API_v4_10

無事に認証が行われると、APIで取得したセルの値が表示されます。

Google_Sheets_API_v4_12

Google_Sheets_API_v4_11

おわりに

そういったわけで、今回はC#からGoogle Sheets API v4を呼び出してみました。
APIの公開後すぐにClient Libraryを用意してくれる、このスピード感はさすがGoogle。
サンプルコードはまだまだ少ないですが、そのうち情報も充実してくるだろうと思います。

なかなか使い勝手が良さそうなAPIですので、興味がある方は一度お試しください。

関連Webページ

Instagram APIをVBAから呼び出してみる。前のページ

カウントダウンタイマーを作成するPowerPointマクロ次のページ

関連記事

  1. Office関連

    Google翻訳の言語自動検出機能を追う

    「Google TTSで文字列を読み上げるマクロ」でGoogle翻訳の…

  2. Google関連

    [Google Apps Script]URL Shortener APIを使って短縮URLを取得す…

    2015/7/14 追記:いつの間にかAPIの呼び出しにAPI…

  3. Google関連

    Google Docs API v1を試してみました。

    下記TechCrunchの記事によると、Google ドキュメントの新…

  4. Google関連

    [Google Apps Script]insertTextBoxメソッドでスライドにテキストボック…

    昨年の9月にスライド上にテキストボックスを挿入するスクリプトについて記…

  5. Google関連

    [Google Apps Script]Google ドライブにある画像をスライドに一括挿入する

    前回の記事では、Google ドライブにある1つの画像ファイルをスライ…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP