文本格式化时文本不同

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

Formating text when text is different

问题

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

  1. Count Name
  2. ----- ----
  3. 1 Cisco Spark Room 55, NetworkAddressMissing
  4. 1 Cisco Spark Room 55, OK
  5. 2 Cisco Spark Room Kit, NetworkAddressMissing
  6. 2 Cisco TelePresence DX80, NetworkAddressMissing
  7. 1 Cisco TelePresence DX80, NoHTTPSResponse
  8. 4 Cisco TelePresence DX80, OK
  9. 10 Cisco TelePresence MX200 G2, OK
  10. 11 Cisco TelePresence MX200, OK
  11. 3 Cisco TelePresence MX300 G2, NoHTTPSResponse
  12. 48 Cisco TelePresence MX300 G2, OK
  13. 6 Cisco TelePresence MX300, OK
  14. 3 Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing

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

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

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

英文:

I'm makeing a table in Powershell like so :

  1. Count Name
  2. ----- ----
  3. 1 Cisco Spark Room 55, NetworkAddressMissing
  4. 1 Cisco Spark Room 55, OK
  5. 2 Cisco Spark Room Kit, NetworkAddressMissing
  6. 2 Cisco TelePresence DX80, NetworkAddressMissing
  7. 1 Cisco TelePresence DX80, NoHTTPSResponse
  8. 4 Cisco TelePresence DX80, OK
  9. 10 Cisco TelePresence MX200 G2, OK
  10. 11 Cisco TelePresence MX200, OK
  11. 3 Cisco TelePresence MX300 G2, NoHTTPSResponse
  12. 48 Cisco TelePresence MX300 G2, OK
  13. 6 Cisco TelePresence MX300, OK
  14. 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

