谁能告诉我为什么下面的Powershell代码导致工作表中的所有单元格都采用“wrapText”样式,而不是仅由range语句指定的单元格?

单元格突出显示仅影响指定的范围,粗体字体也是如此,但是WrapText随处可见!

如果我指定类似:

 $ActiveWorksheet.Range("Q1"),Style.WraptText=$false

它将关闭整个工作表的WrapText。

有没有办法只影响所需的细胞?

谢谢
#Set Up EXCEL Stuff
$ExcelObj = new-Object -comobject excel.application
$ExcelObj.visible = $true
$ExcelObj.DisplayAlerts = $true

$now = get-date -format "yyyyMMddHHss"
$strPath = "Parse_$now.xlsx"

# Create the spreadsheet if it doesnt exist

If (test-Path $strPath) {
    $ActiveWorkbook = $ExcelObj.Workbooks.Open($strPath)
    $ActiveWorkSheet = $ExcelObj.Worksheets.Item(1)
    }
ELSE
    {
#   create file
    $ActiveWorkbook = $ExcelObj.Workbooks.Add()
    $ActiveWorkSheet = $ExcelObj.Worksheets.Item(1)


#   Add headers
    $ActiveWorkSheet.Cells.Item(1,1) = "Date"
    $ActiveWorkSheet.Cells.Item(1,2) = "Time"
    $ActiveWorkSheet.Cells.Item(1,3) = "Channel"
    $ActiveWorkSheet.Cells.Item(1,4) = "Codec"
    $ActiveWorkSheet.Cells.Item(1,5) = "State"
    $ActiveWorkSheet.Cells.Item(1,6) = "Target"
    $ActiveWorkSheet.Cells.Item(1,7) = "Gateway"
    $ActiveWorkSheet.Cells.Item(1,8) = "CFW"
    $ActiveWorkSheet.Cells.Item(1,9) = "DCFW"

# add reminder note - for some reason this gets wrapped
    $ActiveWorkSheet.Cells.Item(10,3) = "NOTE: If this is GW1, change Q-column Formulae from"
    $ActiveWorkSheet.Cells.Item(11,3) = "     N2/(MAX(M2:M20)-0) to N2/(MAX(M2:M20) -  23) in Col Q"

#    $format = $ActiveWorksheet.UsedRange
    $format = $ActiveWorksheet.Range("A1:I1")
    $format.Interior.ColorIndex = 23
    $format.font.ColorIndex = 11
    $format.Font.Bold = "True"

    #Set Summary Headers
    $ActiveWorkSheet.Cells.Item(1,11) = "Day"
    $ActiveWorkSheet.Cells.Item(1,12) = "Hour"
    $ActiveWorkSheet.Cells.Item(1,13) = "Channels"
    $ActiveWorkSheet.Cells.Item(1,14) = "MaxActive"
    $ActiveWorkSheet.Cells.Item(1,15) = "Average Channels Active"
    $ActiveWorkSheet.Cells.Item(1,16) = "Channels Free"
    $ActiveWorkSheet.Cells.Item(1,17) = "Max % Active LD Only"
    #Set Header Format for Summary Data
    $format = $ActiveWorksheet.Range("K1:Q1")
    $format.Interior.ColorIndex = 24
    $format.font.ColorIndex = 11
    #$format.Style.WrapText = "True"
    $ActiveWorksheet.Range("K1:Q1").Style.WrapText = "True"
    $format.Font.Bold = "True"
    #set Percent for max % Active col
    $format = $ActiveWorksheet.Range("Q2:Q50")
    $format.NumberFormat = "0.00%" #aaaa

}

最佳答案

........删除语法.Style

关于excel - Powershell-将 'wrapText'样式应用于某些单元格会导致整个工作表换行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59688287/

10-11 08:51