diff --git a/doc/excel/stmt_blocks.md b/doc/excel/stmt_blocks.md index 089698b..96381df 100644 --- a/doc/excel/stmt_blocks.md +++ b/doc/excel/stmt_blocks.md @@ -2,13 +2,13 @@ ### begin ステートメント 指定された要素の繰り返し処理を開始します。 -|パラメーター|概要|例|ヘッダー/フッター| -|:-----------|:---|:-|:---------------:| -|fields|フィールド定義を繰り返します。|`{#begin fields}`|〇| -|codes|コード記述を開始します。|`{#begin codes}`|×| -|description|処理記述を開始します。|`{#begin description}`|×| -|assignment|代入編集記述を開始します。|`{#begin assignment}`|〇| -|condition|条件分岐記述を開始します。|`{#begin condition}`|〇| +|パラメーター|概要|例|ヘッダー/フッター|シフト| +|:-----------|:---|:-|:---------------:|:-:| +|fields|フィールド定義を繰り返します。|`{#begin fields}`|〇|×| +|codes|コード記述を開始します。|`{#begin codes}`|×|×| +|description|処理記述を開始します。|`{#begin description}`|×|〇| +|assignment|代入編集記述を開始します。|`{#begin assignment}`|〇|〇| +|condition|条件分岐記述を開始します。|`{#begin condition}`|〇|〇| #### コード記述(codes) コード記述ブロック中(codes)に以下の記述を含める必要があります。 @@ -26,6 +26,7 @@ begin ステートメントでは追加パラメーターを指定できるも |:-----------|:---|:-| |header|ヘッダー行数を指定します。|`{#begin condition header:2}`| |footer|フッター行数を指定します。|`{#begin condition footer:1}`| +|shift|段落右シフトを指定します。|`{#begin description shift:1,5}`| 追加パラメーターは同時に複数指定も可能です。(例:`{#begin fields header:2 footer:1}`) diff --git a/src/main/powershell/ControlStatement.psm1 b/src/main/powershell/ControlStatement.psm1 index 4bed249..cbca70a 100644 --- a/src/main/powershell/ControlStatement.psm1 +++ b/src/main/powershell/ControlStatement.psm1 @@ -73,6 +73,14 @@ class ControlStatement : ControlHolder { [long] $headerLength = 0 # フッター行数 [long] $footerLength = 0 + # シフトステップ + [long] $shiftStep = 0 + # シフト上限 + [long] $shiftLimit = 0 + # シフト開始列 + [long] $shiftColumn = 0 + # シフト列幅 + [long] $shiftWidth = 0 ControlStatement([string[]] $params, $cell) { $this.command = $params[1] @@ -80,10 +88,16 @@ class ControlStatement : ControlHolder { # 拡張パラメーター for ($i = 2; $i -lt $params.Length; ++$i) { - if ($params[$i] -match '^(\w+):(\d+)$') { - switch ($Matches[1]) { - 'header' { $this.headerLength = [long]$Matches[2] } - 'footer' { $this.footerLength = [long]$Matches[2] } + switch -regex ($params[$i]) { + '^header:(\d+)$' { $this.headerLength = [long]$Matches[1] } + '^footer:(\d+)$' { $this.footerLength = [long]$Matches[1] } + '^shift:(\d+),(\d+)(?:,(\d+),(\d+))?$' { + $this.shiftStep = [long]$Matches[1] + $this.shiftLimit = [long]$Matches[2] + if ($Matches.Count -ge 5) { + $this.shiftColumn = [long]$Matches[3] + $this.shiftWidth = [long]$Matches[4] + } } } } @@ -93,6 +107,32 @@ class ControlStatement : ControlHolder { # 基底処理呼び出し ([ControlHolder]$this).Close($params, $cell) + # 定義値チェック + if ($this.headerLength -lt 0) { + Write-Warning "[Close] headerLength: $($this.headerLength)" + $this.headerLength = 0 + } + if ($this.footerLength -lt 0) { + Write-Warning "[Close] footerLength: $($this.footerLength)" + $this.footerLength = 0 + } + if ($this.shiftStep -lt 0) { + Write-Warning "[Close] shiftStep: $($this.shiftStep)" + $this.shiftStep = 0 + } + if ($this.shiftLimit -lt 0) { + Write-Warning "[Close] shiftLimit: $($this.shiftLimit)" + $this.shiftLimit = 0 + } + if ($this.shiftColumn -lt 0) { + Write-Warning "[Close] shiftColumn: $($this.shiftColumn)" + $this.shiftColumn = 0 + } + if ($this.shiftWidth -lt 0) { + Write-Warning "[Close] shiftWidth: $($this.shiftWidth)" + $this.shiftWidth = 0 + } + # 行数算出 $this.length = $cell.Row - $this.row + 1 if ($this.headerLength + $this.footerLength -gt $this.length) { @@ -120,7 +160,9 @@ class ControlStatement : ControlHolder { hidden [void] _AppendHeader([DocumentWriter] $docWriter, $target) { if ($this.headerLength -gt 0) { - $docWriter.Append($this.row + 1, $this.headerLength, $target) + $docWriter.Append($this.row + 1, $this.headerLength, + $this.shiftStep, $this.shiftLimit, $this.shiftColumn, $this.shiftWidth, + $target) } } @@ -130,7 +172,9 @@ class ControlStatement : ControlHolder { # 開始と終了を除く $appendLength = $this.length - $this.headerLength - $this.footerLength - 2 if ($appendLength -gt 0) { - $docWriter.Append($this.row + $this.headerLength + 1, $appendLength, $target) + $docWriter.Append($this.row + $this.headerLength + 1, $appendLength, + $this.shiftStep, $this.shiftLimit, $this.shiftColumn, $this.shiftWidth, + $target) } } else { @@ -142,8 +186,8 @@ class ControlStatement : ControlHolder { hidden [void] _AppendFooter([DocumentWriter] $docWriter, $target) { if ($this.footerLength -gt 0) { $docWriter.Append( - $this.row + $this.length - $this.footerLength - 1, - $this.footerLength, + $this.row + $this.length - $this.footerLength - 1, $this.footerLength, + $this.shiftStep, $this.shiftLimit, $this.shiftColumn, $this.shiftWidth, $target) } } diff --git a/src/main/powershell/DocumentWriter.psm1 b/src/main/powershell/DocumentWriter.psm1 index 9e54b3f..b3e22da 100644 --- a/src/main/powershell/DocumentWriter.psm1 +++ b/src/main/powershell/DocumentWriter.psm1 @@ -156,9 +156,19 @@ class DocumentWriter { # テンプレート側行位置 # .PARAM lineLength # 行数 + # .PARAM shiftStep + # シフトステップ + # .PARAM shiftLimit + # シフト上限 + # .PARAM shiftColumn + # シフト開始列 + # .PARAM shiftWidth + # シフト列幅 # .PARAM target # 出力置換対象 - [void] Append([long] $lineStart, [long] $lineLength, $target) { + [void] Append([long] $lineStart, [long] $lineLength, + [long] $shiftStep, [long] $shiftLimit, [long] $shiftColumn, [long] $shiftWidth, + $target) { if ($lineLength -le 0) { throw (New-Object -TypeName 'System.ArgumentException' ` @@ -171,6 +181,40 @@ class DocumentWriter { [void] $this.sheetDocument.Rows("$($lineDocumentStart):$($lineDocumentStart + $lineLength - 1)").Copy() [void] $this.sheetDocument.Rows("$($this.lineDocument + 1)").Insert($global:const.xlShiftDown) + # シフト処理 + if ($shiftStep -gt 0) { + [long] $offset = ($this.stackParagraph.Count - 1) * $shiftStep + if ($shiftLimit -gt 0) { + $offset = [System.Math]::Min($offset, $shiftLimit) + } + + if ($offset -gt 0) { + [object] $shiftSrcRange = $null + [object] $shiftDestRange = $null + + if (($shiftColumn -gt 0) -and ($shiftWidth -gt 0)) { + # 一部のシフト + $shiftSrcRange = $this.sheetDocument.Range( + $this.sheetDocument.Cells($this.lineDocument + 1, $shiftColumn), + $this.sheetDocument.Cells($this.lineDocument + $lineLength, $shiftColumn + $shiftWidth - 1)) + $shiftDestRange = $this.sheetDocument.Range( + $this.sheetDocument.Cells($this.lineDocument + 1, $shiftColumn + $offset), + $this.sheetDocument.Cells($this.lineDocument + $lineLength, $shiftColumn + $shiftWidth + $offset - 1)) + } + else { + # 行全体のシフト + $shiftSrcRange = $this.sheetDocument.Range( + $this.sheetDocument.Cells($this.lineDocument + 1, 1), + $this.sheetDocument.Cells($this.lineDocument + $lineLength, $global:config.searchColumns)) + $shiftDestRange = $this.sheetDocument.Range( + $this.sheetDocument.Cells($this.lineDocument + 1, $offset + 1), + $this.sheetDocument.Cells($this.lineDocument + $lineLength, $offset + $global:config.searchColumns)) + } + + $shiftSrcRange.Cut($shiftDestRange) + } + } + # 出力置換処理 $this._TranslateLines($lineLength, $target) } diff --git a/template/test.xlsm b/template/test.xlsm index d015dac..4944cad 100644 Binary files a/template/test.xlsm and b/template/test.xlsm differ