image

  • フォト Amazonギフト券
    ※この時計の時刻は、閲覧しているパソコンのものであり、必ずしも正確な時間とは限りません

検索

最近のトラックバック

無料ブログはココログ

« "のぼうの城" を読んだ。 | トップページ | ピックアップ:「空気読んじゃう」新入社員・その傾向と対策, Redmine 0.7.1リリース , etc... »

2008-05-06

ruby で csv, openoffice spreadsheet、google spreadsheets を扱う

roo というライブラリーを見つけた。
- http://roo.rubyforge.org/

> This gem allows you to access the content of
>   Open-office spreadsheets (.ods)
>   Excel spreadsheets (.xls) and
>   Google (online) spreadsheets

sudo gem install roo でインストールできる。(現在のversion は 0.9.4)

サンプルのプログラムの例から2つの例を示す。

$ cat sample00.rb
# See http://roo.rubyforge.org/

require 'rubygems'
require 'roo'

HOURLY_RATE = 123.45

# You can get data from  http://spreadsheets.google.com/pub?key=p7dQJMn9Y4pU59GOICSvNTA
oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
4.upto(oo.last_row) do |line|
  date       = oo.cell(line,'A')
  start_time = oo.cell(line,'B')
  end_time   = oo.cell(line,'C')
  pause      = oo.cell(line,'D')
  sum        = (end_time - start_time) - pause
  comment    = oo.cell(line,'F')
  amount     = sum * HOURLY_RATE
  if date
    puts "#{date}\t#{sum}\t#{amount}\t#{comment}"
  end
end

puts oo.to_csv
puts oo.to_csv('out.csv')

ここでは simple_spreadsheet.ods を読み込んで、 csv 形式で標準出力 と、ファイル out.csv に出力している。
(simple_spreadsheet.ods は http://spreadsheets.google.com/pub?key=p7dQJMn9Y4pU59GOICSvNTA に upload してある。
このデータは NeoOffice で作った。)

google spreadsheet は形式なら write することもできる。

http://spreadsheets.google.com/pub?key=p7dQJMn9Y4pVz5Q1n2n_sPQ

を share 公開してある。これを ruby スクリプトで編集する。

$ cat write_me.rb

# See /opt/local/lib/ruby/gems/1.8/gems/roo-0.9.4/examples/write_me.rb

require 'rubygems'
require 'roo'
require 'yaml'

config = YAML.load_file('../config.yaml')
username = config["username"]
password = config["password"]

#-- create a new spreadsheet within your google-spreadsheets and paste
#-- the 'key' parameter in the spreadsheet URL

#-- See http://spreadsheets.google.com/pub?key=p7dQJMn9Y4pVz5Q1n2n_sPQ
key="p7dQJMn9Y4pVz5Q1n2n_sPQ"

MAXTRIES = 10
print "what's your name? "
my_name = gets.chomp
print "where do you live? "
my_location = gets.chomp
print "your message? (if left blank, only your name and location will be inserted) "
my_message = gets.chomp

spreadsheet = Google.new(key, username, password)
spreadsheet.default_sheet = 'Sheet1'

success = false
MAXTRIES.times do
  col = rand(10)+1
  row = rand(10)+1
#  if spreadsheet.empty?(row,col)
    if my_message.empty?
      text = Time.now.to_s+" "+"Greetings from #{my_name} (#{my_location})"
    else
      text = Time.now.to_s+" "+"#{my_message} from #{my_name} (#{my_location})"
    end
    spreadsheet.set_value(row,col,text)
    puts "message written to row #{row}, column #{col}"
    success = true
    break
#  end
  puts "Row #{row}, column #{col} already occupied, trying again..."
end
puts "no empty cell found within #{MAXTRIES} tries" if !success

ここでは、google アクセスのための username, password は yaml ファイルから読み込むようにした。
#  GOOGLE_MAIL, GOOGLE_PASSWORD を環境変数に設定する方法でも良いらしい。

現状では OpenOffice/Excel データは read はできるが write はできない。
write できるようになればもっと便利になるなぁ。

« "のぼうの城" を読んだ。 | トップページ | ピックアップ:「空気読んじゃう」新入社員・その傾向と対策, Redmine 0.7.1リリース , etc... »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: ruby で csv, openoffice spreadsheet、google spreadsheets を扱う:

« "のぼうの城" を読んだ。 | トップページ | ピックアップ:「空気読んじゃう」新入社員・その傾向と対策, Redmine 0.7.1リリース , etc... »

mokuji

2013年12月
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        

google

  • twitter
  • __
  • _
    Googleボットチェッカー

合わせて読む

  • 合わせて読む
    フィードメーター - katoy: cocolog あわせて読みたい

リンク