2015-05-22 55 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

相关问题