gpt4 book ai didi

powershell - 脚本 block 中的变量未使用 PowerShell 传递给我的其余代码

转载 作者:行者123 更新时间:2023-12-02 22:55:58 25 4
gpt4 key购买 nike

在我的代码中,$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

最佳答案

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

将您正在创建的这个匿名函数视为函数,并返回值:

$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

等等

关于powershell - 脚本 block 中的变量未使用 PowerShell 传递给我的其余代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53023498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com