########## # Excel # # 関数:Excel COMオブジェクト取得 function Get-ExcelApplication { param( [bool]$newObj = $false, [bool]$visible = $true ) try { # newObj が指定された場合は強制的に新規作成へ進む if ($newObj) { throw "ForceNew" } # 起動済み Excel を取得 $excel = [Runtime.InteropServices.Marshal]::GetActiveObject("Excel.Application") } catch { # 起動していない、または取得できない場合は新規作成 $excel = New-Object -ComObject Excel.Application $excel.Visible = $visible } return $excel } ########## # Worksbook # # 関数:Workbook取得 function Get-Workbook { param( [Parameter(Mandatory=$true)] $Excel, # Excel.Application [Parameter(Mandatory=$false)] [string]$Path # 省略可 ) # パスが指定されていない → ActiveWorkbook を返す if (-not $Path) { try { return $Excel.ActiveWorkbook } catch { return $null } } # パスが指定されている場合 $full = (Resolve-Path $Path).ProviderPath $targetName = [System.IO.Path]::GetFileName($full) $foundExact = $null $foundSameName = @() foreach ($wb in $Excel.Workbooks) { try { $wbFull = $wb.FullName $wbName = [System.IO.Path]::GetFileName($wbFull) if ($wbName -eq $targetName) { if ($wbFull -eq $full) { # 同じパス → これを返す $foundExact = $wb } else { # 同名だがパス違い → 危険 $foundSameName += $wbFull } } } catch { # FullName が取れないケースは無視 } } # パス違いの同名ファイルが開かれている場合はエラー if ($foundSameName.Count -gt 0) { throw "同名ファイルが既に開かれています:`n$($foundSameName -join "`n")" } # 同じパスのファイルが開かれているならそれを返す if ($foundExact) { return $foundExact } # どれにも該当しなければ開く return $Excel.Workbooks.Open($full) } # 関数:Workbook作成 function New-Workbook { param( [Parameter(Mandatory=$true)] $Excel # Excel.Application ) return $Excel.Workbooks.Add() } # 関数:Workbook開く function Open-Workbook{ param( [Parameter(Mandatory=$true)] [Excel.Application]$Excel, [Parameter(Mandatory=$true)] [String]$Path, [Parameter(Mandatory=$false)] [Bool]$createFile=$false ) if(Test-Path -Path $Path) { try{ $wb = Get-Workbook -Excel $Excel -Path $Path } catch { Write-Host $_ } } else { if($createFile){ $wb = New-Workbook $Excel $wb.SaveAs( $Path ) } else { Write-Warning "$($Path) は、ありませんでした" } } return $wb } # 関数:Workbook保存 function Save-Workbook { param( [Parameter(Mandatory=$true)] $Workbook, # Excel.Workbook [Parameter(Mandatory=$false)] [string]$Path=$null, [Parameter(Mandatory=$false)] [bool]$Force=$false ) $wb = $Workbook $existFile = if($path){Test-Path -Path $Path}else{$false} $eqFileName = $wb.Path -eq $Path if ([string]::IsNullOrEmpty($wb.Path)) { # まだ保存されていないブック(Book1 など) if($Path) { if(-not( $existFile ) -or $Force){ $wb.SaveAs($Path) } else { Write-Warning "$($Path) は存在しています" throw "$($Path) は存在しています" } } else { Write-Warning "未保存ファイルです(Path未指定)" throw "未保存ファイルです(Path未指定)" } } else { if($path){ if($existFile) { if($eqFileName) { $wb.Save() } else { if($Force){ $wb.SaveAs($Path) } else { Write-Warning "$($Path) ではありません" throw "パス不一致:$($wb.Path) <> $($Path)" } } } else { $wb.SaveAs($Path) } } else { $wb.save() } } } # 関数:Workbook閉じる function Close-Workbook { param( [Parameter(Mandatory=$true)] $Workbook, # Excel.Workbook [Parameter(Mandatory=$false)] [string]$Path=$null, [Parameter(Mandatory=$false)] [bool]$Force=$false ) $wb = $Workbook if(-not $wb.Saved){ try{ Save-Workbook -Workbook $wb -Path $Path -Force $Force } catch { Write-Warning $_ } } $wb.Close($false) if ($wb.Application.Workbooks.Count -eq 0) { $wb.Application.Quit() } } ########## # Worksheet # # 関数:Worksheet取得 function Get-Worksheet { param( [Parameter(Mandatory=$true)] $Workbook, # Excel.Workbook [Parameter(Mandatory=$false)] [string]$Name # シート名(省略可) ) # 引数なし → ActiveSheet を返す if (-not $Name) { try { return $Workbook.ActiveSheet } catch { return $null } } # 名前指定 → 存在すれば返す、なければ $null foreach ($ws in $Workbook.Worksheets) { if ($ws.Name -eq $Name) { return $ws } } return $null } # 関数:Worksheet作成 function New-Worksheet { param( [Parameter(Mandatory=$true)] $Workbook, # Excel.Workbook [Parameter(Mandatory=$false)] [string]$Name # 任意 ) $ws = $Workbook.Worksheets.Add() if ($Name) { try { $ws.Name = $Name } catch { # 名前重複などで設定できない場合は無視 } } return $ws } # 関数:セル範囲を二次元配列化 function Get-RangeArray { param( [Parameter(Mandatory=$true)] $Range ) $v = $Range.Value2 # 1セル → 二次元配列に変換 if ($v -isnot [System.Array]) { return ,(,($v)) } return $v }