LoginSignup
13
13

More than 5 years have passed since last update.

RubyでGoogle Driveにあるドキュメントを扱う

Last updated at Posted at 2014-02-18

gimite/google-drive-rubyを使うと簡単にです。

READMEに基本的な使い方は書いていますが、今回は、ドキュメントを扱ってみます。

やりたいこと

  • ディレクトリの中にあるドキュメントのファイルを扱う。
  • ドキュメントの中身を取得したい。

その前に

Googleアカウントに二段階認証をかけている場合。

アプリケーション固有のパスワードを使用してログインしてください。

実施


require "rubygems"
require "google_drive"

#ログイン
session = GoogleDrive.login('Googleアカウント', 'Googleアカウントのパスワード')

#showfolders => true でフォルダ一覧も含める
session.files('showfolders' => true).each do |d|

  if d.title == 'フォルダのタイトル'
    #documentsでドキュメントの取得
    d.documents.each do |f|
        # ファイルのタイトル
        title = f.title
        # テキストでダウンロードするためのurlを生成
        url = f.document_feed_entry.css('content')[0]['src']
        url += '&exportFormat=txt&format=txt'

        #テキストの取得
        body = session.request(:get, url, :response_type => :raw)
        text = body.force_encoding('UTF-8')
    end
  end
end

参考

コチョナナバ: rubyでgoogle docsをテキストで取得する

13
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
13