function エクセルファイルを開く {
param (
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$ファイルパス,
[switch]$読み取り専用,
[switch]$非表示,
[switch]$ダイアログ
)
# Convert a single string path to an array
if ($ファイルパス.GetType().Name -eq 'String') {
$ファイルパス = @($ファイルパス)
}
# Prompt for file if not found
foreach ($path in $ファイルパス) {
if (-not (Test-Path $path)) {
if ($ダイアログ) {
$selectedFile = (New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [System.IO.Path]::GetDirectoryName($path)
Filter = 'Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|All Files (*.*)|*.*'
}).ShowDialog()
if ($selectedFile -eq [System.Windows.Forms.DialogResult]::OK) {
$path = $dlg.FileName
} else {
throw "ファイルが見つかりませんでした: $path"
}
} else {
throw "ファイルが見つかりませんでした: $path"
}
}
# Open Excel file
try {
$excel = New-Object -ComObject Excel.Application
$excel.Visible = !$非表示.IsPresent
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Open($path, [Type]::Missing, $読み取り専用.IsPresent, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing)
$worksheet = $workbook.Sheets.Item(1)
return $worksheet.UsedRange
}
catch {
Write-Error $_
throw "ファイルを開けませんでした: $path"
}
finally {
$worksheet = $null
$workbook = $null
$excel.Quit()
$excel = $null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
}
}
function エクセルファイルを開く {
param (
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[string[]]$ファイルパス,
[switch]$読み取り専用,
[switch]$非表示,
[switch]$存在しない場合ダイアログ選択,
[switch]$ファイルが見つからない場合エラーメッセージ表示
)
# 単一文字列を配列に変換する
if ($ファイルパス.GetType().Name -eq 'String') {
$ファイルパス = @($ファイルパス)
}
foreach ($ファイル in $ファイルパス) {
# ファイルが見つからない場合はダイアログを表示する
if (-not (Test-Path $ファイル)) {
if ($存在しない場合ダイアログ選択) {
$ダイアログ = New-Object System.Windows.Forms.OpenFileDialog -Property @{
InitialDirectory = [System.IO.Path]::GetDirectoryName($ファイル)
Filter = 'Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|All Files (*.*)|*.*'
}
$選択結果 = $ダイアログ.ShowDialog()
if ($選択結果 -eq [System.Windows.Forms.DialogResult]::OK) {
$ファイル = $ダイアログ.FileName
} else {
if ($ファイルが見つからない場合エラーメッセージ表示) {
throw "ファイルが見つかりません: $ファイル"
}
continue
}
} else {
if ($ファイルが見つからない場合エラーメッセージ表示) {
throw "ファイルが見つかりません: $ファイル"
}
continue
}
}
# Excelファイルを開く
try {
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = !$非表示.IsPresent
$Excel.DisplayAlerts = $false
$wb = $Excel.Workbooks.Open($ファイル, [Type]::Missing, $読み取り専用.IsPresent, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing)
$ws = $wb.Sheets.Item(1)
return $ws.UsedRange
}
catch {
Write-Error $_
throw "ファイルを開けませんでした: $ファイル"
}
finally {
$ws = $null
$wb = $null
$Excel.Quit()
$Excel = $null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
}
}
function Open-ExcelFile {
param (
[Parameter(Mandatory=$false, Position=0, ValueFromPipeline=$true)]
[string[]]$FilePath,
[switch]$ReadOnly,
[switch]$Hidden,
[ValidateSet('ErrorMessage', 'SelectDialog')]
[string]$FileNotFoundBehavior = 'ErrorMessage'
)
# ファイルパスが指定されていない場合はダイアログ表示
if (-not $FilePath) {
$selectedFile = (New-Object System.Windows.Forms.OpenFileDialog -Property @{
Filter = 'Excel Files (*.xls, *.xlsx)|*.xls;*.xlsx|All Files (*.*)|*.*'
}).ShowDialog()
if ($selectedFile -eq [System.Windows.Forms.DialogResult]::OK) {
$FilePath = @($dlg.FileName)
} else {
if ($FileNotFoundBehavior -eq 'SelectDialog') {
throw 'ファイルが選択されていません。'
} else {
Write-Error 'ファイルが選択されていません。'
return
}
}
}
# 複数のファイルパスを渡された場合、先頭のファイルのみ使用する
$FilePath = $FilePath[0]
# ファイルが存在しない場合、ダイアログ表示かエラーメッセージ表示を行う
if (-not (Test-Path $FilePath)) {
if ($FileNotFoundBehavior -eq 'SelectDialog') {
throw "ファイルが見つかりません: $FilePath"
} else {
Write-Error "ファイルが見つかりません: $FilePath"
return
}
}
# Excelファイルを開く
$excel = New-Object -ComObject Excel.Application
$excel.Visible = !$Hidden.IsPresent
$excel.DisplayAlerts = $false
$wb = $excel.Workbooks.Open($FilePath, [Type]::Missing, $ReadOnly.IsPresent, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing, [Type]::Missing)
$ws = $wb.Sheets.Item(1)
$usedRange = $ws.UsedRange
# Excelファイルを閉じる
try {
$ws = $null
$wb.Close()
} catch {
Write-Error $_
throw "Excelファイルを閉じることができませんでした: $FilePath"
} finally {
$wb = $null
$excel.Quit()
$excel = $null
[GC]::Collect()
[GC]::WaitForPendingFinalizers()
}
return $usedRange
}