着色单词表中的特定单元格
我目前正在制作一个表格并在每个单元格中搜索以根据该文本查找特定的文本和颜色单元格。表格的创建很快发生在不到一秒钟的时间内,但为需要清洁的每个单元添加颜色的速度非常缓慢。有没有更好的方法来做到这一点?着色单词表中的特定单元格
这是我现在的代码。
$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有性格!在它里面,细胞会被染成黄色。
搜索导入的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
你可以发布从输入csv的示例吗? –
IFs对我来说似乎过于复杂,你不能使用'-contains'或'-match'而不用'| ()).replace“”,“”)。trim()' – LotPings
@wgray请将[详细](https://stackoverflow.com/posts/45469420/edit)支持很多格式化) –