在CSV文件中添加单元格并使用PowerShell移动一些单元格 [英] Add a cell in a CSV file and move some cells with PowerShell

查看:144
本文介绍了在CSV文件中添加单元格并使用PowerShell移动一些单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须添加一个单元格并在我的Excel表格(CSV)中移动数组第二部分的单元格,这是我需要得到的一个例子:

I have to add a cell and move the cells of the second part of the array in my Excel table (CSV), this is an example of what i need to get :

col1中不存在  Column2 Column3 Column4     不存在于col2  Column1 Column3 Column4

Not present in col1   Column2 Column3 Column4      Not present in col2   Column1 Column3 Column4

                                        58          85          25           
                                    75          85          25

                                        58          85           25                                                 75           85           25

                                        10          10            50         
                                      40          40          50

                                       10          10            50                                                 40           40           50

我想添加"不存在于col"中 喜欢这个带有值的列。

I would like to add the "Not present in col"  like this before the columns with the values.

我当前导出的文件:

这个我的脚本的当前结果,我想在那里得到结果。我知道如何删除列引用(最长的一个)。

This the current result of my script and I would like to get the result up there. I know how to remove the column reference (the longest one).




我导入的文件:

以下代码允许删除与列引用相比的不同列中的相同值(匹配单元格):

The following code allows to remove the same values (matching cells) in the different columns compared to the column reference:

$csv = Import-Csv .\test1.csv -Delimiter ';'

$ ref = [ordered] @ {}

$ref = [ordered]@{}

$ columns = foreach($ i in 0..7 ){,[Collections.ArrayList] @()}

$columns = foreach ($i in 0..7) { ,[Collections.ArrayList]@() }

foreach($ csv中的$行){ 

foreach ($row in $csv) { 

  $ value = $ row.Column1   

  $value = $row.Column1   

$ ref [$ value] = $ true 

$ref[$value] = $true 

  $ columns [0] .add($ value)> $ null}

  $columns[0].add($value) >$null}

foreach($ csv中的$ row){ 

foreach ($row in $csv) { 

  $ i = 1   

  $i = 1   

foreach($ Column in'Column2','Column3','Column4'){     

foreach ($col in 'Column2', 'Column3', 'Column4') {     

  $ value = $ row。$ col     

  $value = $row.$col     

  if(!$ ref [$ value]){       

  if (!$ref[$value]) {       

    $ columns [$ i] .add($ value)> $ null        }   

    $columns[$i].add($value) >$null        }   

    $ I ++ NBSP;   } $

    $i++    }}

$ maxLine =($ columns | select -expand Count | measure -Maximum).Maximum - 1

$maxLine = ($columns | select -expand Count | measure -Maximum).Maximum - 1

$ csv = foreach($ i in $ .. $ maxLine) {    [PSCustomObject] @ {     

$csv = foreach ($i in 0..$maxLine) {    [PSCustomObject]@{     

  Column1 = $ columns [0] [$ i]     

  Column1 = $columns[0][$i]     

  Column2 = $ columns [1] [$ i]       

  Column2 = $columns[1][$i]       

Column3 = $ columns [2] [$ i]       

Column3 = $columns[2][$i]       

Column4 = $ columns [3] [$ i]    }

Column4 = $columns[3][$i]    }}

$ csv | Export-CSV -Path"。\ test3.csv" -NoTypeInformation -Delimiter";"

$csv | Export-CSV -Path ".\test3.csv" -NoTypeInformation -Delimiter ";"

$ csv = Import-Csv。\ test1.csv -Delimiter';'

$csv = Import-Csv .\test1.csv -Delimiter ';'

$ ref = [ordered] @ {} $ columns = foreach($ i in 0..7){,[Collections.ArrayList] @()}

$ref = [ordered]@{}$columns = foreach ($i in 0..7) { ,[Collections.ArrayList]@() }

foreach($ csv $ row){    $ value = $ row.Column2   

foreach ($row in $csv) {    $value = $row.Column2   

$ ref [$ value] = $ true    $ columns [0] .add($ value)> $ null}

$ref[$value] = $true    $columns[0].add($value) >$null}

foreach($ csv中的$行){    $ i = 1   

foreach ($row in $csv) {    $i = 1   

foreach($ Column in'Column1','Column3','Column4'){     

foreach ($col in 'Column1', 'Column3', 'Column4') {     

  $ value = $ row。$ col        if(!$ ref [$ value]){         

  $value = $row.$col        if (!$ref[$value]) {         

  $ columns [$ i] .add($ value)> $ null        }        $ I ++ NBSP;   } $

  $columns[$i].add($value) >$null        }        $i++    }}

$ maxLine =($ columns | select -expand Count | measure -Maximum).Maximum - 1

$maxLine = ($columns | select -expand Count | measure -Maximum).Maximum - 1

$ csv = foreach($ i in $ .. $ maxLine) {    [PSCustomObject] @ {     

$csv = foreach ($i in 0..$maxLine) {    [PSCustomObject]@{     

  Column1 = $ columns [1] [$ i]     

  Column1 = $columns[1][$i]     

  Column2 = $ columns [0] [$ i]   

  Column2 = $columns[0][$i]   

  Column3 = $ columns [2] [$ i]     

  Column3 = $columns[2][$i]     

Column4 = $ columns [3] [$ i]    }

Column4 = $columns[3][$i]    }}

$ csv |导出CSV  -Path"。\ test3.csv" -NoTypeInformation -Delimiter" ;;" -Append

$csv | Export-CSV   -Path ".\test3.csv" -NoTypeInformation -Delimiter ";" -Append







推荐答案

您在此处发布的代码就是一行中的所有内容。这不能以这种方式工作,阅读和理解是不可能的。可以纠正代码,也许可以解释更详细的尝试吗?根据给出的解释,我没有得到它。
The code you posted here is everything on one line. That cannot work this way and it's quite impossible to read and to understand. Could correct the code and maybe explain more detailed what you try to do? With the given explanation I did not get it.


这篇关于在CSV文件中添加单元格并使用PowerShell移动一些单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