Powershell 在word中为表的特定单元格着色

Powershell 在word中为表的特定单元格着色,powershell,ms-word,com,Powershell,Ms Word,Com,我目前正在制作一个表格,搜索每个单元格,以找到特定的文本和基于该文本的颜色单元格。表的创建通常在不到一秒钟的时间内快速完成,但为每个需要sot的单元格添加颜色的速度非常慢。有没有更好的办法 这是我当前的代码 $Word = New-Object -comobject word.application $Word.Visible = $true $Doc = $Word.Documents.Add() $Range = $Doc.Range() $text=(Import-CSV "c:\use

我目前正在制作一个表格,搜索每个单元格,以找到特定的文本和基于该文本的颜色单元格。表的创建通常在不到一秒钟的时间内快速完成,但为每个需要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的示例行

“服务器名称”、“服务器描述”、“Microsoft Windows Server 2008 R2(64位)”、“2020年1月14日”、“Microsoft SQL Server 2008 R2 SP1标准”、“2019年7月9日,如果更新到最新的service pack(SP3)!”

当使用导入Csv导入时,它看起来像

@{Server name=Server name;Description=Server Description;OS=Microsoft Windows Server 2008 R2(64位);OS EOL=2020年1月14日;SQL=Microsoft SQL Server 2008 R2 SP1标准;SQL EOL=2019年7月9日,如果更新为最新的service pack(SP3)!;}


因为SQLEOL有这个特性!在其中,单元格将被涂成黄色。

搜索导入的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
而不使用
|Out String)-replace“”)。trim()
@wgray请将详细信息输入到你的原始帖子中(评论不支持很多格式)@MathiasR.Jessenyeah,对此表示抱歉。我现在编辑了这篇文章。就在门口bottom@LotPings仅仅是使用
-就像
一样正常工作。看起来,我在昨晚下班前很快就写了这篇文章,没有太多时间复习。