Created
July 3, 2012 05:02
-
-
Save ohtake/3037796 to your computer and use it in GitHub Desktop.
Combine green chiku-kanji
This file contains 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
# テーブルオブジェクト内のデータを結合して保存する | |
# | |
# スクリプト中でクリップボードを使うので、スクリプト実行中はクリップボードを操作してはだめ。 | |
# 最後の保存のところが UNC のままだと通らないので、ネットワークドライブとしてマウントしておくこと。 | |
ls -Filter 2012?.xlsx |% -Begin { | |
$excel = New-Object -ComObject Excel.Application | |
$excel.Visible = $true | |
$newBook = $excel.Workbooks.Add() | |
$destSheet = $newBook.Sheets.Item(1) | |
$nextPasteRow = 1 | |
} -Process { | |
$book = $excel.Workbooks.Open($_.Fullname) | |
foreach($i in 1..$book.Sheets.Count) { | |
$sheet = $book.Sheets.Item($i) | |
foreach($j in 1..$sheet.ListObjects.Count) { | |
$obj = $sheet.ListObjects.Item($j) | |
if($obj.Name -eq "テーブル1") { | |
$tableRows = $obj.ListRows | |
$firstRowRange = $tableRows.Item(1).Range | |
$lastRowRange = $tableRows.Item($tableRows.Count).Range | |
$cellUpperLeft = $sheet.Cells.Item($firstRowRange.Row, $firstRowRange.Column) | |
$cellLowerRight = $sheet.Cells.Item($lastRowRange.Row, $lastRowRange.Columns.Item($lastRowRange.Columns.Count).Column) | |
$rangeData = $sheet.Range($cellUpperLeft, $cellLowerRight) | |
$rangeData.Copy() | Out-Null | |
$destSheet.Paste($destSheet.Cells.Item($nextPasteRow, 1)) | |
$nextPasteRow += $rangeData.Rows.Count | |
} | |
} | |
} | |
# http://support.microsoft.com/kb/287392 | |
# Method 2 が PowerShell でできなさそうなので method 1 で | |
$book.ActiveSheet.Range("A1").Copy() | Out-Null | |
$book.Close() | |
} -End { | |
$newBook.SaveAs((Join-Path (pwd) ("combined-" + (Get-Date).ToString("yyyyMMdd'T'HHmm") + ".xlsx")), [Microsoft.Office.Interop.Excel.XlFileFormat]::xlOpenXMLWorkbook) | |
$excel.Quit() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment