Created
June 24, 2014 23:07
-
-
Save TearTheSky/f5d39df867b278eb5555 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#引数定義 | |
Param ( | |
[string]$pathName = "", | |
[int]$targetSheetNumber = -1, | |
[string]$targetCollumn = "", | |
[int]$targetRow = -1 | |
) | |
#****************************************************************************** | |
# 機能 : Excel起動 | |
# | |
# 機能説明 : Excelを起動する。 | |
# | |
# 引数 : なし | |
# | |
# 戻り値 : Excelオブジェクト | |
#****************************************************************************** | |
function OpenExcel | |
{ | |
[void] ([Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")) | |
$excel = New-Object "Microsoft.Office.Interop.Excel.ApplicationClass" | |
return $excel | |
} | |
#****************************************************************************** | |
# 機能 : ワークブック起動 | |
# | |
# 機能説明 : 指定したExcelファイルを開く。 | |
# | |
# 引数 : $excel Excelオブジェクト | |
# $filePath Excelファイルのフルパスファイル名 | |
# | |
# 戻り値 : ワークブックオブジェクト | |
#****************************************************************************** | |
function OpenWorkbook($excel, $filePath) | |
{ | |
$workbook = $excel.Workbooks.Open($filePath) | |
return $workbook | |
} | |
#****************************************************************************** | |
# 機能 : Excel終了 | |
# | |
# 機能説明 : Excelを終了する。 | |
# | |
# 引数 : $excel Excelオブジェクト | |
# | |
# 戻り値 : なし | |
#****************************************************************************** | |
function CloseExcel($excel) | |
{ | |
if ($excel -ne $null) | |
{ | |
$excel.Quit() | |
$excel = $null | |
[GC]::Collect([GC]::MaxGeneration) | |
} | |
return | |
} | |
#****************************************************************************** | |
# 機能 : ワークブック終了 | |
# | |
# 機能説明 : 指定したワークブックを閉じる。 | |
# | |
# 引数 : $workbook ワークブックオブジェクト | |
# | |
# 戻り値 : なし | |
#****************************************************************************** | |
function CloseWorkbook($workbook) | |
{ | |
if ($workbook -ne $null) { | |
$workbook.Close($false) | |
$workbook = $null | |
} | |
return | |
} | |
#****************************************************************************** | |
# 機能 : Excelカラム番号変換 | |
# | |
# 機能説明 : Excelカラム名を番号へ変換する。 | |
# | |
# 引数 : $columnName Excelカラム名 | |
# | |
# 戻り値 : カラム番号 | |
#****************************************************************************** | |
function ConvertExcelColumnNo($columnName) | |
{ | |
$columnValue = 0 | |
for ($i = 0; $i -lt $columnName.Length; $i++) | |
{ | |
$columnValue += ((([int] $columnName.ToUpper().Chars($i)) - 64) ` | |
* ([math]::pow(26, $columnName.Length - $i - 1))) | |
} | |
return [int]$columnValue | |
} | |
#****************************************************************************** | |
# 機能 : 項目値読み取り | |
# | |
# 機能説明 : 指定したシート、セル(単一項目)の設定値を読み取る。 | |
# | |
# 引数 : $workbook ワークブックオブジェクト | |
# $sheetNo シート番号(1オリジン) | |
# $rowNo 行番号(1オリジン) | |
# $columnName 列名 | |
# | |
# 戻り値 : Excelセル設定値 | |
#****************************************************************************** | |
function ReadExcelItem($workbook, $sheetNo, $rowNo, $columnName) | |
{ | |
$worksheet = $workbook.Worksheets.Item($sheetNo) | |
$columnNo = ConvertExcelColumnNo $columnName | |
$value = $worksheet.Cells.Item($rowNo, $columnNo).Text.Trim() | |
$worksheet = $null | |
return $value | |
trap { | |
Write-Debug "ReadExcelItem():exception" | |
$worksheet = $null | |
break | |
} | |
} | |
#----------------------------------------------------------------------------------------------------- | |
# - - - 処理の流れについて - - - # | |
#全部のExcelファイルを取得する(.xlsと.xlsx) | |
#各ファイルについて | |
# 開く | |
#指定されたセルの値を取得する | |
#その値を返す | |
#キーが0個の連想配列を作成しておく | |
#戻ってきた値について | |
#0ならキーを追加する | |
#キーに対して値を1増やす | |
#配列のキーが1個以上なら | |
#そのキーがすでに存在するかを確認する | |
#存在していたらそのキーに対して値を1増やす | |
#キーがが存在しないならキーを追加する | |
#そのキーに対して値を1増やす | |
# 最後にわかりやすく標準出力する。 | |
#----------------------------------------------------------------------------------------------------- | |
if($pathName -eq "") { | |
#フォルダ選択ダイアログの表示 | |
$shell = New-Object -com Shell.Application | |
$folderPath = $shell.BrowseForFolder(0,"対象フォルダーを選択してください",0,"0") | |
if($folderPath -eq $null){exit} | |
$myFolderPath = $folderPath.Self.Path | |
} | |
else { | |
[string]$myFolderPath = $pathName | |
} | |
#変数準備 | |
$excelObject = OpenExcel | |
$excelFiles = @() | |
$answers = @{} | |
#Excelファイルを取得 | |
Set-Location $myFolderPath | |
$excelFiles += Get-ChildItem -Name "*.xlsx" | |
$excelFiles += Get-ChildItem -Name "*.xls" | |
# 取得対象セルを特定 | |
if($targetSheetNumber -eq -1) { | |
Write-Output "値を知りたいセルがあるシートの番号(左から数えて何個目のシートか、たとえば3個目なら3)を入力してください。" | |
[int]$targetSheetNumber = Read-Host | |
} | |
if($targetCollumn -eq "") { | |
Write-Output "値を知りたいセルの列名(A5ならA)を入力してください。" | |
[string]$targetCollumn = Read-Host | |
} | |
if($targetRow -eq -1) { | |
Write-Output "値を知りたいセルの行名(A5なら5)を入力してください。" | |
[int]$targetRow = Read-Host | |
} | |
#各Excelファイルのそのセルから値を取得 | |
foreach($aFile in $excelFiles) { | |
$filePath = ( [string](Get-Location)+ "\" +$aFile) | |
$workbookObject = OpenWorkbook $excelObject $filePath | |
$targetValue = ReadExcelItem $workbookObject $targetSheetNumber $targetRow $targetCollumn | |
if( ($answers.Contains($targetValue) ) -eq $false ) { | |
$answers.Add($targetValue, 1) | |
} | |
else { | |
$answers[$targetValue] += 1 | |
} | |
CloseWorkbook | |
} | |
CloseExcel | |
$answers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment