简体   繁体   中英

easier way to do this?

I have the following portion of PowerShell code and it works perfectly but I was wondering if there was an easier way of doing this.

I am taking different ranges in Excel and formatting the cell borders with different line styles and thicknesses to make it look better and more readable for a management report

TIA Andy

$formatCells = $ws1.Range("A1:W$a")
$formatCells.select()
$formatCells.font.size=10
$formatCells.Borders.Item($xledgebottom).Weight = $xlThick
$formatCells.Borders.Item($xledgetop).Weight = $xlThick 
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgebottom).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgetop).LineStyle = $xlContinuous 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous 
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous 
$formatCells = $ws1.Range("A2:W2")
$formatCells.select()
$formatCells.Borders.Item($xledgebottom).Weight = $xlThin
$formatCells.Borders.Item($xledgebottom).LineStyle = $xlContinuous
$formatCells = $ws1.Range("A3:W$a")
$formatCells.select()
$formatCells.Borders.Item($xlinsidehorizontal).LineStyle = $xldot
$formatCells.Borders.Item($xlinsidevertical).LineStyle = $xldot
$formatCells.Borders.Item($xlinsidehorizontal).Weight = $xlhairline
$formatCells.Borders.Item($xlinsidevertical).weight = $xlhairline
$formatCells = $ws1.Range("C1:C$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous 
$formatCells = $ws1.Range("F1:F$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous 
$formatCells = $ws1.Range("J1:J$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous 
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick 
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous 
$formatCells = $ws1.Range("N1:N$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick 
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous  
$formatCells = $ws1.Range("R1:R$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick 
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous  
$formatCells = $ws1.Range("V1:V$a")
$formatCells.select()
$formatCells.Borders.Item($xledgeright).Weight = $xlThick 
$formatCells.Borders.Item($xledgeright).LineStyle = $xlContinuous
$formatCells.Borders.Item($xledgeleft).Weight = $xlThick 
$formatCells.Borders.Item($xledgeleft).LineStyle = $xlContinuous  

I see a lot of repeated code. That immediately makes me think you should use functions to carry out that code. Slight differences could be handled with parameters.

My knee-jerk reaction was adding some functions but then I realized you are setting a lot of different variations of the borders (at least that is what it seems, without knowing how the resulting sheet would look like). Coming up with nice function that would actually simplify the code, without restricting future updates would likely be challenging. Simply wrapping the call in a function would not add much value.

So if I were you I would add a blank line when you move to set another range and call it a day.

Something like this will make maintenance very quick:

$r = $ws1.Range("A1:W$a")
bweigth $r  'bottom','top','left','right' thick
bstyle $r  'bottom','top','left','right' continuous

$r = $ws1.Range("A2:W2")
bweight $r bottom thin
bstyle $r bottom continous

$r = $ws1.Range("A3:W$a")
bweight $r 'lineinsidehorizontal','lineinsidevertical' hairline
bstyle $r 'lineinsidehorizontal','lineinsidevertical' dot

function bweight ($range, [string[]]$edge, $value)
{
    $range.select();
    $edge | % {
        $e = get-variable "xl${$_}"
        $v = get-variable "xl${$value}"
        $range.Borders.Item($e).Weight = $v
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM