表插入空白单元格

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了表插入空白单元格相关的知识,希望对你有一定的参考价值。

我正在尝试将列表导入html格式的表。它似乎正在按预期工作,除了我有很多单元格填充空白。我试图使用修剪和其他方法删除所有额外的空格,但额外的空格仍然在表中。

这是代码。

uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
    select -Skip 4 > $PSSCriptRootoutput.txt

$nl = [System.Environment]::NewLine
$unity = Get-Content $PSScriptRootoutput.txt

$unity = $unity -replace "[0-9]:",""
$unity = $unity -replace "=",":"
$unity = $unity -replace "$n1$n1",""
$unity = $unity.Trim()

$UnitySummary = ($unity) -split '$n1$n1' | foreach {
    $Stringdata = $_.Replace(':','=') -replace '$n1',''
    New-Object PSObject -Property $(ConvertFrom-StringData $Stringdata)
} 

$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }
TH{border-width: 2px;padding: 3px;border-style: solid; border-color: black;background-color: #08088A; color: white; font-size: 67%;}
TD{border-width: 2px;padding: 3px;border-style: solid; border-color: black; font-size: 100%;}
</style>
"@

$unitySummary |
    select 'Id','Time','Message' |
    ConvertTo-Html -Head $style |
    Out-File $PSScriptRootweboutput.html

这是我在第一行(output.txt)处理的原始文件:

1:    ID           = alert_457
      Time         = 2017-12-16 02:46:04.937
      Message      = A recommended system software (version 4.2.1.9535982) is now available for download. 
      Description  = "A recommended system software is now available for download. To ensure optimal system performance, EMC recommends upgrading to this version. Run a health check about a week before installing the upgrade to identify and resolve any underlying issues that may prevent a successful update. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_update_software.html)"
      Severity     = info
      Acknowledged = no

2:    ID           = alert_456
      Time         = 2017-12-15 01:04:33.480
      Message      = Host 172.16.243.13 is operating normally.
      Description  = "The component is operating normally. No action is required."
      Severity     = info
      Acknowledged = no

3:    ID           = alert_455
      Time         = 2017-12-15 01:04:33.367
      Message      = Host 172.16.243.12 is operating normally.
      Description  = "The component is operating normally. No action is required."
      Severity     = info
      Acknowledged = no

4:    ID           = alert_454
      Time         = 2017-12-15 01:04:33.245
      Message      = Host 172.16.243.11 is operating normally.
      Description  = "The component is operating normally. No action is required."
      Severity     = info
      Acknowledged = no

5:    ID           = alert_453
      Time         = 2017-12-15 00:04:38.035
      Message      = Unable to refresh managed server 172.16.243.13 because of a connection issue.
      Description  = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)"
      Severity     = critical
      Acknowledged = no

6:    ID           = alert_452
      Time         = 2017-12-15 00:04:37.935
      Message      = Unable to refresh managed server 172.16.243.12 because of a connection issue.
      Description  = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)"
      Severity     = critical
      Acknowledged = no

7:    ID           = alert_451
      Time         = 2017-12-15 00:04:37.753
      Message      = Unable to refresh managed server 172.16.243.11 because of a connection issue.
      Description  = "Failed to connect to host. Please check your network connection. (https://172.16.243.100/help/webhelp/en_US/index.html?#unity_t_unable_to_refresh_managed_server_because_of_a_connection_issue.html)"
      Severity     = critical
      Acknowledged = no

这是我得到的输出。您可以看到项目放在正确的列中,但是插入了额外的空白单元格,导致数据显示在错误的行中。

enter image description here

答案

Get-Content不会将输入文件读作单个字符串,而是读取行数组。因为New-Object只创建具有单个属性的对象。通过select 'Id','Time','Message'管道结果然后添加其他属性,仅使用空值。

通过Get-Content管理Out-String的输出来解决这个问题:

$unity = Get-Content $PSScriptRootoutput.txt | Out-String

在PowerShell v3和更新版本上,您也可以使用参数Get-Content调用-Raw以获得相同的结果:

$unity = Get-Content $PSScriptRootoutput.txt -Raw
另一答案

请接受Ansgar的回答,因为它解决了您的问题的根源。我发布这个作为额外的帮助,让你得到你想要的东西。

而不是将整个事物作为一个字符串读取,将其拆分,进行多次替换(包括将所有等号转换为冒号,然后将所有冒号转换为等号),取大字符串,将其拆分为条目(由数字后跟一行开头的冒号),并通过ConvertFrom-StringData管道每个条目并从中创建对象。它将为您提供我非常确定您希望能够传递到您的表中的内容(选择您想要的粗略属性)。

您甚至可以通过将原始命令传送到Out-String来跳过整个保存到磁盘并将其读回。

$Unity = uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
    select -Skip 4 |
    Out-String
$Unity -split '(?m)^d:' |
    Where{$_.Trim()} |
    ForEach{ [PSCustomObject](ConvertFrom-StringData -StringData $_) }

然后你只需将它输出到你的ConvertTo-Html就可以了。

$Unity = uemcli -d 192.168.1.100 -u Local/admin -p admin /event/alert/hist show -acknowledged no |
        select -Skip 4 |
        Out-String
$style = @"
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse; }
TH{border-width: 2px;padding: 3px;border-style: solid; border-color: black;background-color: #08088A; color: white; font-size: 67%;}
TD{border-width: 2px;padding: 3px;border-style: solid; border-color: black; font-size: 100%;}
</style>
"@
$Unity -split '(?m)^d:' |
    Where{$_.Trim()} |
    ForEach{ [PSCustomObject](ConvertFrom-StringData -StringData $_) } |
    ConvertTo-Html -Head $style -as Table -Property 'Id','Time','Message' |
    Set-Content $PSScriptRootweboutput.html

以上是关于表插入空白单元格的主要内容,如果未能解决你的问题,请参考以下文章

用于查找空白单元格的 VBA

如果单元格为空白,则清除 VBA 中另一个单元格代码的内容

搜索包含文本的单元格并在下一个空白单元格中粘贴值

是否存在按 Ctrl + G 并选择空白不会突出显示空单元格的情况?

Excel VBA - 如何选择范围向下到第一个空白单元格

求助excel单元格不同自动添加3个空白行