0
1

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 5 years have passed since last update.

DBの修正前後の結果を比較する

Last updated at Posted at 2020-04-03

やりたいこと

Tsvファイルに出力した結果を使用し、DBの修正前後の差分を出力する。

スクリプト

Tsvファイルを2つ選択し、結果をExcelファイルに出力

TsvFileCompare.ps1
using namespace System.Text;
using namespace System.windows.forms;
using namespace System.Runtime.InteropServices;
using namespace Microsoft.Office.Interop;

Add-Type -AssemblyName "System.windows.forms";
Add-Type -AssemblyName "Microsoft.Office.Interop.Excel";

# -------------------------------
# 比較対象の取得
# -------------------------------
   
# ダイアログ生成
$clsDialog                  = [OpenFileDialog]::New();
$clsDialog.Filter           = "Tsvファイル(*.tsv)|*.tsv"
$clsDialog.InitialDirectory = $PSCommandPath
$clsDialog.Title            = "Tsvファイルを選択してください"
    
# 1つ目のファイル取得 
Write-Host "比較対象の1つ目のファイルを選択してください"
if($clsDialog.ShowDialog() -eq [DialogResult]::OK)
{
    $FilePathA = $clsDialog.FileName
}
else
{
    exit; # 終了
}

# 2つ目のファイル取得 
Write-Host "比較対象の2つ目のファイルを選択してください"
if($clsDialog.ShowDialog() -eq [DialogResult]::OK)
{
    $FilePathB = $clsDialog.FileName
}
else
{
    exit; # 終了
}
    
# -------------------------------
# ファイル比較
# -------------------------------

# データ取得
$FileA = Get-Content $FilePathA
$FileB = Get-Content $FilePathB
    
# ファイル比較
#  $Result = Compare-Object $FileA $FileB -IncludeEqual # 変更のない行も取得
$Result =Compare-Object $FileA $FileB ;

# 差分取得
$DiffA  = @();
$DiffB  = @();
foreach($item in $Result)
{
    # 結果をもとにファイルごとの差分を取得
    switch($item.SideIndicator)
    {
        "=>"{$DiffA  += $item.InputObject} # FileAにあってFileBにない行
        "<="{$DiffB  += $item.InputObject} # FileBにあってFileAにない行
        default{}
    }
}
# 差分なしは終了
if($Result -eq $null)
{
    Write-Host "差分なし"
    Read-Host "終了するにはEnterキーを押してください"
    exit;
}

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

# 1つ目のシート取得
$Sheet = $ExcelApp.WorkSheets.Item(1);

# -------------------------------
# 差分結果を並べて出力
# -------------------------------
# 列数と最大行数の取得
$iColumnCount  = $DiffA[0].Split("`t").Count
$iRowCount     = [Math]::Max($DiffA.Count,$DiffB.Count)
    
# 文字化け回避のため、文字コードをSJISに変更する。
$OutputEncoding = [Console]::OutputEncoding

# クリップボードを使用し値をA1に貼り付け
$Sheet.Activate();
$Sheet.Cells(1,1).Select() | Out-Null;
$DiffA | clip;
$Sheet.paste();
    
# クリップボードを使用し値を隣に貼り付け
$Sheet.Activate();
$Sheet.Cells(1,1 + $iColumnCount).Select() | Out-Null;
$DiffB | clip;
$Sheet.paste();

# 文字コードを戻す
$OutputEncoding = [ASCIIEncoding]::New();
    
# 関数作成
$MinusCount1 = $iColumnCount * 2;
$MinusCount2 = $iColumnCount;
$Formula ="=(INDIRECT(`"RC[-$MinusCount1]`",FALSE)) = (INDIRECT(`"RC[-$MinusCount2]`",FALSE))";

# 差分箇所明瞭化の関数を結果の隣に貼り付け
$PastePosition1 = ($iColumnCount * 2) + 1;
$PastePosition2 = $iColumnCount * 3;
$Sheet.Range($Sheet.Cells(1 ,$PastePosition1),$Sheet.Cells($iRowCount,$PastePosition2)) = $Formula;

# 条件書式追加(関数がFalseとなる項目を強調)
$FormatCondition = $sheet.Range($Sheet.Cells(1 ,$PastePosition1),$Sheet.Cells($iRowCount,$PastePosition2)).FormatConditions.Add([Excel.XlFormatConditionType]::xlCellValue,[Excel.XlFormatConditionOperator]::xlEqual,"false");
$FormatCondition.Font.Bold            = $true; # 太字
$FormatCondition.Interior.ColorIndex  = 6;   # 背景色黄色

# Excel保存
$strPath = Join-Path (Split-Path -Parent $PSCommandPath) "TSV比較結果.xlsx"
$WorkBook.SaveAs($strPath);
Write-Host "結果を出力しました。$strPath";

# Excel解放
$ExcelApp.Quit();
[Marshal]::ReleaseComObject($Sheet)   | Out-Null;
[Marshal]::ReleaseComObject($WorkBook)| Out-Null;
[Marshal]::ReleaseComObject($ExcelApp)| Out-Null;

# 終了確認メッセージ
Read-Host "終了するにはEnterキーを押してください"
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?