16
16

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.

ディレクトリ内の全Excelファイルの特定セルをRubyで書き換える

Posted at
# encoding: utf-8

require 'win32ole'

if ARGV.length != 4 then
  abort "引数の数が正しくありません。\nruby edit_iraisho.rb 対象ディレクトリ 対象シート 対象セル 値"
end

@src_dir, @target_sheet, @target_cell, @value = ARGV

@src_dir = @src_dir.gsub(/\\/) {'/'}
p @src_dir
begin
  excel = WIN32OLE.new('Excel.Application')
  excel.visible = false
  
  Dir.glob(@src_dir + '/*.xlsx') do |f|
    f = File.expand_path(f)
    wb = excel.Workbooks.Open(f)
    sh = wb.Worksheets.Item(@target_sheet)
    sh.Range(@target_cell).Value = @value.encode('utf-8', 'cp932')
    wb.Save
    wb.close
  end
ensure
  excel.Quit
end
16
16
3

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?