0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ピボット2

0
Posted at

try {
// Excel アプリケーションを起動(既存があれば接続)
var excelApp = new ActiveXObject("Excel.Application");
excelApp.Visible = true; // true にすると画面表示

// ワークブックを開く(既存のものを使用)
var wb = excelApp.Workbooks.Open("C:\\Users\\ユーザー名\\Documents\\元データ.xlsx");

// シートを取得
var wsData = wb.Sheets("元データ");

// データ範囲の最終行・最終列
var lastRow = wsData.Cells(wsData.Rows.Count, 1).End(-4162).Row; // xlUp = -4162
var lastCol = wsData.Cells(1, wsData.Columns.Count).End(-4159).Column; // xlToLeft = -4159

var rngData = wsData.Range(wsData.Cells(1,1), wsData.Cells(lastRow,lastCol));

// ピボット用シート作成(存在すれば削除)
try { wb.Sheets("ピボット").Delete; } catch(e) {}
var wsPivot = wb.Sheets.Add();
wsPivot.Name = "ピボット";

// PivotCache 作成
var pc = wb.PivotCaches().Create(1, rngData); // 1 = xlDatabase

// ピボットテーブル作成
var pt = pc.CreatePivotTable(wsPivot.Range("A3"), "ピボットテーブル1");

// フィールド設定
pt.PivotFields("商品名").Orientation = 1; // xlRowField = 1
pt.PivotFields("商品名").Position = 1;

pt.PivotFields("地域").Orientation = 2; // xlColumnField = 2
pt.PivotFields("地域").Position = 1;

// 値フィールド追加(合計)
pt.AddDataField(pt.PivotFields("売上"), "合計/売上", -4157); // xlSum = -4157

// 保存して終了
wb.Save();
// wb.Close(); // 必要に応じて
// excelApp.Quit(); // 必要に応じて

WScript.Echo("ピボット作成完了");

} catch(e) {
WScript.Echo("エラー: " + e.message);
}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?