2017-08-02 103 views
1

我目前正在製作一個表格並在每個單元格中搜索以根據該文本查找特定的文本和顏色單元格。表格的創建很快發生在不到一秒鐘的時間內,但爲需要清潔的每個單元添加顏色的速度非常緩慢。有沒有更好的方法來做到這一點?着色單詞表中的特定單元格

這是我現在的代碼。

$Word = New-Object -comobject word.application 
$Word.Visible = $true 
$Doc = $Word.Documents.Add() 
$Range = $Doc.Range() 

$text=(Import-CSV "c:\users\user\documents\AIX\Server Owner.csv" -header @("Server name", "Description", "OS", "OS EOL", "SQL", "SQL EOL")) 
$text = $text -replace ",","" 
$newtext = (($text -replace "@{Server name=(.*)?; Description=(.*)?; OS=(.*)?; OS EOL=(.*)?; SQL=(.*)?; SQL EOL=(.*)?}", '$1, $2, $3, $4, $5, $6') | Out-String).trim() 
$newtext 
$Range.Text = "Server name, Description, OS, OS EOL, SQL, SQL EOL`n$newtext" 
$separator=[Microsoft.Office.Interop.Word.WdTableFieldSeparator]::wdSeparateByCommas 
$table=$Range.ConvertToTable($separator) 
$table.AutoFormat([Microsoft.Office.Interop.Word.WdTableFormat]::wdTableFormatNone) 
$Table.Style = "Medium Shading 1 - Accent 1" 

#Adds colours to the table blocks 
#How do I make this faster 

下面這部分是我需要的基本上所發生的事情是它通過表格和支票列4和6,細胞中的每一行,用於「加快

$x = 2 
foreach($l in $text) { 
    if((($Table.Cell($x,4).Range.Text | Out-String) -replace "","").trim() -eq 'Out of date') { 
     $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 255 
    } 
    elseif((($Table.Cell($x,4).Range.Text | Out-String) -replace "","").trim() -like "*!*") { 
     $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 65535 
    } 

    if((($Table.Cell($x,6).Range.Text | Out-String) -replace "","").trim() -eq 'Out of date') { 
     $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 255 
    } 
    elseif((($Table.Cell($x,6).Range.Text | Out-String) -replace "","").trim() -like "*!*") { 
     $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 65535 
    } 
    $x++ 
} 

日期「和字符」!「。如果這些單元格包含其中的任何一種,則顏色會更改爲黃色或紅色。 「| Out-String)-replace」「,」「)。trim()」部分只是爲了確保比較時格式正確。

從導入CSV

"Server name","Server description","Microsoft Windows Server 2008 R2 (64-bit)","14-Jan-2020","Microsoft SQL Server 2008 R2 SP1 Standard","9-Jul-2019 if updated to the latest service pack (SP3)!"

一個例子線採用進口時進口-CSV將目光像

@{Server name=Server name; Description=Server description; OS=Microsoft Windows Server 2008 R2 (64-bit); OS EOL=14-Jan-2020; SQL=Microsoft SQL Server 2008 R2 SP1 Standard; SQL EOL=9-Jul-2019 if updated to the latest service pack (SP3)!;}

而且因爲SQL EOL有性格!在它裏面,細胞會被染成黃色。

+0

你可以發佈從輸入csv的示例嗎? –

+1

IFs對我來說似乎過於複雜,你不能使用'-contains'或'-match'而不用'| ()).replace「」,「」)。trim()' – LotPings

+0

@wgray請將[詳細](https://stackoverflow.com/posts/45469420/edit)支持很多格式化) –

回答

0

搜索導入的CSV文件,然後根據您在CSV中找到的內容更改顏色,比搜索表格要快得多。它幾乎不像桌子創作本身那麼快,但現在會做。這是我更新的代碼。

$Word = New-Object -comobject word.application 
$Word.Visible = $true 
$Doc = $Word.Documents.Add() 
$Range = $Doc.Range() 

$text=(Import-CSV "c:\users\user\documents\AIX\Server Owner.csv" -header @("Server name", "Description", "OS", "OS EOL", "SQL", "SQL EOL")) 
$newtext = $text -replace ",","" 
$newtext = (($newtext -replace "@{Server name=(.*)?; Description=(.*)?; OS=(.*)?; OS EOL=(.*)?; SQL=(.*)?; SQL EOL=(.*)?}", '$1, $2, $3, $4, $5, $6') | Out-String).trim() 
$newtext 
$Range.Text = "Server name, Description, OS, OS EOL, SQL, SQL EOL`n$newtext" 
$separator=[Microsoft.Office.Interop.Word.WdTableFieldSeparator]::wdSeparateByCommas 
$table=$Range.ConvertToTable($separator) 
$table.AutoFormat([Microsoft.Office.Interop.Word.WdTableFormat]::wdTableFormatNone) 
$Table.Style = "Medium Shading 1 - Accent 1" 

#Adds colours to the table blocks 
$x = 2 
foreach($l in $text) { 
    if($l."OS EOL" -like 'Out of date') { 
     $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 255 
    } 
    elseif($l."OS EOL" -like "*!*") { 
     $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 65535 
    } 

    if($l."SQL EOL" -like 'Out of date') { 
     $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 255 
    } 
    elseif($l."SQL EOL" -like "*!*") { 
     $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 65535 
    } 
    $x++ 
} 

Remove-variable x, table, text, newtext, range, separator, word 
相關問題