5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby用Google DriveとSpreadsheetのまとめ

Last updated at Posted at 2017-05-07

Drive API V3とSheet API V4をRubyから使用するときのまとめです。
本ページの内容はAPIとしてまとめて、"easy-google-drive"としてRubygemsから公開しました。よければ、そちらをご使用ください。

Drive API

基本情報

  • google driveは、ファイルをidのみで識別しています。そのため、同一フォルダに同じファイル名を存在させることができるようです。
  • 各ファイルが属しているフォルダはparentsのid番号で知ることができます。
  • 一番上の階層は知ることができないようです。わかったら教えてください。

mime_type

mime_typeは、ファイルの属性を示すパラメータで、次のようなタイプがあります。

MIME Type 	| Description |

------------------------|-------------------------------
application/vnd.google-apps.audio | Audio
application/vnd.google-apps.document | Google Docs
application/vnd.google-apps.drawing | Google Drawing
application/vnd.google-apps.file | Google Drive file
application/vnd.google-apps.folder | Google Drive folder
application/vnd.google-apps.form | Google Forms
application/vnd.google-apps.fusiontable | Google Fusion Tables
application/vnd.google-apps.map | Google My Maps
application/vnd.google-apps.photo |
application/vnd.google-apps.presentation | Google Slides
application/vnd.google-apps.script | Google Apps Scripts
application/vnd.google-apps.sites | Google Sites
application/vnd.google-apps.spreadsheet | Google Sheets
application/vnd.google-apps.unknown |
application/vnd.google-apps.video |
application/vnd.google-apps.drive-sdk | 3rd party shortcut

ファイルやディレクトリの取得

ファイルや属性を取得するrubyのプログラムです。

begin
	response = @service.list_files(
		q: "name='readme.txt' and trashed = false",
		spaces: 'drive',
		fields: "nextPageToken, files(id, name, parents,kind,mimeType)",
		page_token: page_token)
	for file in response.files
		ref_file.push(file)
	end
	page_token = response.next_page_token
end while !page_token.nil?

ポイントは次の通りです。

  • "q: ...."の行で、readme.txtを探す、またゴミ箱に入っていないファイルを探しています。name='readme.txt'の部分を削除すれば、すべてのファイルを取得することができます。
  • ファイルは一度に100しか取得できないため、分割して取得する必要があります。そのために、nextPageTokenをフィールドに設定しています。
  • また、同じくfields:の行で、ファイルのid、名前、フォルダを示す"parents"、ファイルタイプを示す"mime_type"を取得しています。
  • 共有ファイルは、parentsのパラメータがないので、それで知ることができます。

ファイルを取得する

file.idが示すファイルをreadme.txtというファイル名で保存する例が以下になります。フォルダを指定しなければ、プログラムのルートフォルダにファイルが保存されます。

@service.get_file(file.id,{download_dest: "readme.txt"})

ファイルを送信する

この例では、src.txtのファイルを、google driveのfolder_idが示すフォルダに、dst.txtというファイル名で保存しています。
"parents"はArray型で設定する必要があります。

file_metadata = Google::Apis::DriveV3::File.new(
	name: "dst.txt",
	mine_type: 'application/vnd.google-apps.unknown',
	parents: [folder_id],
	)
@service.create_file(file_metadata, upload_source: "src.txt", fields: 'id')

ファイルを消去する

delete_fileにidを指定するだけです。

@service.delete_file(file.id)

google spreadsheet

range

データを追加、取得双方ともrangeを設定する必要があります。
rangeは次の書式に従って設定してください。

  • range = "sheet1!A:C"
    sheet1のA:Cにデータを追加、またはデータを取得します。
  • range = "A:C"
    デフォルトのシートのA:Cにデータを追加、または取得します。
  • range = "sheet1!A3:C4"
    sheet1のA3~C4の計6マスにデータを追加、または取得を行います。

spreadsheetの最後にデータを追加する

次のコードは、
1,2,3
4,5,6
というデータを追加するコードです。

value_range_object = {
	majorDimension:"ROWS",
	values: [[1,2,3],[4,5,6],
}
update_res = @service.append_spreadsheet_value(spreadsheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')

spreadsheetのデータを取得する

response = @service.get_spreadsheet_values(spreadsheet_id, range)

別のアカウント、もしくはプロジェクトでログインする場合

認証情報を消去してください。

rm -r ~/.credentials

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?