2014-09-01 3 views
0

자동화 된 워드 문서 작업을 수행하고 올바르게 출력 할 수없는 foreach 쿼리에 문제가 발생했습니다. 나는 아래의 데이터가 :PowerShell 및 MS Word 2013 차트, ForEach

Date,  server 1, server 2, server 8, server 12 
20/08/2014, 6.4,  3.8,  5.05,  2.1 

는 기본적으로 첫 번째 줄 이후의 선으로 덮어 도착하고 만 마지막 줄이 존재 :

Date,  server 1, server 2, server 8, server 12 
19/08/2014, 4.24,  5.8,  2.05,  1.0 
20/08/2014, 6.4,  3.8,  5.05,  2.1 

내 스크립트로 출력 유지하지만

.

#### Create Word Document #### 

$word = New-Object -ComObject word.application 
$word.visible = $true 
$doc = $word.documents.add() 
$selection = $word.selection 
$nl = [Environment]::NewLine 

# Create a new chart 
$docChart = $Word.ActiveDocument.InlineShapes.AddChart() 
$docChart.Chart.Type = "1" 
$docChart.chart.SubType = "2" 
$docChart.Chart.ChartStyle = "2" 
$docChart.Chart.Legend.AutoScaleFont = "true" 
$docChart.Chart.HasTitle = "True" 
$docChart.Chart.ChartTitle.Formula = "VM Memory Consumption" 

$cells = $docChart.chart.ChartData.Workbook.ActiveSheet.cells 

$columns = $MyData | Get-Member -MemberType Properties | Select Name 

foreach($column in $columns){ 
    $row=1 
    $col++ 

    $column2 = $column.Name 
    $cells.Item($Row,$col) = "$column2" 

    $MyData | ForEach-Object{ 
     $Row1=2 
     $col 
     $date = $_.c3 
     $array = ($_ | Select-Object $Column2)."$Column2" 

     $Array | ForEach-Object{ 

      $cells.Item($row1,$col) = "$array" 
     } 
    } 
} 

난 정말이 문제를 해결하는 몇 가지 도움을 주셔서 감사합니다 :

아래에있는 내 코드를 참조하십시오.

감사합니다, 스티브

+1

$ Row1 = 2? 그 값을 증가 시켜서는 안됩니까? –

+0

감사합니다. 문제 해결에 도움이되었습니다. 답변은 다음과 같습니다. –

답변

1

은 다음 코드는 기본적으로 내가 2 foreach 루프의 $ ROW1 첫 번째 foreach 루프에서 변수 다음 $ ROW1을 ++ 생성, 내 문제를 해결했다,이 후 예상대로의 데이터를 넣습니다 .

#### Create Word Document #### 

$word = New-Object -ComObject word.application 
$word.visible = $true 
$doc = $word.documents.add() 
$selection = $word.selection 
$nl = [Environment]::NewLine 

# Create a new chart 
$docChart = $Word.ActiveDocument.InlineShapes.AddChart() 
$docChart.Chart.Type = "1" 
$docChart.chart.SubType = "2" 
$docChart.Chart.ChartStyle = "276" 
$docChart.Chart.Legend.AutoScaleFont = "true" 
$docChart.Chart.HasTitle = "True" 
$docChart.Chart.ChartTitle.Formula = "VM Memory Consumption" 
$docChart.chart.Legend.Position = "-4107" 

$cells = $docChart.chart.ChartData.Workbook.ActiveSheet.cells 

$columns = $MyData | Get-Member -MemberType Properties | Select Name 

foreach($column in $columns){ 
    $row=1 
    $col++ 
    $row1=1 
    $column2 = $column.Name 
    $cells.Item($Row,$col) = "$column2" 

    $MyData | ForEach-Object{ 
     $row1++ 
     $date = $_.c3 
     $array = ($_ | Select-Object $Column2)."$Column2" 

     $Array | ForEach-Object{ 
      $cells.Item($row1,$col) = "$array" 
     } 
    } 
}