我目前正在创建一个表并搜索每个单元格,以根据该文本查找特定的文本和颜色单元格。表格创建在不到一秒的时间内快速发生,但是为每个需要sot的单元格添加颜色非常慢。有没有更好的方法呢?
这是我目前的代码。
$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
以下部分是我需要加快的速度
$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++
}
基本上发生的事情是它遍历表格的每一行并检查第4和第6列。它是"过时"的单元格。和角色"!"。如果细胞包含其中任何一种,则颜色变为黄色或红色。 " | 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)!"
使用Import-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 :(得分: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