PowerShell -Part 7 (584レス)
前次1-
抽出解除 必死チェッカー(本家) (べ) 自ID レス栞 あぼーん

127: 110 2024/03/07(木)22:55 ID:PPl+ScPv(1/4) AAS
Add-Type -AssemblyName System.Windows.Forms

function ConvertShopName([string]$shopName) {
 switch -Wildcard ($shopName) {
  "ファミリーマート*" { $shopName2 = [regex]::Replace($shopName, "(ファミリーマート)([  ]*)(.*)(店)", { $args.groups[1].value + " " + $args.groups[3].value }) }
  "ローソン*" { $shopName2 = [regex]::Replace($shopName, "(ローソン)([  ]*)(.*)", { $args.groups[1].value }) }
  "セブンイレブン*" { $shopName2 = "セブンイレブン" }
  "マクドナルド*" { $shopName2 = "マクドナルド" }
  "すき家*" { $shopName2 = "すき家" }
  "Amazonで注文" { $shopName2 = "AMAZON.CO.JP" }

  default { $shopName2 = $shopName }

 }
 return $shopName2
}

if ($Args.Count -eq 0) {
 [System.Windows.Forms.MessageBox]::Show("引数にファイルを指定してください", "エラー")
 return 0
}

# 家計簿から抽出した csv ファイルを読み込む
$excelData = Import-Csv $Args[0] -Encoding Default -Header "Date", "Shop", "Amount" |
 Select-Object @{ label = 'Date'; expression = { [DateTime]$_.Date} }, Shop, @{ label = 'Amount'; expression = { [int]$_.Amount } }

# 引数で指定されたファイルをフォルダパスとファイル名(拡張子なし)に分離する
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($Args[0])
$folderPath = [System.IO.Path]::GetDirectoryName($Args[0])
128: 110 2024/03/07(木)22:56 ID:PPl+ScPv(2/4) AAS
# ファイル名から日付に変換する
$targetDate = [DateTime]::ParseExact($baseName + "01日","yyyy年MM月dd日", $null)

# 引数と同じフォルダにある Vpass からダウンロードした csv ファイル(ファイル名は翌月) を読み込む
$vpassFile = $folderPath + "\" + $targetDate.AddMonths(1).ToString("yyyyMM") + ".csv"
$vpassData = Import-Csv $vpassFile -Encoding Default -Header "Date", "Shop", "TotalAmount", "Type", "Count", "PaymentAmount", "ShopDetail" |
 Select-Object @{ label = 'Date'; expression = { [DateTime]$_.Date } }, Shop, @{ label = 'TotalAmount'; expression = { [int]$_.TotalAmount } }, Type, Count, @{ label = 'PaymentAmount'; expression = { [int]$_.PaymentAmount } }, ShopDetail

echo "実行します . . ."
# 家計簿データの Shop が "自動引き落とし" で Amount がプラスのデータを削除する
$excelDataList = [System.Collections.ArrayList]$excelData | Where-Object { $_.Shop -ne "自動引き落とし" -and $_.Amount -lt 0 }

# 家計簿データの 2項目目が空のデータ行を削除する
$excelDataList = [System.Collections.ArrayList]$excelData | Where-Object { ![string]::IsNullOrEmpty($_.Shop) }

# 家計簿のマイナスになっている消し込みデータをプラスに変換し、店名をクレジット明細側に変換する
foreach($data in $excelDataList) {
 $data.Amount = $data.Amount * -1
 $data.Shop = ConvertShopName($data.Shop)
}

# vpass データの Type, Count, PaymentAmount, ShopDetail が null のデータを削除する
$vpassData2 = $vpassData | Where-Object { ![string]::IsNullOrEmpty($_.Type) -or ![string]::IsNullOrEmpty($_.Count) -or ![string]::IsNullOrEmpty($_.PaymentAmount) -or ![string]::IsNullOrEmpty($_.ShopDetail) }
129: 110 2024/03/07(木)22:57 ID:PPl+ScPv(3/4) AAS
# vpass の Date, Shop, TotalAmount, Type, Count, Count, ShopDetail が null のデータを削除する
$vpassData3 = $vpassData2 | Where-Object { ![string]::IsNullOrEmpty($_.Date) -or ![string]::IsNullOrEmpty($_.Shop) -or ![string]::IsNullOrEmpty($_.TotalAmount) -or ![string]::IsNullOrEmpty($_.Type) -or ![string]::IsNullOrEmpty($_.Count) -or ![string]::IsNullOrEmpty($_.ShopDetail) }

# 抽出後のデータを格納するための変数
[System.Collections.ArrayList]$vpassDataList = @()
[System.Collections.ArrayList]$unknownhDataList = @()

# vpass データのデータを必要な項目のみにする
foreach($data in $vpassData3) {
 if ( $data.Type -eq "1" -and $data.Count -eq "1" -and $data.TotalAmount -eq $data.PaymentAmount -and $data.ShopDetail -eq "" ) {
  $temp = $data | Select-Object Date, Shop, @{ label = 'Amount'; expression = { [int]$_.TotalAmount } }
  $vpassDataList += $temp
 } elseif ( $data.Type -eq "1" -and $data.Count -eq "1" -and $data.TotalAmount -eq $data.PaymentAmount -and $data.ShopDetail -ne $null ) {
  $temp = $data | Select-Object Date, @{ label = 'Shop'; expression = { $_.ShopDetail } }, @{ label = 'Amount'; expression = { [int]$_.TotalAmount } }
  $vpassDataList += $temp
 } else {
  $unknownhDataList += $data
 }
}

# 重複するオブジェクトを探す
$commonObjects1 = Compare-Object -ReferenceObject $excelDataList -DifferenceObject $vpassDataList -Property Date, Shop, Amount -IncludeEqual | Where-Object { $_.SideIndicator -eq '<=' }
$commonObjects2 = Compare-Object -ReferenceObject $excelDataList -DifferenceObject $vpassDataList -Property Date, Shop, Amount -IncludeEqual | Where-Object { $_.SideIndicator -eq '=>' }
130: 110 2024/03/07(木)23:00 ID:PPl+ScPv(4/4) AAS
Add-Content $Args[0] "`r`n`r`n--------`r`n`r`n"
Add-Content $Args[0] $unknownhDataList
Add-Content $Args[0] "`r`n`r`n"
Add-Content $Args[0] $commonObjects1
Add-Content $Args[0] "`r`n`r`n"
Add-Content $Args[0] $commonObjects2

echo "終了しました。続行するには何かキーを押してください . . ."
$host.UI.RawUI.ReadKey()

-----
とりあえず形にはなりました。ありがとうございました!
前次1-
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ

ぬこの手 ぬこTOP 0.022s