为单词中 table 的特定单元格着色
Coloring specific cells of a table in word
我目前正在制作 table 并在每个单元格中进行搜索以查找基于该文本的特定文本和颜色单元格。 table 创建通常在不到一秒的时间内快速发生,但是为需要 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=(.*)?}", ', , , , , ') | 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++
}
基本上发生的事情是遍历 table 的每一行并检查第 4 列和第 6 列。它是 "Out of Date" 的单元格和字符“!”。如果单元格包含其中任何一个,颜色将变为黄色或红色。 "| 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 具有字符 !在其中,单元格将显示为黄色。
搜索导入的 CSV,然后根据您在 CSV 中找到的内容更改颜色比搜索 table 快得多。它几乎没有 table 创建本身那么快,但它现在就可以了。这是我更新的代码。
$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=(.*)?}", ', , , , , ') | 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
我目前正在制作 table 并在每个单元格中进行搜索以查找基于该文本的特定文本和颜色单元格。 table 创建通常在不到一秒的时间内快速发生,但是为需要 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=(.*)?}", ', , , , , ') | 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++
}
基本上发生的事情是遍历 table 的每一行并检查第 4 列和第 6 列。它是 "Out of Date" 的单元格和字符“!”。如果单元格包含其中任何一个,颜色将变为黄色或红色。 "| 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 具有字符 !在其中,单元格将显示为黄色。
搜索导入的 CSV,然后根据您在 CSV 中找到的内容更改颜色比搜索 table 快得多。它几乎没有 table 创建本身那么快,但它现在就可以了。这是我更新的代码。
$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=(.*)?}", ', , , , , ') | 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