VBA の例はリファレンスマニュアルに載っているので、WSH (Windows Script Host) の JScript によるサンプルを示す。
wsh_excel_sample.js
JScript は基本的に JavaScript とほぼ同じだし、必要になりそうな要素は上記のスクリプトにおおよそ入れ込んであるので、後は適当にやればおおよそのことは出来るはず。
wsh_excel_sample.js
//------------------------------------------------------------------------------- // XlFileFormat Enumeration // @reference http://msdn.microsoft.com/en-us/library/bb241279.aspx //------------------------------------------------------------------------------- xlWorkbookNormal = -4143; //------------------------------------------------------------------------------- // XlWBATemplate Enumeration // http://msdn.microsoft.com/en-us/library/ff835310.aspx //------------------------------------------------------------------------------- xlWBATWorksheet = -4167; //------------------------------------------------------------------------------- // XlCalculation Enumeration // @reference http://msdn.microsoft.com/en-us/library/bb240978.aspx //------------------------------------------------------------------------------- xlCalculationAutomatic = -4105; xlCalculationManual = -4135; Excel = new ActiveXObject("Excel.Application").Application; // Excel Aplication の ActiveX オブジェクト取得(Excel の起動) Excel.Visible = true; // Visible にしない場合 Window なしで実行される。 var fn = "foo.xlsx"; var args = WScript.Arguments; // コマンドライン引数の取り方 if (0 < args.length) fn = args(0); // 配列ではなくオブジェクトである点に注意([]括弧じゃなくて()括弧) //Excel.Workbooks.Open(fn); // ファイルを開く場合 Excel.Workbooks.Add(xlWBATWorksheet); // ワークブックを作成 Excel.screenupdating = false; // 画面の更新を停止 Excel.calculation = xlCalculationManual; // 自動計算を OFF //var ws1 = Excel.Workbooks(1).Worksheets(1); // ワークシート指定して取得する場合 var ws = Excel.Workbooks(1).ActiveSheet; // アクティブワークシートを取得する場合 ws.Cells(1,1).AddComment(); // 1行1列目にコメントを作成 ws.Cells(2,1).AddComment(ws.Cells(1,1).Comment.Text() + ws.Cells(1,1).Comment.Text()); // コメントの参照 ws.Cells(3,1).Value = ws.Cells(1,1).Comment == null ? "A1 はコメントなし" : "A1 はコメントあり"; // コメントの有無判定と値の設定 ws.Cells(4,1).Formula = ws.Cells(3,1).Comment == null ? "=\"A3 はコメントなし\"" : "=\"A3 はコメントあり\""; // コメントの有無判定と数式の設定 ws.Cells(5,1).Formula = ws.Cells(4,1).Value; // 値の参照 ws.Cells(6,1).Formula = ws.Cells(4,1).Formula; // 数式の参照 ws.Cells(7,1).Value = "fn = \"" + fn + "\""; ws.Cells(8,1).Value = " "; // 次の行で使用範囲を求めるためのダミー ws.Cells(8,1).Value = "全部で" + ws.UsedRange.Rows.Count + "行" + ws.UsedRange.Columns.Count + "列"; Excel.calculation = xlCalculationAutomatic; // 自動計算をON Excel.screenupdating = true; // 画面の更新を再開 //ws.Parent.SaveAs("bar.xlsx", xlWorkbookDefault); // 名前を付けて保存 //ws.Parent.Close(false); // ワークシートを閉じる //Excel.Quit(); // Excel の終了 //WScript.Quite(); // WSH の終了コマンドプロンプトから cscript.exe または wscript.exe の何れかを用いて以下のようにすれば実行出来る。
cscript wsh_excel_sample.js hoge wscript wsh_excel_sample.js hoge実行結果は以下のような感じ。
JScript は基本的に JavaScript とほぼ同じだし、必要になりそうな要素は上記のスクリプトにおおよそ入れ込んであるので、後は適当にやればおおよそのことは出来るはず。
Excel 内でマクロを使う場合は定数は定義済みだが、WSH から Office の API を叩く場合、自前で定義してやる必要がある。
シンボル名と数値の対応は上記の Reference 末尾の Enumerations に一覧がある。
シンボル名と数値の対応は上記の Reference 末尾の Enumerations に一覧がある。
自動計算が ON になっていると、極端に処理が遅いので必要ない場合は極力自動計算を OFF にしたほうが良い。
画面の更新も抑制しておくと良い。
上記の例だと、Excel.calculation, Excel.screenupdating をいじってるところがそれ。
画面の更新も抑制しておくと良い。
上記の例だと、Excel.calculation, Excel.screenupdating をいじってるところがそれ。
タグ
コメントをかく