【问题标题】:Updating Excel columns(or rows?) based on a condition根据条件更新 Excel 列(或行?)
【发布时间】:2019-08-14 06:32:54
【问题描述】:

我有一个包含用户名 city 的 excel 文件。我正在尝试向同一个 excel 添加一个新列,假设该列是部门。我有不同的用户列表,它告诉我哪个用户属于哪个部门。我现在正在尝试更新我拥有的原始 excel 文件中的 Dept 列。我目前的代码如下

$test = import-csv .\test.csv | Select Name,City,Dept
if ($test.name = 'John'){
$test.Dept = 'HR' 
}

它不起作用,得到这个错误

Exception setting "Name": "The property 'Name' cannot be found on this object. Verify that the property exists and can be set."
At line:2 char:1
+ $test.Name= 'John'
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
    + FullyQualifiedErrorId : ExceptionWhenSetting

不确定我在哪里做错了,这是否是正确的方法。

【问题讨论】:

    标签: excel powershell


    【解决方案1】:

    这可能会对您有所帮助,请使用 dept 或您要添加的任何其他列名称,而不是 column3

    Import-Csv file.csv | 
    Select-Object *,"column3" | 
    Export-Csv file.csv -NoTypeInformation
    

    【讨论】:

    • 不确定在这种情况下它如何帮助我。我需要根据基于名称的条件在 Dept 列中写入。
    猜你喜欢
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 2023-02-20
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多