我使用 PowerShell 中的 Excel com 界面将条件格式应用于特定单元格。这是一个示例摘录。
# Conditional formats
$SafeCheck = $OutputSheet.Range("AI$i")
$SafeCheck.FormatConditions.Add($xlCellValue, $xlEqual, "TRUE") #good
$SafeCheck.FormatConditions.Add($xlCellValue, $xlLess, 100) #good
$SafeCheck.FormatConditions.Add($xlCellValue, $xlLess, 0) #warning
$SafeCheck.FormatConditions.Add($xlCellValue, $xlEqual, "FALSE")#bad
$SafeCheck.FormatConditions.Add($xlCellValue, $xlLess, -100) #bad
$SafeCheck.FormatConditions.Add($xlCellValue, $xlGreater, 100) #bad
$SafeCheck.FormatConditions.Item(1).Interior.Color = $CGood
$SafeCheck.FormatConditions.Item(1).Font.Color = $FGood
$SafeCheck.FormatConditions.Item(2).Interior.Color = $CGood
$SafeCheck.FormatConditions.Item(2).Font.Color = $FGood
$SafeCheck.FormatConditions.Item(3).Interior.Color = $CWarn
$SafeCheck.FormatConditions.Item(3).Font.Color = $FWarn
$SafeCheck.FormatConditions.Item(4).Interior.Color = $CBad
$SafeCheck.FormatConditions.Item(4).Font.Color = $FBad
$SafeCheck.FormatConditions.Item(5).Interior.Color = $CBad
$SafeCheck.FormatConditions.Item(5).Font.Color = $FBad
$SafeCheck.FormatConditions.Item(6).Interior.Color = $CBad
$SafeCheck.FormatConditions.Item(6).Font.Color = $FBad
目前它可以工作,但即使只修改列中的 30 行,速度也非常慢。它似乎花费了大量时间来收集和显示成功消息。像这样。
Application : Microsoft.Office.Interop.Excel.ApplicationClass
Creator : 1480803660
Parent : System.__ComObject
Type : 1
Operator : 3
Formula1 : =TRUE
Formula2 :
Interior : System.__ComObject
Borders : System.__ComObject
Font : System.__ComObject
Text :
TextOperator :
DateOperator :
NumberFormat :
Priority : 19
StopIfTrue : True
AppliesTo : System.__ComObject
PTCondition : False
ScopeType :
Application : Microsoft.Office.Interop.Excel.ApplicationClass
Creator : 1480803660
Parent : System.__ComObject
Type : 1
Operator : 6
Formula1 : =100
Formula2 :
Interior : System.__ComObject
Borders : System.__ComObject
Font : System.__ComObject
Text :
TextOperator :
DateOperator :
NumberFormat :
Priority : 20
StopIfTrue : True
AppliesTo : System.__ComObject
PTCondition : False
ScopeType :
有没有办法加快速度?我预计它会运行得很快。它可以工作,但运行速度非常慢。
我最终将格式设置为一行,然后使用
$OutputSheet.Range($StartRange + "2:" + $EndRange + "2").copy() $OutputSheet.Range($StartRange + $i + ":" + $EndRange + $i).Select() $OutputSheet.Range($StartRange + $i + ":" + $EndRange + $i).PasteSpecial(-4122)
复制并粘贴它。快多了。接下来我会尝试@iRon的建议。