Skip to content

Commit

Permalink
段落シフトの実装
Browse files Browse the repository at this point in the history
resolved #7
  • Loading branch information
love2hina-net committed Aug 21, 2021
1 parent 78d721b commit b71e9a5
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 16 deletions.
15 changes: 8 additions & 7 deletions doc/excel/stmt_blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)に以下の記述を含める必要があります。
Expand All @@ -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}`)

Expand Down
60 changes: 52 additions & 8 deletions src/main/powershell/ControlStatement.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,31 @@ 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]
$this.row = $cell.Row

# 拡張パラメーター
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]
}
}
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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 {
Expand All @@ -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)
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/main/powershell/DocumentWriter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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' `
Expand All @@ -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)
}
Expand Down
Binary file modified template/test.xlsm
Binary file not shown.

0 comments on commit b71e9a5

Please sign in to comment.