我的代碼如下:
#Creating object with data name properties
$myData = New-Object -TypeName psobject
$myData | Add-Member -MemberType NoteProperty -Name Name -Value $null
$myData | Add-Member -MemberType NoteProperty -Name Bot -Value $null
$myData | Add-Member -MemberType NoteProperty -Name PDD -Value $null
$myData | Add-Member -MemberType NoteProperty -Name SD -Value $null
$myData | Add-Member -MemberType NoteProperty -Name Dev -Value $null
#Empty ArrayList to Populate
$InputArray = @()
for ($i = 2; $i -le $rowMax; $i )
{
$objTemp = $myData | Select-Object *
#Reading row data
$objTemp.Name = $sheet.Cells.Item($i,1).Text
$objTemp.Bot = $sheet.Cells.Item($i,2).Text
$objTemp.PDD = $sheet.Cells.Item($i,3).Text
$objTemp.SD = $sheet.Cells.Item($i,4).Text
$objTemp.Dev = $sheet.Cells.Item($i,5).Text
$InputArray = $objTemp
}
foreach ($i in $InputArray)
{
if ($i.Name -eq $CurrentName) {
#want to convert these name properties to strings
$Name = $i.Name
$Bot = $i.Bot
$PDD = $i.PDD
$Dev = $i.Dev
}
}
上面的代碼使用從 Excel 表中讀取的幾個名稱屬性構建 PSobject 物件。之后,我正在讀取 $InputArray 中的每個 psobject,并針對該索引中當前陣列的屬性。
我遇到的問題是我需要將屬性值(名稱、機器人、PDD、SD、Dev)轉換為字串值。
我嘗試了一些方法無濟于事,任何輸入將不勝感激
uj5u.com熱心網友回復:
[string] $Name = $i.Name$i.Name例如,將存盤in variable的字串化值$Name- 盡管令人驚訝的是,這是必要的,因為您正在訪問.TextExcel 單元格物件上命名的屬性。
通常,在 PowerShell 中有兩種基本的字串化(將值轉換為字串)方法;在以下示例中假設以下兩個變數定義:
# Sample values to stringify.
$val = 1.2 # [double]
$coll = 42, 1.2 # array, containing [int] and [double]
使用 PowerShell 自定義邏輯:culture- invariant,以及用于有意義地對集合進行字串化的附加邏輯,例如陣列:
通過
[string]強制轉換或型別約束:[string] 1.2 # -> "1.2", irrespective of the current culture. # In combination with variables: $valAsString = [string] 1.2 # cast [string] $valAsString = 1.2 # type-constraint; auto-converts future # assignments to [string] too [string] $coll # -> "42 1.2", i.e. the space-concatenated list # of the (themselves stringified) elements.通過可擴展(插值)字串,即內部
"...":# Note: Only simple variable references as used here can *directly* # be embedded in "..."; to embed *expressions or commands*, # you must use $(...), the subexpression operator; e.g.: # "$($val 1)" # -> "2.2" "$val" # same as: [string] $val "$coll" # same as: [string] $coll
通過手頭的 .NET 型別:可能對文化敏感:
明確地,通過它的
.ToString()方法:$val.ToString() # -> "1.2" in English cultures, # "1,2" in French, for instance $coll.ToString() # -> "System.Object[]", i.e. just the *type name*隱式地,通過
-f格式運算子:'val: {0}' -f $val # -> "val: 1.2" in English cultures, # "val: 1,2" in French, for instance
有關更多資訊,請參閱此答案。
另請注意,PowerShell 靈活的型別轉換會按需執行字串化,例如將非字串傳遞給[string]-typed 引數時。
至于你嘗試了什么:
您的代碼可以大大簡化如下;源代碼注釋提供了指標,但解釋每個優化都超出了這個答案的范圍:
# Let PowerShell collect the loop output in an array for you,
$inputArray =
foreach ($i in 2..$rowMax) {
# Construct and output a [pscustomobject] via a literal.
# Note: If desired, you could apply [string] casts *here*; e.g.:
# Name = [string] $sheet.Cells.Item($i,1).Text
[pscustomobject] @{
Name = $sheet.Cells.Item($i,1).Text
Bot = $sheet.Cells.Item($i,2).Text
PDD = $sheet.Cells.Item($i,3).Text
SD = $sheet.Cells.Item($i,4).Text
Dev = $sheet.Cells.Item($i,5).Text
}
}
# ...
foreach ($i in $InputArray) {
if ($i.Name -eq $CurrentName) {
# want to convert these name properties to strings
[string] $Name = $i.Name
[string] $Bot = $i.Bot
[string] $PDD = $i.PDD
[string] $Dev = $i.Dev
}
# ...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/488537.html
標籤:电源外壳
