我尝试使用PowerShell拾取CSV文件,从另一列中减去一列的值并将其放入第三列,然后将CSV打印到默认打印机。
除了数学之外我一切正常。它导入,设置我的标题和打印。但是,似乎没有执行我的merge
来进行数学运算。它运行没有错误。
foreach
有人能告诉我为什么数学部分($hhsignscsv = Import-Csv -Header ("PLU","Description","Quantity","Price","FreqShopType","FreqShopValue","FreqShopPrice","LabelFormat","LabelQTY","SizeMeasurement","Limit") -Path hhsignsmod.csv
foreach ($hhsigns in $hhsignscsv) {
$PLU = $hhsigns.PLU
$Description = $hhsigns.Description
$Quantity = $hhsigns.Quantity
$Price = $hhsigns.Price
$FreqShopType = $hhsigns.FreqShopType
$FreqShopValue = $hhsigns.FreqShopValue
$FreqShopPrice = $hhsigns.FreqShopPrice
$LabelFormat = $hhsigns.LabelFormat
$LabelQTY = $hhsigns.LabelQTY
$SizeMeasurement = $hhsigns.SizeMeasurement
$Limit = $hhsigns.Limit
}
foreach ($hhsigns in $hhsignscsv) {
$FreqShopPrice = $Price - $FreqShopValue
}
Out-Printer -InputObject $hhsignscsv
)不会将这些值放入$FreqShopPrice = $Price - $FreqShopValue
列吗?在调试或运行时,我没有遇到任何语法错误,但在打印输出时,$FreqShopPrice
为空,而不是包含减法的值。
答案 0 :(得分:2)
声明
$FreqShopPrice = $hhsigns.FreqShopPrice
将CSV字段FreqShopPrice
的值复制到变量$FreqShopPrice
。
声明
$FreqShopPrice = $Price - $FreqShopValue
使用变量$FreqShopPrice
和$Price
之间的差异更新变量$FreqShopValue
。但是,由于您在当前循环之前在单独的循环中填充了这些变量,因此它们包含来自CSV的最后一条记录的值。
要实际更新CSV中的字段FreqShopPrice
,您需要这样做:
foreach($hhsigns in $hhsignscsv) {
$hhsigns.FreqShopPrice = $hhsigns.Price - $hhsigns.FreqShopValue
}
删除另一个循环。除了刻录CPU周期外,它没有用处。