以下是翻译好的部分:

  1. 假设您的示例表格输出是类似以下数组的 PSObjects 的结果:
  2. > $data = [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, NetworkAddressMissing'},
  3. > [PsCustomObject]@{ Count = 1; Name = 'Cisco Spark Room 55, OK'},
  4. > [PsCustomObject]@{ Count = 2; Name = 'Cisco Spark Room Kit, NetworkAddressMissing'},
  5. > [PsCustomObject]@{ Count = 2; Name = 'Cisco TelePresence DX80, NetworkAddressMissing'},
  6. > [PsCustomObject]@{ Count = 1; Name = 'Cisco TelePresence DX80, NoHTTPSResponse'},
  7. > [PsCustomObject]@{ Count = 4; Name = 'Cisco TelePresence DX80, OK'},
  8. > [PsCustomObject]@{ Count = 10; Name = 'Cisco TelePresence MX200 G2, OK'},
  9. > [PsCustomObject]@{ Count = 11; Name = 'Cisco TelePresence MX200, OK'},
  10. > [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence MX300 G2, NoHTTPSResponse'},
  11. > [PsCustomObject]@{ Count = 48; Name = 'Cisco TelePresence MX300 G2, OK'},
  12. > [PsCustomObject]@{ Count = 6; Name = 'Cisco TelePresence MX300, OK'},
  13. > [PsCustomObject]@{ Count = 3; Name = 'Cisco TelePresence Profile 52/55 C40, NetworkAddressMissing'}
  14. 如果您想在分组的项目之间插入空行,可以尝试以下方法:
  15. # 在第一次迭代中显示标题
  16. $hideHeaders = $false
  17. $data | Group-Object @{ Expression = {($_.Name -split ',')[0]}} | ForEach-Object {
  18. ($_.Group | Format-Table -AutoSize -HideTableHeaders:$hideHeaders | Out-String).TrimEnd()
  19. $hideHeaders = $true
  20. }
  21. 输出如下:
  22. > Count Name
  23. > ----- ----
  24. > 1 Cisco Spark Room 55, NetworkAddressMissing
  25. > 1 Cisco Spark Room 55, OK
  26. >
  27. > 2 Cisco Spark Room Kit, NetworkAddressMissing
  28. >
  29. > 2 Cisco TelePresence DX80, NetworkAddressMissing
  30. > 1 Cisco TelePresence DX80, NoHTTPSResponse
  31. > 4 Cisco TelePresence DX80, OK
  32. >
  33. > 10 Cisco TelePresence MX200 G2, OK
  34. >
  35. > 11 Cisco TelePresence MX200, OK
  36. >
  37. > 3 Cisco TelePresence MX300 G2, NoHTTPSResponse
  38. > 48 Cisco TelePresence MX300 G2, OK
  39. >
  40. > 6 Cisco TelePresence MX300, OK
  41. >
  42. > 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:

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

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:

  1. $style = @&quot;
  2. &lt;style type=&quot;text/css&quot;&gt;
  3. body {
  4. font-family: Calibri, Verdana, Arial, Geneva, Helvetica, sans-serif;
  5. font-size: 11pt;
  6. color: black;
  7. }
  8. table, td, th {
  9. border-style: solid;
  10. font-family: Calibri, Verdana, Arial, Geneva, Helvetica, sans-serif;
  11. font-size: 11pt;
  12. margin: 8pt 0 8pt 0;
  13. }
  14. table {
  15. border-width: 0 0 1px 1px;
  16. border-spacing: 0;
  17. border-collapse: collapse;
  18. }
  19. td, th {
  20. margin: 0;
  21. padding: 4px;
  22. border-width: 1px 1px 0 0;
  23. text-align: left;
  24. }
  25. th {
  26. color: white;
  27. font-weight: bold;
  28. }
  29. &lt;/style&gt;
  30. &quot;@
  31. # HTML start
  32. $openHtml = @&quot;
  33. &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;
  34. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  35. &lt;head&gt;
  36. &lt;title&gt;Report&lt;/title&gt;
  37. &lt;meta name=&quot;generator&quot; content=&quot;PowerShell&quot; /&gt;
  38. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;&gt;
  39. $style
  40. &lt;/head&gt;
  41. &lt;body&gt;
  42. &quot;@
  43. # HTML close
  44. $closeHtml = &#39;&lt;/body&gt;&lt;/html&gt;&#39;
  45. function ConvertTo-HTMLTable {
  46. # Converts an object to a themed HTML table, mimicking the Excel &quot;Table Style Normal&quot;
  47. # Accepts a System.Data.DataTable object or an array of PSObjects and converts to styled HTML table
  48. [CmdletBinding(DefaultParameterSetName = &#39;ByTheme&#39;)]
  49. Param (
  50. [parameter(Mandatory = $true, Position = 0)]
  51. [object[]]$Data,
  52. [parameter(ParameterSetName = &#39;ByTheme&#39;)]
  53. [ValidateSet(&#39;Black&#39;, &#39;LightBlue&#39;, &#39;Orange&#39;, &#39;Gray&#39;, &#39;Gold&#39;, &#39;Blue&#39;, &#39;Green&#39;)]
  54. [string]$ThemeColor,
  55. [parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
  56. [string]$HeaderColor,
  57. [parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
  58. [string]$OddRowColor,
  59. [parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
  60. [string]$EvenRowColor,
  61. [parameter(ParameterSetName = &#39;ByCustomColors&#39;)]
  62. [string]$BorderColor
  63. )
  64. # add type needed to replace HTML special characters into entities
  65. Add-Type -AssemblyName System.Web
  66. # define theme colors as array of hashtables
  67. $colors = switch($ThemeColor) {
  68. &#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;} }
  69. &#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;} }
  70. &#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;} }
  71. &#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;} }
  72. &#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;} }
  73. &#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;} }
  74. &#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;} }
  75. default { @{ &#39;header&#39; = $HeaderColor; &#39;oddrow&#39; = $OddRowColor; &#39;evenrow&#39; = $EvenRowColor; &#39;border&#39; = $BorderColor} }
  76. }
  77. $sb = New-Object -TypeName System.Text.StringBuilder
  78. [void]$sb.AppendLine(&#39;&lt;table style=&quot;border-color: {0};&quot;&gt;&#39; -f $colors[&#39;border&#39;])
  79. if ($null -ne $Data) {
  80. if (([object]$Data).GetType().FullName -eq &#39;System.Data.DataTable&#39;){
  81. # it is a DataTable; convert to array of PSObjects
  82. $Data = $Data | Select-Object * -ExcludeProperty ItemArray, Table, RowError, RowState, HasErrors
  83. }
  84. $headers = $Data[0].PSObject.Properties | Select -ExpandProperty Name
  85. [void]$sb.AppendLine((&#39;&lt;thead&gt;&lt;tr style=&quot;background-color: {0};&quot;&gt;&#39; -f $colors[&#39;header&#39;]))
  86. foreach ($column in $headers) {
  87. $th = [System.Web.HttpUtility]::HtmlEncode($column)
  88. [void]$sb.AppendFormat(&#39;&lt;th style=&quot;border-color: {0};&quot;&gt;{1}&lt;/th&gt;&#39;, $colors[&#39;border&#39;], $th)
  89. }
  90. [void]$sb.AppendLine(&#39;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&#39;)
  91. $currentName = $Data[0].Name
  92. $rowcolor = $colors[&#39;evenrow&#39;]
  93. $Data | ForEach-Object {
  94. # add inline style for different colored rows on each Name change
  95. if ($_.Name -ne $currentName) {
  96. if ($rowcolor -eq $colors[&#39;evenrow&#39;]) {
  97. $rowcolor = $colors[&#39;oddrow&#39;]
  98. }
  99. else {
  100. $rowcolor = $colors[&#39;evenrow&#39;]
  101. }
  102. $currentName = $_.Name
  103. }
  104. if ([string]::IsNullOrWhiteSpace($rowcolor)) {
  105. $tr = &#39;&lt;tr&gt;&#39;
  106. }
  107. else {
  108. $tr = &#39;&lt;tr style=&quot;background-color: {0};&quot;&gt;&#39; -f $rowcolor
  109. }
  110. [void]$sb.AppendLine($tr)
  111. # now add the data cells
  112. foreach ($column in $headers) {
  113. [string]$val = $($_.$column)
  114. if ([string]::IsNullOrWhiteSpace($val)) {
  115. $td = &#39;&lt;td style=&quot;border-color: {0};&quot;&gt;&amp;nbsp;&lt;/td&gt;&#39; -f $colors[&#39;border&#39;]
  116. }
  117. else {
  118. # if it&#39;s a number, align to the right
  119. [double]$num = 0
  120. if ([double]::TryParse($val,[ref]$num)) {
  121. $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
  122. }
  123. else {
  124. $val = [System.Web.HttpUtility]::HtmlEncode($val)
  125. $td = &#39;&lt;td style=&quot;border-color: {0};&quot;&gt;{1}&lt;/td&gt;&#39; -f $colors[&#39;border&#39;], $val
  126. }
  127. }
  128. [void]$sb.Append($td)
  129. }
  130. [void]$sb.AppendLine(&#39;&lt;/tr&gt;&#39;)
  131. }
  132. [void]$sb.AppendLine(&#39;&lt;/tbody&gt;&#39;)
  133. }
  134. [void]$sb.AppendLine(&#39;&lt;/table&gt;&#39;)
  135. return $sb.ToString()
  136. }

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

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

Next, you can save it as html file

  1. $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:

确定