0
新建一个excel文件,在文件里A、B栏输入report A的key和value,在D、E栏输入report B的key和value,然后执行下面的命令就行了。
代码
Posted on
Wednesday, February 22, 2017
by
醉·醉·鱼
and labeled under
ruby
背景是,有两个report的金额不平,需要找出里面不平的原因。很早以前是用这个工具去实现的。新建一个excel文件,在文件里A、B栏输入report A的key和value,在D、E栏输入report B的key和value,然后执行下面的命令就行了。
代码
require 'slop'
require 'logger'
require 'simple_xlsx_reader'
# Usage
# ruby discrepancy_finder.rb [options]
# options:
# -v print the version
# -d enable debug mode
# -m model:
# model = 1 check both keys and values
# model = 2 only check keys
# -s sheet index: 0 will be the first sheet
# example:
# ruby discrepancy_finder.rb
# ruby discrepancy_finder.rb -d
# ruby discrepancy_finder.rb -m 2
# ruby discrepancy_finder.rb -d -m 2 -s 0
opts = Slop.parse do |o|
o.integer '-m', '--model', 'model', default: 1
o.integer '-s', '--sheet_index', 'specific sheet number', default: 0
o.bool '-d', '--debug', 'enable debug mode'
o.on '-v', 'print the version' do
puts '0.0.1'
exit
end
end
def logger
@logger ||= Logger.new(STDOUT)
end
def load_col_data(wb, col_index)
_hash = Hash.new
lastrow = 1
while wb.rows[lastrow] && wb.rows[lastrow][col_index]
_hash["#{wb.rows[lastrow][col_index]}"] = (_hash["#{wb.rows[lastrow][col_index]}"].nil? ? 0 : _hash["#{wb.rows[lastrow][col_index]}"]) + ( wb.rows[lastrow][col_index+1].nil? ? 0 : wb.rows[lastrow][col_index+1].to_f.round(2) )
lastrow = lastrow + 1
end
_hash
end
doc = SimpleXlsxReader.open("revenue_report_diff.xlsx")
wb = doc.sheets[opts[:sheet_index]]
logger.debug "Running in model #{opts[:model]}" if opts.debug?
#remove duplicate records
left_hash = load_col_data(wb, 0)
right_hash = load_col_data(wb, 3)
leftSum = left_hash.values.inject(:+).round(2)
rightSum = right_hash.values.inject(:+).round(2)
revenueDiff = leftSum - rightSum
puts '##########REVENUE DIFF(LEFT - RIGHT)##############'
if opts[:model] == 2
puts "LEFT: " + leftSum.to_s
puts "RIGHT: " + rightSum.to_s
puts "DIFF: " + revenueDiff.round(2).to_s
end
lostRecordsInRight = left_hash.keys - right_hash.keys
lostRecordsInLeft = right_hash.keys - left_hash.keys
unless lostRecordsInRight.empty?
puts 'Missing records in RIGHT side:'
lostRecordsInRight.each do |t|
puts t + "\t" + left_hash["#{t}"].round(2).to_s
end
end
unless lostRecordsInLeft.empty?
puts 'Missing records in LEFT side:'
lostRecordsInLeft.each do |t|
puts t + "\t" + right_hash["#{t}"].round(2).to_s
end
end
if opts[:model] == 2
puts 'Discrepancy:'
left_hash.each do |k, v|
if right_hash["#{k}"].nil?
# puts k + " is missing."
elsif left_hash["#{k}"].round(2) != right_hash["#{k}"].round(2)
puts k + "\t" + left_hash["#{k}"].round(2).to_s + "\t" + right_hash["#{k}"].round(2).to_s
end
end
end
Post a Comment