我想使用powershell在假设&#x27中减去数值(在.csv文件中)。 B2'从相邻的单元格中说' c2'

发布于 2025-02-13 18:48:16 字数 994 浏览 1 评论 0原文

我想使用powerShell在相邻单元格中说“ e2”中的数值(在.csv文件中)减去一个数值,却说“ d2”,但前提应在“ f2”中显示,如果“ a2”不以字母C开头,则该字母c将“ d2”复制到'f2',

这应该在以下行

Serial         Date        Mono    Colour  Spotcol  result
CNFL64383      07/07/2022  3994    19990   2167     17823
74648160200FK  07/07/2022  771941  0       0        0
VNF4J20449     07/07/2022                           0
CNJL40076      07/07/2022  6369    3602    762      2840
CNJK65021      07/07/2022  19431   10444   839      9605
CNB3Q1NBJG     07/07/2022  199     171     0        171
75280520H5VMT  07/07/2022  6532    12715   622      12715

之前发生:

之后:

任何帮助都将不胜感激,因为这已经在我的大脑中炸了一段时间:)

i want to use Powershell to subtract a numeric value (in a .csv file) in lets say ' E2' from an adjacent cell lets say 'D2' but only if the text in 'A2' starts with a letter C. the sum result should be displayed in 'F2', if 'A2' does not start with the letter C it copies cell 'D2' to 'F2'

this should happen for all following rows

Serial         Date        Mono    Colour  Spotcol  result
CNFL64383      07/07/2022  3994    19990   2167     17823
74648160200FK  07/07/2022  771941  0       0        0
VNF4J20449     07/07/2022                           0
CNJL40076      07/07/2022  6369    3602    762      2840
CNJK65021      07/07/2022  19431   10444   839      9605
CNB3Q1NBJG     07/07/2022  199     171     0        171
75280520H5VMT  07/07/2022  6532    12715   622      12715

Before:

enter image description here

After:

enter image description here

any help would be greatly appreciated as this has been frying my brain for some time now :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2025-02-20 18:48:16

如果您尝试自己执行此操作,则应发布您的代码,以便我们可以帮助调试它。

您需要使用Import-CSV将数据输入PS。这将为您提供一系列PS对象。从这里,您可以循环遍历每个对象,并执行所需的计算。

$data = Import-Csv -Path C:\temp\test.csv

$data | Format-Table

foreach ($datum in $data) {
    if ($datum.Serial.StartsWith('C')) {
        $datum.result = $datum.Colour - $datum.Spotcol
    }

    else {
        if ($datum.Colour) { $datum.result = $datum.Colour }
        else { $datum.result = 0 }
    }
}

$data | Format-Table

让我们调用$ data数组<代码> $ datum 的每次迭代。您的D列标题为$ datum.colour,e列的标题为$ datum.spotcol,列的标题为$ datum.serial.serial。这是因为数组的每个对象都有列标题为属性名称,而不是Excel的A2 D2东西。

在每次迭代中,如果$ datum.Serial [a2]以“ C”开头,我们做$ datum.colour [d2] - $ datum.spotcol < /code> [e2]并将其分配给$ datum.result [f2]。否则,我们制作$ datum.result [f2] $ datum.colour [d2]的值。

编辑:

进行语法更改($ data | ft to $ data |格式 - 桌面

If you have tried to do it on your own, you should post your code so we can help debug it.

You need to use Import-Csv to get the data into PS. This will give you an array of PS Objects. From here, you can loop through each object and do what calculations you need.

$data = Import-Csv -Path C:\temp\test.csv

$data | Format-Table

foreach ($datum in $data) {
    if ($datum.Serial.StartsWith('C')) {
        $datum.result = $datum.Colour - $datum.Spotcol
    }

    else {
        if ($datum.Colour) { $datum.result = $datum.Colour }
        else { $datum.result = 0 }
    }
}

$data | Format-Table

Let's call each iteration of the $data array $datum. Your D column is titled $datum.Colour, E column is titled $datum.Spotcol, and A column is titled $datum.Serial. This is because each object of the array has the column titles as the attribute names instead of Excel's A2 D2 stuff.

At each iteration, if $datum.Serial [A2] starts with "C", we do $datum.Colour [D2] - $datum.Spotcol [E2] and assign it to $datum.result [F2]. Otherwise, we make $datum.result [F2] the value of $datum.Colour [D2].

Edit:

Made grammatical changes ($data | FT to $data | Format-Table)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文