Skip to content

Instantly share code, notes, and snippets.

@tokutoku3
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokutoku3/a097d9372ac45eff84fc to your computer and use it in GitHub Desktop.
Save tokutoku3/a097d9372ac45eff84fc to your computer and use it in GitHub Desktop.
Excelの差分を生成する
#!/usr/bin/env ruby
#
# Excelの差分抽出スクリプト
# Excelは.xls形式じゃないと読み込めない
# Spreadsheetが必要なので、無い場合はインストールする
#
# $ sudo gem install spreadsheet
#
# usage: ruby excel_diff.rb <xls filename1> <xls filename2>
#
require 'spreadsheet'
Spreadsheet.client_encoding = 'UTF-8'
DIFF_XLS = './diff.xls'.freeze
class Excel
def initialize(filename, sheet_idx = 0)
sheet = Spreadsheet.open(filename, 'r').worksheet(sheet_idx)
col_cnt = sheet.column_count
@rows = Array.new
sheet.each do |row|
@rows.push(row[0..col_cnt])
end
end
def rows
@rows
end
end
def chk_args
if (ARGV[0].nil? || ARGV[1].nil?) then
puts 'ERROR: invalid args'
exit 1
end
end
def make_diff(original, modified)
rows = original - modified
wb = Spreadsheet::Workbook.new
ws = wb.create_worksheet(:name => 'diff')
rows.each_with_index do |row, idx|
ws.row(idx).replace(row)
end
wb.write(DIFF_XLS)
puts "Generate diff list: #{DIFF_XLS}"
rescue => e
puts "[#{e.class}] #{e.message}"
exit 1
end
chk_args
excel1 = Excel.new(ARGV[0])
excel2 = Excel.new(ARGV[1])
make_diff(excel1.rows, excel2.rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment