在我的代码中,$CoName并不总是完美的,需要稍加调整。$CoFixes修复了这个问题。但是,当我运行它时,如下所示,$CoName从未到达$cell。我需要在$CoFixes中多次重用代码,所以我正在努力学习如何使其工作。
$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){$CoName = "L.F. of 10' Panel w/o lath"}
if ($CoName -eq "L.F. 9' Panel w/o lath"){$CoName = "L.F. of 9' Panel w/o lath"}
if ($CoName -eq "L.F. 8'2`" Panel w/o lath"){$CoName = "L.F. of 8'2`" Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/o lath"){$CoName = "L.F. of 4' Panel w/o lath"}
if ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){$CoName = "L.F. of 4' Panel w/ 8`" top w/o lath"}
if ($CoName -eq 'Special Window Openings over 27"'){$CoName = 'Special Window Openings over 37"'}
if ($CoName -eq 'Door Opening up to 41.5" wide'){$CoName = 'Door Opening up to 41 1/2" wide'}
}
$CoName = $ChangeOrder1Worksheet.range('B20').text
&$CoFixes
$cell = $QuoteSheet.range('B1:B60').Find($CoName).offset(0, 3).address(0,0)
$value = $ChangeOrder1Worksheet.range('A20').text
&$vba
$objExcel.run("ChangeOrder", $cell, $value)
write-host $CoName " " $cell " " $value " " $QuoteSheet.range($cell).text发布于 2018-10-27 15:44:33
你的范围有问题。父作用域中的变量可以从子作用域中访问,但是一旦写入它们,它们就被复制到本地作用域中,这就是您要修改的内容。
将正在创建的匿名函数视为函数,然后返回值:
$CoFixes = {
if ($CoName -eq "L.F. 10' Panel w/o lath"){"L.F. of 10' Panel w/o lath"}
elseif ($CoName -eq "L.F. 9' Panel w/o lath"){"L.F. of 9' Panel w/o lath"}
elseif ($CoName -eq "L.F. 8'2`" Panel w/o lath"){"L.F. of 8'2`" Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/o lath"){"L.F. of 4' Panel w/o lath"}
elseif ($CoName -eq "L.F. 4' Panel w/ 8`" top w/o lath"){"L.F. of 4' Panel w/ 8`" top w/o lath"}
elseif ($CoName -eq 'Special Window Openings over 27"'){'Special Window Openings over 37"'}
elseif ($CoName -eq 'Door Opening up to 41.5" wide'){'Door Opening up to 41 1/2" wide'}
else { $CoName }
}
$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes要使这更符合习惯,请尝试一下开关:
$CoFixes = {
switch($CoName)
{
"L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
"L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
"L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
"L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
"L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
'Special Window Openings over 27"' {'Special Window Openings over 37"'}
'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
default { $Name }
}
}
$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = &$CoFixes然后也许把它放在一个真正的功能中:
function Repair-CoName {
param(
[String]
$Name
)
switch($Name)
{
"L.F. 10' Panel w/o lath" {"L.F. of 10' Panel w/o lath"}
"L.F. 9' Panel w/o lath" {"L.F. of 9' Panel w/o lath"}
"L.F. 8'2`" Panel w/o lath" {"L.F. of 8'2`" Panel w/o lath"}
"L.F. 4' Panel w/o lath" {"L.F. of 4' Panel w/o lath"}
"L.F. 4' Panel w/ 8`" top w/o lath" {"L.F. of 4' Panel w/ 8`" top w/o lath"}
'Special Window Openings over 27"' {'Special Window Openings over 37"'}
'Door Opening up to 41.5" wide' {'Door Opening up to 41 1/2" wide'}
default { $CoName }
}
}
$CoName = $ChangeOrder1Worksheet.range('B20').text
$CoName = Repair-CoName -Name $CoName等。
发布于 2018-10-27 15:40:16
这是因为范围的问题。
$Variable1 = 1
& {
$Variable1 = 2 #This will create a new local variable and visible only inside the scriptblock
"Local variable is $Variable1"
"Global Variable is $Global:Variable1"
}执行以上脚本,您可以很容易地理解。
https://stackoverflow.com/questions/53023498
复制相似问题