首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >脚本块中的变量不会使用PowerShell传递给我的其余代码。

脚本块中的变量不会使用PowerShell传递给我的其余代码。
EN

Stack Overflow用户
提问于 2018-10-27 15:32:34
回答 2查看 30关注 0票数 2

在我的代码中,$CoName并不总是完美的,需要稍加调整。$CoFixes修复了这个问题。但是,当我运行它时,如下所示,$CoName从未到达$cell。我需要在$CoFixes中多次重用代码,所以我正在努力学习如何使其工作。

代码语言:javascript
复制
$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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-27 15:44:33

你的范围有问题。父作用域中的变量可以从子作用域中访问,但是一旦写入它们,它们就被复制到本地作用域中,这就是您要修改的内容。

将正在创建的匿名函数视为函数,然后返回值:

代码语言:javascript
复制
$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

要使这更符合习惯,请尝试一下开关:

代码语言:javascript
复制
$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

然后也许把它放在一个真正的功能中:

代码语言:javascript
复制
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

等。

票数 5
EN

Stack Overflow用户

发布于 2018-10-27 15:40:16

这是因为范围的问题。

代码语言:javascript
复制
$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"  
}

执行以上脚本,您可以很容易地理解。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53023498

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档