Home » excel » excel – Powershell – applying 'wrapText' style to some cells causes the whole sheet to wrap text

excel – Powershell – applying 'wrapText' style to some cells causes the whole sheet to wrap text

Posted by: admin May 14, 2020 Leave a comment

Questions:

Can someone tell me why the Powershell code below causes all cells in the worksheet to take on the ‘wrapText’ style, instead of just the cells specified by range statement?

The cell highlighting is only affecting the specified range, as is the bold font, But WrapText is going everywhere!.

and if I specify something like:

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

It turns off WrapText for the whole sheet.

Is there a way to affect just the desired cells?

thanks

#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

}
How to&Answers:

……..Remove the syntax .Style