文本格式化时文本不同

huangapple go评论68阅读模式
英文:

Formating text when text is different

问题

我正在使用Powershell制作一个表格,如下所示:

    Count Name                                                       
    ----- ----                                                       
        1 Cisco Spark Room 55, NetworkAddressMissing                 
        1 Cisco Spark Room 55, OK                                    
        2 Cisco Spark Room Kit, NetworkAddressMissing                
        2 Cisco TelePresence DX80, NetworkAddressMissing              
        1 Cisco TelePresence DX80, NoHTTPSResponse                    
        4 Cisco TelePresence DX80, OK                                
       10 Cisco TelePresence MX200 G2, OK                            
       11 Cisco TelePresence MX200, OK                               
        3 Cisco TelePresence MX300 G2, NoHTTPSResponse               
       48 Cisco TelePresence MX300 G2, OK                            
        6 Cisco TelePresence MX300, OK                               
        3 Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing

Powershell脚本主要用于获取一些数据并对其进行分组。我获取的内容取决于后端系统中的内容。我被要求将结果分开以使其更具美感。有建议在不同系统之间添加白色分隔线,或者以不同颜色显示不同系统等。这意味着我需要检查“Name”字段的开头,直到逗号,以查看它是否是具有不同状态的相同系统,还是不同的系统,需要某种分隔符。

上面的表格显然要长得多。它是通过Powershell制作的,我还使用Powershell将其转换为HTML,并使用一些CSS使其更适合非技术人员的眼睛。

关于如何按照建议的格式进行排版,有什么建议吗?

英文:

I'm makeing a table in Powershell like so :

Count Name                                                       
----- ----                                                       
    1 Cisco Spark Room 55, NetworkAddressMissing                 
    1 Cisco Spark Room 55, OK                                    
    2 Cisco Spark Room Kit, NetworkAddressMissing                
    2 Cisco TelePresence DX80, NetworkAddressMissing             
    1 Cisco TelePresence DX80, NoHTTPSResponse                   
    4 Cisco TelePresence DX80, OK                                
   10 Cisco TelePresence MX200 G2, OK                            
   11 Cisco TelePresence MX200, OK                               
    3 Cisco TelePresence MX300 G2, NoHTTPSResponse               
   48 Cisco TelePresence MX300 G2, OK                            
    6 Cisco TelePresence MX300, OK                               
    3 Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing

The Powershell script mainly goes in and gets some data, grouping it. What I get depends on what's in the back-systems. I've been asked to seperate the results to make it more ... aesthetic. It's been suggested that we should have white lines in-between the different systems, or place the different systems in different colors etc. Which means I need to check the beginning of "Name" up to the comma to see if it's the same system with a different status, or if it's a different system which would require some kind of seperator.

The table above is alot longer obviously. It's made through Powershell, and I also use Powershell to convert it into HTML with a small CSS to make it more pleasing for the non-technical eye.

Any suggestions on how to go about formating as suggested ?

答案1

得分: 1

以下是翻译好的部分:

假设您的示例表格输出是类似以下数组的 PSObjects 的结果:

