機種依存文字をチェックする
微妙に需要があるみたいだったので機種依存文字があるセルをチェックするツールを作ってみた。
- 空白、Latin文字、全角英数記号、JIS第一水準、JIS第二水準はOKとする
- 機種依存文字がみつかったセルの背景色を変える
- 左端に絞り込み用のフラグを書き込み、オートフィルタをかける
使い方:exeにしてデスクトップ等に置き、csvやExcelファイルをドロップすればOK。
checkjis.rb
#! /usr/bin/ruby -Ks require 'excel_lib' require 'rubygems' require 'win32/clipboard' $KCODE = 's' class Excel def initialize(visible = true) @raw_object = connector('Excel.Application') @raw_object.visible = visible if block_given? yield self end end def connector(app_name) begin raw_object = WIN32OLE.connect(app_name) @connected = true rescue raw_object = WIN32OLE.new(app_name) end WIN32OLE.my_const_load(raw_object, self.class) raw_object end class Worksheet def used_range Range.new(@raw_object.usedrange) end end class Range def copy_value self.copy value = Win32::Clipboard.data Win32::Clipboard.empty # 内容を消しておく value.split("\r\n").map do |s| s.split("\t") end end def paste_value(aa) Win32::Clipboard.set_data((aa.map do |a| a.join("\t") end).join("\r\n")) self.worksheet.paste('Destination' => @raw_object) Win32::Clipboard.empty # 内容を消しておく end end end class Excel class CheckJIS JIS_1_2 = %r(\A[ \x20-\x7E # latin \xA1-\xDF # kana -╂ # non kanji 亜-腕 # jis1 弌-滌漾-熙 # jis2 ]*\Z)xo def initialize(file) return unless file && File.exist?(file) @xls = Excel.new @wkb = @xls.open_book(file) check() autofilter() end def check @sht = @wkb.sheets(1) a = @sht.used_range.copy_value b = a.map { |r| r.map { |e| jis_1_2?(e) } } @sht.range('A:A').insert b.each_with_index do |r, y| next if r.all? @sht.cells(y + 1, 1).value = 1 r.each_with_index do |e, x| @sht.cells(y + 1, x + 2).interior.colorIndex = 6 unless e end end end def autofilter @sht.range('A1').autofilter('Field' => 1, 'Criteria1' => '1') end def jis_1_2?(s) JIS_1_2.match(s) ? true : false end end end if __FILE__ == $0 Excel::CheckJIS.new(ARGV[0]) end
rakefile.rb
debug = '' script = 'checkjis.rb' argv = 'sample.xls' task :default => [:run] task :run do ruby %(#{debug} #{script} #{argv}) end task :debug do debug = '-d -r debug' end task :exe do ruby %(-rexerb/mkexy #{script} #{argv}) sh %(exerb -c gui checkjis.exy) end
exeを作るときはrake exeで。