LoginSignup
0
0

More than 3 years have passed since last update.

Roo / Ruby on Rails Gem 日本語訳(2)

Last updated at Posted at 2021-03-27

今回実務で扱ったRubyonRailのGemRooについてまとめました。
ソースコード: https://github.com/roo-rb/roo

Roo / Ruby on Rails Gem 日本語訳(1)
Roo / Ruby on Rails Gem 日本語訳(3)

投稿動機

→実務で扱ったGemを復習する
→Githubなどのソースコードにビビらないようになりたい
→将来実務で英語を使いたいので、そのポートフォリオの一環として作成したい

また、最後にも記載しますが著作権に関して疎いので、この記事が著作権違反にあたらないと認識して投稿しています。
なので、著作権を侵害するつもりもありませんし、もし著作権違反になるようであればすぐにこの投稿を削除します。

I will mention at the end, I am not familiar with copyright, so I am posting this article recognizing that it does not violate copyright.
So I don't intend to infringe copyright, and if it does, I'll delete this post immediately.

Roo

スプレッドシートの読み取り

each文を使用して、rowを読み取ります。
each文がcolumnの名前を引数としてhashを渡すと、rowにcolumを対応させた形で返します。

sheet.each(id: 'ID', name: 'FULL_NAME') do |hash|
  puts hash.inspect
  # => { id: 1, name: 'John Smith' }
end

:headersオプションを使用するとheaderを含んでメソッドを使用できます

sheet.parse(headers: true)

:header_searchオプションを使用すると、headerと一致したheaderの名前を探せます。

sheet.parse(header_search: [/UPC*SKU/,/ATS*\sATP\s*QTY\z/])

cleanオプションを使用して、制御文字と周囲の空白を取り除きます。

sheet.parse(clean: true)

cellが統合されているドキュメントを開く場合、nilを返さないようにする。

xlsx = Roo::Excelx.new('./roo_error.xlsx', {:expand_merged_ranges => true})

スプレッドシートに書き出す

Rooでは下記のようなシートにデータを書き出すことができます。

sheet.to_csv
sheet.to_matrix
sheet.to_xml
sheet.to_yaml

Excel (xlsx and xlsm)

rowをExcelファイルから取り出す場合

xlsx = Roo::Excelx.new("./test_data/test_small.xlsx")
xlsx.each_row_streaming do |row|
  puts row.inspect # `Excelx::Cell`の配列
end

デフォルトでは空白のcellは配列から弾かれます。もしそのままにしたい場合、pad_cells:オプションを使用します。

xlsx.each_row_streaming(pad_cells: true) do |row|
  puts row.inspect # `Excelx::Cell`の配列
end

もし限られた部分のみのrowを読み込む場合は、offsetオプションを使用します。

xlsx.each_row_streaming(offset: 1) do |row| # `header`である最初の`row`は飛ばします。
  puts row.inspect # `Excelx::Cell`の配列
end
xlsx.each_row_streaming(max_rows: 3) do |row| # 4つの`row`が追加されます。
  puts row.inspect # `Excelx::Cell`の配列
end

rowに対してeach文を繰り返す

xlsx.each_row do |row|
  ...
end

Roo::Excelxは下記のような便利なメソッドを使えます。

xlsx.excelx_type(3, 'C')
# => :numeric_or_formula

xlsx.cell(3, 'C')
# => 600000383.0

xlsx.excelx_value(row,col)
# => '600000383'

xlsx.formatted_value(row,col)
# => '0600000383'

Roo::Excelxは下記のようなタイプにアクセスできます。
celltype, comments, font information, formulas, hyperlinks, labels

xlsx.comment(1,1, ods.sheets[-1])
xlsx.font(1,1).bold?
xlsx.formula('A', 2)

Roo / Ruby on Rails Gem 日本語訳(1)
Roo / Ruby on Rails Gem 日本語訳(3)

最後に

著作権に関して疎いので、この記事が著作権違反にあたらないと認識して投稿しています。
なので、著作権を侵害するつもりもありませんし、もし著作権違反になるようであればすぐにこの投稿を削除します。

I am not familiar with copyright, so I am posting this article recognizing that it does not violate copyright.
So I don't intend to infringe copyright, and if it does, I'll delete this post immediately.

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