>     $data = [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, NetworkAddressMissing'},
>             [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, OK'},
>             [PsCustomObject]@{ Count = 2; Name = 'Cisco Spark Room Kit, NetworkAddressMissing'},
>             [PsCustomObject]@{ Count = 2; Name = 'Cisco TelePresence DX80, NetworkAddressMissing'},
>             [PsCustomObject]@{ Count = 1; Name = 'Cisco TelePresence DX80, NoHTTPSResponse'},
>             [PsCustomObject]@{ Count = 4; Name = 'Cisco TelePresence DX80, OK'},
>             [PsCustomObject]@{ Count = 10; Name = 'Cisco TelePresence MX200 G2, OK'},
>             [PsCustomObject]@{ Count = 11; Name = 'Cisco TelePresence MX200, OK'},
>             [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence MX300 G2, NoHTTPSResponse'},
>             [PsCustomObject]@{ Count = 48; Name = 'Cisco TelePresence MX300 G2, OK'},
>             [PsCustomObject]@{ Count = 6; Name = 'Cisco TelePresence MX300, OK'},
>             [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing'}

如果您想在分组的项目之间插入空行,可以尝试以下方法:

# 在第一次迭代中显示标题
$hideHeaders = $false
$data | Group-Object @{ Expression = {($_.Name -split ',')[0]}} | ForEach-Object {
    ($_.Group | Format-Table -AutoSize -HideTableHeaders:$hideHeaders | Out-String).TrimEnd()
    $hideHeaders = $true
}

输出如下:

>     Count Name                                      
>     ----- ----                                      
>         1 Cisco Spark Room 55, NetworkAddressMissing
>         1 Cisco Spark Room 55, OK
> 
>         2 Cisco Spark Room Kit, NetworkAddressMissing
> 
>         2 Cisco TelePresence DX80, NetworkAddressMissing
>         1 Cisco TelePresence DX80, NoHTTPSResponse      
>         4 Cisco TelePresence DX80, OK
> 
>        10 Cisco TelePresence MX200 G2, OK
> 
>        11 Cisco TelePresence MX200, OK
> 
>         3 Cisco TelePresence MX300 G2, NoHTTPSResponse
>        48 Cisco TelePresence MX300 G2, OK
> 
>         6 Cisco TelePresence MX300, OK
> 
>         3 Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing

请注意,这只是一部分翻译。如果您需要更多内容的翻译,请提出具体的要求。

英文:

Assuming your example table output is the result of an array of PSObjects like this:

> $data = [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, NetworkAddressMissing'},
> [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, OK'},
> [PsCustomObject]@{ Count = 2; Name = 'Cisco Spark Room Kit, NetworkAddressMissing'},
> [PsCustomObject]@{ Count = 2; Name = 'Cisco TelePresence DX80, NetworkAddressMissing'},
> [PsCustomObject]@{ Count = 1; Name = 'Cisco TelePresence DX80, NoHTTPSResponse'},
> [PsCustomObject]@{ Count = 4; Name = 'Cisco TelePresence DX80, OK'},
> [PsCustomObject]@{ Count = 10; Name = 'Cisco TelePresence MX200 G2, OK'},
> [PsCustomObject]@{ Count = 11; Name = 'Cisco TelePresence MX200, OK'},
> [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence MX300 G2, NoHTTPSResponse'},
> [PsCustomObject]@{ Count = 48; Name = 'Cisco TelePresence MX300 G2, OK'},
> [PsCustomObject]@{ Count = 6; Name = 'Cisco TelePresence MX300, OK'},
> [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing'}

And you want an empty line between the grouped items, this might help:

# show headers on the first iteration only
$hideHeaders = $false
$data | Group-Object @{ Expression = {($_.Name -split ',')[0]}} | ForEach-Object {
    ($_.Group | Format-Table -AutoSize -HideTableHeaders:$hideHeaders | Out-String).TrimEnd()
    $hideHeaders = $true
}

Output:

> Count Name
> ----- ----
> 1 Cisco Spark Room 55, NetworkAddressMissing
> 1 Cisco Spark Room 55, OK
>
> 2 Cisco Spark Room Kit, NetworkAddressMissing
>
> 2 Cisco TelePresence DX80, NetworkAddressMissing
> 1 Cisco TelePresence DX80, NoHTTPSResponse
> 4 Cisco TelePresence DX80, OK
>
> 10 Cisco TelePresence MX200 G2, OK
>
> 11 Cisco TelePresence MX200, OK
>
> 3 Cisco TelePresence MX300 G2, NoHTTPSResponse
> 48 Cisco TelePresence MX300 G2, OK
>
> 6 Cisco TelePresence MX300, OK
>
> 3 Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing

<hr>

On re-reading the question, I understand that your table output should/could be HTML.

In that case, perhaps the below function could be of service:

$style = @&quot;
&lt;style type=&quot;text/css&quot;&gt;
body {
font-family: Calibri, Verdana, Arial, Geneva, Helvetica, sans-serif;
font-size: 11pt;
color: black;
}
table, td, th {
border-style: solid;
font-family: Calibri, Verdana, Arial, Geneva, Helvetica, sans-serif;
font-size: 11pt;
margin: 8pt 0 8pt 0;
}
table {
border-width: 0 0 1px 1px;
border-spacing: 0;
border-collapse: collapse;
}
td, th {
margin: 0;
padding: 4px;
border-width: 1px 1px 0 0;
text-align: left;
}
th {
color: white;
font-weight: bold;
}
&lt;/style&gt;
&quot;@
# HTML start
$openHtml = @&quot;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
&lt;title&gt;Report&lt;/title&gt;
&lt;meta name=&quot;generator&quot; content=&quot;PowerShell&quot; /&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
$style
&lt;/head&gt;
&lt;body&gt;
&quot;@
# HTML close
$closeHtml = &#39;&lt;/body&gt;&lt;/html&gt;&#39;
function ConvertTo-HTMLTable {
# Converts an object to a themed HTML table, mimicking the Excel &quot;Table Style Normal&quot;
# Accepts a System.Data.DataTable object or an array of PSObjects and converts to styled HTML table
[CmdletBinding(DefaultParameterSetName = &#39;ByTheme&#39;)]  
Param (
[parameter(Mandatory = $true, Position = 0)] 
[object[]]$Data,
[parameter(ParameterSetName = &#39;ByTheme&#39;)]
[ValidateSet(&#39;Black&#39;, &#39;LightBlue&#39;, &#39;Orange&#39;, &#39;Gray&#39;, &#39;Gold&#39;, &#39;Blue&#39;, &#39;Green&#39;)]
[string]$ThemeColor,
[parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
[string]$HeaderColor,
[parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
[string]$OddRowColor,
[parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
[string]$EvenRowColor,
[parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
[string]$BorderColor
)
# add type needed to replace HTML special characters into entities
Add-Type -AssemblyName System.Web
# define theme colors as array of hashtables
$colors = switch($ThemeColor) {
&#39;Black&#39;     { @{ &#39;header&#39; = &#39;#D9D9D9&#39;; &#39;oddrow&#39; = &#39;#000000&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#000000&#39;} }
&#39;LightBlue&#39; { @{ &#39;header&#39; = &#39;#5B9BD5&#39;; &#39;oddrow&#39; = &#39;#DDEBF7&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#000000&#39;} }
&#39;Orange&#39;    { @{ &#39;header&#39; = &#39;#ED7D31&#39;; &#39;oddrow&#39; = &#39;#FCE4D6&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#F4B084&#39;} }
&#39;Gray&#39;      { @{ &#39;header&#39; = &#39;#A5A5A5&#39;; &#39;oddrow&#39; = &#39;#EDEDED&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#C9C9C9&#39;} }
&#39;Gold&#39;      { @{ &#39;header&#39; = &#39;#FFC000&#39;; &#39;oddrow&#39; = &#39;#FFF2CC&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#FFD966&#39;} }
&#39;Blue&#39;      { @{ &#39;header&#39; = &#39;#4472C4&#39;; &#39;oddrow&#39; = &#39;#D9E1F2&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#8EA9DB&#39;} }
&#39;Green&#39;     { @{ &#39;header&#39; = &#39;#70AD47&#39;; &#39;oddrow&#39; = &#39;#E2EFDA&#39;; &#39;evenrow&#39; = $null; &#39;border&#39; = &#39;#A9D08E&#39;} }
default     { @{ &#39;header&#39; = $HeaderColor; &#39;oddrow&#39; = $OddRowColor; &#39;evenrow&#39; = $EvenRowColor; &#39;border&#39; = $BorderColor} }
}
$sb = New-Object -TypeName System.Text.StringBuilder
[void]$sb.AppendLine(&#39;&lt;table style=&quot;border-color: {0};&quot;&gt;&#39; -f $colors[&#39;border&#39;])
if ($null -ne $Data) {
if (([object]$Data).GetType().FullName -eq &#39;System.Data.DataTable&#39;){
# it is a DataTable; convert to array of PSObjects
$Data = $Data | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors
}
$headers = $Data[0].PSObject.Properties | Select -ExpandProperty Name
[void]$sb.AppendLine((&#39;&lt;thead&gt;&lt;tr style=&quot;background-color: {0};&quot;&gt;&#39; -f $colors[&#39;header&#39;]))
foreach ($column in $headers) {
$th = [System.Web.HttpUtility]::HtmlEncode($column)
[void]$sb.AppendFormat(&#39;&lt;th style=&quot;border-color: {0};&quot;&gt;{1}&lt;/th&gt;&#39;, $colors[&#39;border&#39;], $th)
}
[void]$sb.AppendLine(&#39;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&#39;)
$currentName  = $Data[0].Name
$rowcolor = $colors[&#39;evenrow&#39;]
$Data | ForEach-Object {
# add inline style for different colored rows on each Name change
if ($_.Name -ne $currentName) {
if ($rowcolor -eq $colors[&#39;evenrow&#39;]) {
$rowcolor = $colors[&#39;oddrow&#39;]
}
else {
$rowcolor = $colors[&#39;evenrow&#39;]
}
$currentName = $_.Name
}
if ([string]::IsNullOrWhiteSpace($rowcolor)) {
$tr = &#39;&lt;tr&gt;&#39;
} 
else {
$tr = &#39;&lt;tr style=&quot;background-color: {0};&quot;&gt;&#39; -f $rowcolor
}
[void]$sb.AppendLine($tr)
# now add the data cells
foreach ($column in $headers) {
[string]$val = $($_.$column)
if ([string]::IsNullOrWhiteSpace($val)) { 
$td = &#39;&lt;td style=&quot;border-color: {0};&quot;&gt;&amp;nbsp;&lt;/td&gt;&#39; -f $colors[&#39;border&#39;]
} 
else { 
# if it&#39;s a number, align to the right
[double]$num = 0
if ([double]::TryParse($val,[ref]$num)) {
$td = &#39;&lt;td style=&quot;border-color: {0}; text-align: right;&quot;&gt;{1}&lt;/td&gt;&#39; -f $colors[&#39;border&#39;], $val
}
else {
$val = [System.Web.HttpUtility]::HtmlEncode($val)
$td = &#39;&lt;td style=&quot;border-color: {0};&quot;&gt;{1}&lt;/td&gt;&#39; -f $colors[&#39;border&#39;], $val
}
}
[void]$sb.Append($td)
}
[void]$sb.AppendLine(&#39;&lt;/tr&gt;&#39;)
}
[void]$sb.AppendLine(&#39;&lt;/tbody&gt;&#39;)
}
[void]$sb.AppendLine(&#39;&lt;/table&gt;&#39;)
return $sb.ToString()
}

With all that in place, you can transform your original data (again: I assume this is an array of PSObjects) like so:

# split the Name property into Name and Status and update the objects
$data | ForEach-Object {
$name, $status = ($_.Name -split &#39;,&#39;, 2).Trim()
$_.Name = $name
$_ | Add-Member -MemberType NoteProperty -Name &#39;Status&#39; -Value $status
}
# convert the data to styled HTML table
$table = ConvertTo-HTMLTable -Data ($data | Sort-Object Name) -ThemeColor Blue
# complete the HTML
$html  = &#39;{0}{1}{2}&#39; -f $openHtml, $table, $closeHtml

Next, you can save it as html file

$html | Set-Content -Path &#39;D:\report.html&#39;

Or use Send-MailMessage to send it as nice email to your colleagues.

The Blue theme gives you this output

文本格式化时文本不同

huangapple
  • 本文由 发表于 2020年1月6日 20:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定