2015-05-22 96 views
0

我有以下部分的PowerShell代碼,它的工作原理完美,但我想知道是否有更簡單的方法來做到這一點。更簡單的方法來做到這一點?

我在Excel中採取不同的範圍和格式有不同的線條樣式和厚度的單元格邊框,使它看起來一個管理報告

TIA 安迪

$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 

回答

0

我看到更好的,更具可讀性很多重複的代碼。這立即讓我認爲你應該使用函數來執行該代碼。輕微的差異可以通過參數來處理。

0

我的下跪反應是增加了一些功能,但後來我意識到你正在設置很多不同的邊界變化(至少看起來是這樣,不知道結果表看起來如何)。用不錯的功能來實際簡化代碼,而不限制未來的更新可能會有挑戰性。簡單地將呼叫包裝在一個函數中不會增加太多價值。

所以,如果我是你,我會添加一個空行,當你移動設置另一個範圍,並稱之爲一天。

0

像這樣的事情會令維護非常快:

$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 
    } 
} 
+1

哪裏'bstyle'功能?你也應該在他們被調用之前聲明你的函數。 – Matt

+0

@Matt,我應該回答嗎?如果你願意,你可以修復代碼 - 答案足夠好。無論如何,這不應該是「讓我做你的家庭作業」。 – majkinetor

相關問題