0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellでExcelの条件付き書式を設定する

Last updated at Posted at 2020-04-06

PowerShellでExcelの条件書式を追加する処理のメモ

スクリプト

ExcelFormatConditions.ps1
using namespace System.Runtime.InteropServices;
using namespace Microsoft.Office.Interop;

# -------------------------------
# Excel起動
# -------------------------------
Add-Type -AssemblyName "Microsoft.Office.Interop.Excel";

$ExcelApp               = New-Object -ComObject Excel.Application;
$ExcelApp.Visible       = $true;                      # Excel画面表示
$ExcelApp.DisplayAlerts = $false;                     # 警告メッセージ非表示
$WorkBook               = $ExcelApp.Workbooks.Add();  # Bookの追加処理

# -------------------------------
# テストデータ作成
# -------------------------------
# 1つ目のシート取得
$Sheet = $ExcelApp.WorkSheets.Item(1);

# 適当な値を設定
$Sheet.Range("A1:D5") = "=ROW() + COLUMN()";

# -------------------------------
# 条件書式の追加
# -------------------------------
# 書式追加1(3と一致するものに罫線)
$FormatCondition1 = $sheet.Range("A1:D5").FormatConditions.Add([Excel.XlFormatConditionType]::xlCellValue,[Excel.XlFormatConditionOperator]::xlEqual,3);
$FormatCondition1.Borders.LineStyle   = [Excel.XlLineStyle]::xlContinuous; # 罫線

# 書式追加2(5~7の間となるものを文字・背景色変更)
$FormatCondition2 = $sheet.Range("A1:D5").FormatConditions.Add([Excel.XlFormatConditionType]::xlCellValue,[Excel.XlFormatConditionOperator]::xlBetween,5,7); 
$FormatCondition2.Font.Bold            = $true; # 太字
$FormatCondition2.Interior.ColorIndex  = 6;     # 背景色黄色
$FormatCondition2.Font.ColorIndex      = 3;     # 文字色赤

# -------------------------------
# 条件書式の削除
# -------------------------------
# 1つ目の書式削除
$sheet.Range("A1:D5").FormatConditions(1).Delete(); 
# 全て削除
$sheet.Range("A1:D5").FormatConditions.Delete();

# -------------------------------
# 解放処理
# -------------------------------
$ExcelApp.Quit();
[Marshal]::ReleaseComObject($Sheet);
[Marshal]::ReleaseComObject($WorkBook);
[Marshal]::ReleaseComObject($ExcelApp);

引数の設定値

  • 1つ目の引数(XlFormatConditionType)
定数名 説明
xlCellValue 1 セルの値が
xlExpression 2 数式が
xlColorScale 3 カラー スケール
xlDatabar 4 データバー
xlTop10 5 上から 10 個の値
xlIconSets 6 アイコン セット
xlUniqueValues 8 一意の値
xlTextString 9 テキスト文字列
xlBlanksCondition 10 空白の条件
xlTimePeriod 11 期間
xlAboveAverageCondition 12 平均以上の条件
xlNoBlanksCondition 13 空白の条件なし
xlErrorsCondition 16 エラー条件
xlNoErrorsCondition 17 エラー条件なし
  • 2つ目の引数(XlFormatConditionOperator)
定数名 説明
xlBetween 1 次の値の間
xlNotBetween 2 次の値の間以外
xlEqual 3 次の値に等しい
xlNotEqual 4 次の値に等しくない
xlGreater 5 次の値より大きい
xlLess 6 次の値より小さい
xlGreaterEqual 7 次の値以上
xlLessEqual 8 次の値以下

3つ目と4つ目の引数は他の引数に合わせて設定。
(参考:XlFormatConditionType
(参考:XlFormatConditionOperator

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?