hack のためのネタ帳, etc,,,

オブジェクトのリファレンスマニュアル

上記の Reference に一覧がある。

JScript による基本的な処理のひな形

VBA の例はリファレンスマニュアルに載っているので、WSH (Windows Script Host) の JScript によるサンプルを示す。
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 に一覧がある。

速度の改善

自動計算が ON になっていると、極端に処理が遅いので必要ない場合は極力自動計算を OFF にしたほうが良い。
画面の更新も抑制しておくと良い。

上記の例だと、Excel.calculation, Excel.screenupdating をいじってるところがそれ。

コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Wiki内検索

フリーエリア

管理人/副管理人のみ編集できます