2017年2月13日月曜日

Excel用WSFファイルを作成する時に使うサンプル

wshやhtaからExcelをコントロールする際、Excel側で使えるメソッドや定数の値を調べるために「マクロの記録」を使っていますが、利用頻度が高いメソッドや定数を毎回調べるのはムダなので、よく使うものはまとめてみることにしました。


ちなみにこのサンプルを作成する際に使用したのはMS Office Professional Plus 2010のExcelです。それ以前のバージョンでも使用できるのかは確認していません。

定数についてはVBA上では「Excel.xlNone」のような参照もできるのでWSHでも「(new ActiveXObject('Excel.Application')).xlNone」で取得できるかもと予想して試しましたが、ダメでした。
Evalutionメソッドを使ってExcel.Evalution("xlNone")のようなことができないか?とも思いましたが、それもダメでした。

ソース
<job>
<script>
excel=function(){
var app=function(objIns){return objIns.Application}
var wsf=function(objIns){return app(objIns).WorksheetFunction}
var 定数={
xlAutomatic:-4105,
xlBetween:1,
xlCalculationAutomatic:-4105,
xlCalculationManual:-4135,
xlCellValue:1,
xlCenter:-4108,
xlEqual:3,
xlGreater:5,
xlGreaterEqual:7,
xlIMEModeNoControl:0,
xlLastCell:11,
xlLess:6,
xlLessEqual:8,
xlNone:-4142,
xlNotBetween:2,
xlNotEqual:4,
xlPasteValues:-4163,
xlPasteFormats:-4122,
xlPasteAll:-4104,
xlSolid:1,
xlValidAlertStop:1,
xlValidateList:3
}
var num2hex=function(num, 桁数){ var str=num.toString(16), L='length', 桁=桁数||str[L], str0='00000'+str, L0=str0[L]; return str0.slice(L0-桁,L0) }
var RGB=function(R,G,B){return parseInt(num2hex(B,2)+num2hex(G,2)+num2hex(R,2), 16)}
return {
newInstance:function(){return new ActiveXObject('Excel.Application')},
getInstance:function(){
var obj
try{obj=GetObject('','Excel.Application')}catch(e){obj=this.newInstance()}
return obj
},
定数:定数,
wsf:wsf,
RGB:RGB,
読み取り専用で開く:function(objIns, path, updateLinks){return objIns.Workbooks.Open(path, updateLinks, true)},
getLastCell:function(sh){return sh.Cells.SpecialCells(定数.xlLastCell)},
pasteSpecial:function(){
var obj={値のみ:'xlPasteValues', 書式のみ:'xlPasteFormats'}
return function(rng, 貼りつけるもの, sw空白無視){
var paste=定数[obj[貼りつけるもの] || 'xlPasteAll'], operation=定数.xlNone
rng.PasteSpecial(paste, 定数.xlNone, operation, sw空白無視 || false) // 第4引数をtrueにすると行列の入れ替えができる…筈なんだけど4番目の引数を渡すと何故かエラーになる。。
}
}(),
set警告表示:function(objIns, sw){app(objIns).DisplayAlerts = sw},
set画面更新:function(objIns, sw){app(objIns).ScreenUpdating = sw},
set演算:function(objIns, sw){app(objIns).Calculation = sw ? 定数.xlCalculationAutomatic : 定数.xlCalculationManual},
set文字色:function(rng, R, G, B){rng.Font.Color = RGB(R, G, B)},
set背景色:function(rng, sw塗りつぶしなし, R, G, B){
with(rng.Interior){
TintAndShade = 0
PatternTintAndShade = 0
if(sw塗りつぶしなし){
Pattern = 定数.xlNone
return
}
Pattern = 定数.xlSolid
PatternColorIndex = 定数.xlAutomatic
Color = RGB(R, G, B)
}
},
条件付き書式:function(){
var F=function(rng){return rng.FormatConditions}, obj演算子={AとBの間:'xlBetween', Aと等しい:'xlEqual', Aより大きい:'xlGreater', A以上:'xlGreaterEqual', Aより小さい:'xlLess', A以下:'xlLessEqual', AとBの間以外:'xlNotBetween', Aと等しくない:'xlNotEqual'}
return {
削除:function(rng){ F(rng).Delete() },
追加:function(rng, 演算子, A, B){
var f=F(rng), newCon=f.Add(定数.xlCellValue, 定数[obj演算子[演算子]], A, B)
newCon.SetFirstPriority()
newCon.StopIfTrue = false
return newCon
}
}
}(),
入力規則_リスト:function(rng, str){
with(rng.Validation){
Delete()
Add(定数.xlValidateList, 定数.xlValidAlertStop, 定数.xlBetween, str)
IgnoreBlank = true
InCellDropdown = true
InputTitle = ""
ErrorTitle = ""
InputMessage = ""
ErrorMessage = ""
IMEMode = 定数.xlIMEModeNoControl
ShowInput = true
ShowError = true
}
},
setセル書式:function(){
var obj={標準:'G/標準', 数値:'0_ ', 数値小数第4位:'0.0000_ ', 文字列:'@', 年月日:'yyyy"年"m"月"d"日";@'}
return function(rng, 種類, str){ rng. NumberFormatLocal=str || obj[種類 || '標準']}
}(),
セル結合:function(rng){
rng.MergeCells = false
rng.Merge()
},
セル内中央寄せ:function(rng){
rng.HorizontalAlignment = 定数.xlCenter
rng.VerticalAlignment = 定数.xlCenter
}
}
}()
main = function(){
var arg=WScript.Arguments
if(!arg.length){return WScript.Echo('引数なし')}
var objExcel=excel.getInstance(), bk=excel.読み取り専用で開く(objExcel, arg(0)), sh=bk.Sheets(1)
sh.Range('A1').value = 'abc'
sh.Range('B1').value = 'def'
excel.set文字色(sh.Range('A1'), 0xff, 0, 0)
var newCon = excel.条件付き書式.追加(sh.Range('C10'), 'Aと等しくない', 'XX')
excel.set文字色(newCon, 0xff, 0, 0)
excel.set背景色(newCon, false, 0xff, 0xff, 0)
sh.Range('A1:B1').Copy()
excel.pasteSpecial(sh.Range('C10'), '値のみ')
excel.入力規則_リスト(sh.Range('C11'), 'a,b,c')
excel.セル結合(sh.Range('C11:D12'))
excel.セル内中央寄せ(sh.Range('C11:D12'))
WScript.Echo(excel.wsf(objExcel).sum(sh.Cells))
}
main()
</script>
</job>
view raw excel.wsf hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