Skip to content

Commit

Permalink
出力ファイル列挙処理
Browse files Browse the repository at this point in the history
refs #10
  • Loading branch information
love2hina-net committed Dec 11, 2021
1 parent f6ca032 commit a1397c8
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 67 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"type": "PowerShell",
"request": "launch",
"script": "${workspaceFolder}/src/main/powershell/ganan.ps1",
"args": ["-Debug", "-Verbose", "-ErrorAction Break"],
"args": ["-Path test.xml", "-Template template/test.xlsm", "-OutputDirectory test", "-Debug", "-Verbose", "-ErrorAction Break"],
"cwd": "${workspaceFolder}"
},
{
Expand Down
39 changes: 26 additions & 13 deletions src/main/powershell/DocumentGenerator.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,52 @@ using namespace System.Linq
using namespace System.Xml.XPath

# ドキュメント生成
class DocumentGenerator {
class DocumentGenerator: System.IDisposable {

# テンプレートフォーマット情報
$format = @{ entries = @() }
hidden $format = @{ entries = @() }
# Excel Application
$excel = (New-Object -ComObject 'Excel.Application')
hidden $excel = (New-Object -ComObject 'Excel.Application')
# テンプレートワークブック
$bookTemplate
hidden $bookTemplate
# 生成ドキュメントワークブック
$bookDocument
hidden $bookDocument

# XMLドキュメント
hidden [System.Xml.XmlDocument] $xml
# XPath
hidden [XPathNavigator] $xpath

[void] GenerateDocument([string]$fileXml, [string]$fileTemplate) {

# コード解析XMLファイルを開く
$this.xml = [System.Xml.XmlDocument](Get-Content $fileXml)
$this.xpath = $this.xml.CreateNavigator()
DocumentGenerator([string] $fileTemplate) {

# テンプレートを開く
$this.bookTemplate = $this.excel.Workbooks.Open($fileTemplate, 0, $true)
$this.ParseTemplate()
}

[void] Dispose() {

# テンプレートを閉じる
$this.bookTemplate.Close($false)

# Excelを閉じる
$this.excel.Quit()
$this.excel = $null
}

[void] GenerateDocument([string] $fileXml, [string] $fileExcel) {

# コード解析XMLファイルを開く
$this.xml = [System.Xml.XmlDocument](Get-Content $fileXml)
$this.xpath = $this.xml.CreateNavigator()

# ドキュメントを生成する
$this.bookDocument = $this.excel.Workbooks.Add($global:const.xlWorksheet)
$this.MakeDocument()

$this.bookTemplate.Close($false)
#$this.excel.Quit()
$this.excel = $null
# 保存する
$this.bookDocument.SaveAs($fileExcel)
$this.bookDocument.Close($false)
}

# 制御文解析
Expand Down
43 changes: 23 additions & 20 deletions src/main/powershell/Messages.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
$global:messages = Data {
ConvertFrom-StringData -StringData @'
E001001 = The control statement is not closed correctly.
E002001 = This open was duplicated, cannot be performed on a control statement that has already been opened.
E002002 = Incorrect combination of control statements.
E002003 = Close is invalid for non-nested control statements.
E002004 = The number of header/footer lines exceeds the predefined total number of block lines.
E002005 = Output is invalid for non-nested control statements.
E002006 = The description control statement is not defined in the code control statement.
E002007 = The assignment control statement is not defined in the code control statement.
E002008 = The condition control statement is not defined in the code control statement.
W002001 = The extended parameter value is invalid and will be ignored.
E003001 = Invalid number of lines.
E003002 = Cannot start output for a specified line, because that has already been output.
E003003 = Output cannot be started for rows outbounds the transaction range that have already been started.
E003004 = Cannot operation for a specified line, because that has already been output.
E003005 = The number of lines specified for output is less than one.
E004001 = The search starting point is unknown.
'@
}
$global:messages = Data {
ConvertFrom-StringData -StringData @'
E001001 = The control statement is not closed correctly.
E002001 = This open was duplicated, cannot be performed on a control statement that has already been opened.
E002002 = Incorrect combination of control statements.
E002003 = Close is invalid for non-nested control statements.
E002004 = The number of header/footer lines exceeds the predefined total number of block lines.
E002005 = Output is invalid for non-nested control statements.
E002006 = The description control statement is not defined in the code control statement.
E002007 = The assignment control statement is not defined in the code control statement.
E002008 = The condition control statement is not defined in the code control statement.
W002001 = The extended parameter value is invalid and will be ignored.
E003001 = Invalid number of lines.
E003002 = Cannot start output for a specified line, because that has already been output.
E003003 = Output cannot be started for rows outbounds the transaction range that have already been started.
E003004 = Cannot operation for a specified line, because that has already been output.
E003005 = The number of lines specified for output is less than one.
E004001 = The search starting point is unknown.
E005001 = The specified a template file was not found.
E005002 = The specified output path is not a directory.
E005003 = The specified path is not a file or directory.
'@
}
62 changes: 48 additions & 14 deletions src/main/powershell/ganan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ param(
# ディレクトリを指定した場合、配下のXMLを対象とします。
[Parameter(Mandatory=$true, ParameterSetName="Normal")]
[Parameter(Mandatory=$false, ParameterSetName="Test")]
[string[]]$Path,
[string[]] $Path,

# Excelテンプレートファイルを指定します。
[Parameter(Mandatory=$true, ParameterSetName="Normal")]
[Parameter(Mandatory=$false, ParameterSetName="Test")]
[string]$Template,
[string] $Template,

# 出力先ディレクトリを指定します。
[Parameter(Mandatory=$true, ParameterSetName="Normal")]
[Parameter(Mandatory=$false, ParameterSetName="Test")]
[string] $OutputDirectory,

[Parameter(Mandatory=$false, ParameterSetName="Normal")]
[Parameter(Mandatory=$true, ParameterSetName="Test")]
[Switch]$Test = $false
[Switch] $Test = $false
)

$global:config = @{}
Expand All @@ -50,31 +55,62 @@ Import-LocalizedData -BindingVariable 'messages' -FileName 'Messages'
class GananApplication {

# テンプレートファイル名
[string]$template
[string] $template

# 出力先ディレクトリ
[string] $outputDir

# ファイルリスト
[string[]]$files = @()
[string[]] $files = @()

[void] Initialize() {

# テンプレートチェック
if (![File]::Exists($script:Template)) {
throw (New-Object -TypeName 'System.ArgumentException' `
-ArgumentList ("$($global:messages.E005001) Template:$($script:Template)"))
}

[void] Initialize([string[]]$files) {
# 出力先ディレクトリチェック
if (![Directory]::Exists($script:OutputDirectory)) {
throw (New-Object -TypeName 'System.ArgumentException' `
-ArgumentList ("$($global:messages.E005002) OutputDirectory:$($script:OutputDirectory)"))
}

foreach ($i in $files) {
# 生成対象XMLファイル列挙
$list = [System.Collections.Generic.List[string]]::new()
foreach ($i in $script:Path) {
if ([File]::Exists($i)) {
$this.files += $i
[void]$list.Add($i)
}
elseif ([Directory]::Exists($i)) {

# ディレクトリ内のXMLファイルを対象とする
[void]$list.AddRange([Directory]::EnumerateFiles($i, '*.xml'))
}
else {
throw (New-Object -TypeName 'System.ArgumentException' `
-ArgumentList ("$($global:messages.E005003) Path:$i"))
}
}

$this.template = $script:Template
$this.outputDir = $script:OutputDirectory
$this.files = $list.ToArray()
}

[void] GenerateDocuments() {
# TODO
}

[void] Test() {

$projectRoot = (Convert-Path "$PSScriptRoot\\..\\..\\..")

$generator = [DocumentGenerator]::new()
$generator = [DocumentGenerator]::new("$projectRoot\\template\\test.xlsm")
$generator.excel.Visible = $true

$generator.GenerateDocument("$projectRoot\\test.xml", "$projectRoot\\template\\test.xlsm")
$generator.GenerateDocument("$projectRoot\\test.xml", "$projectRoot\\test\\test.xlsx")
$generator.Dispose()
}

}
Expand All @@ -86,7 +122,5 @@ if ($Test) {
#$app.Test()
}
else {
$app.template = $Template

$app.Initialize($Path)
$app.Initialize()
}
41 changes: 22 additions & 19 deletions src/main/powershell/ja-JP/Messages.psd1
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# culture="ja-JP"
ConvertFrom-StringData @'
E001001 = 制御文が正しく閉じられていません。
E002001 = 既にOpenされている制御文に重複したOpenは実行できません。
E002002 = 制御文の組み合わせが正しくありません。
E002003 = ネストしない制御文に対するCloseは無効です。
E002004 = ヘッダー/フッター行数が定義済みのブロック行数を超えています。
E002005 = ネストしない制御文に対する出力は無効です。
E002006 = コード制御文(codes)中に記述制御文(description)が未定義です。
E002007 = コード制御文(codes)中に代入式制御文(assignment)が未定義です。
E002008 = コード制御文(codes)中に条件制御文(condition)が未定義です。
W002001 = 拡張パラメーター値が不正なため、無視します。
E003001 = 行数が不正です。
E003002 = 既に出力が確定している行に対して、出力を開始できません。
E003003 = 既に開始されているトランザクション範囲外の行に対して、出力を開始できません。
E003004 = 既に出力が確定している行には操作できません。
E003005 = 出力指定行数が1未満です。
E004001 = 検索起点が不明です。
'@
# culture="ja-JP"
ConvertFrom-StringData @'
E001001 = 制御文が正しく閉じられていません。
E002001 = 既にOpenされている制御文に重複したOpenは実行できません。
E002002 = 制御文の組み合わせが正しくありません。
E002003 = ネストしない制御文に対するCloseは無効です。
E002004 = ヘッダー/フッター行数が定義済みのブロック行数を超えています。
E002005 = ネストしない制御文に対する出力は無効です。
E002006 = コード制御文(codes)中に記述制御文(description)が未定義です。
E002007 = コード制御文(codes)中に代入式制御文(assignment)が未定義です。
E002008 = コード制御文(codes)中に条件制御文(condition)が未定義です。
W002001 = 拡張パラメーター値が不正なため、無視します。
E003001 = 行数が不正です。
E003002 = 既に出力が確定している行に対して、出力を開始できません。
E003003 = 既に開始されているトランザクション範囲外の行に対して、出力を開始できません。
E003004 = 既に出力が確定している行には操作できません。
E003005 = 出力指定行数が1未満です。
E004001 = 検索起点が不明です。
E005001 = 指定されたテンプレートファイルがありません。
E005002 = 指定された出力先がディレクトリではありません。
E005003 = 指定されたパスはファイルやディレクトリではありません。
'@

0 comments on commit a1397c8

Please sign in to comment.