Создание таблицы данных из табличного отчета с использованием Groovy - PullRequest
1 голос
/ 28 марта 2011

Я занялся преобразованием отчета в виде таблицы в табличные данные, и по разным причинам у меня есть только программный инструмент groovy (то есть без команд оболочки, без C, без Perl).Данные предварительно подытожены в пяти столбцах:

Группировка (например, компания), Подгруппа (например, квартал), промежуточный итог, общий итог подгруппы, общий итог (итоговые данные прикреплены к первомустрока каждой группы):

Big Co.\t2009 Q4\t29\t88\t308
\t2010 Q1\t38\t\t          
\t2010 Q4\t21\t\t          
Gargantua Inc.\t2009 Q4\t33\t139       
\t2010 Q1\t31\t\t          
\t2010 Q2\t36\t\t          
\t2010 Q3\t39\t\t          
Mediocre Ltd.\t2009 Q4\t39\t81     
\t2010 Q4\t42\t\t          

И вывод должен быть примерно таким:

<table>
    <tr><th>Group</th><th>2009 Q4</th><th>2010 Q1</th><th>2010 Q2</th><th>2010 Q3</th><th>2010 Q4</th><th>Subtotal</th></tr>
    <tr><th>Big Co</th><td>29</td><td>38</td><td>&nbsp;</td><td>&nbsp;</td><td>38</td><th>308</th></tr>      
    <tr><th>Gargantua Inc</th><td>29</td><td>31</td><td>36</td><td>39</td><td>&nbsp;</td><th>139</th></tr>
    <tr><th>Gargantua Inc</th><td>39</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>42</td><th>81</th></tr>
    <tr><th>&nbsp;</th><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><th>308</th></tr>
</table>

Я хотел бы иметь возможность выполнять несколько циклов над данными;вставлять вещи в ассоциативный массив ассоциативных массивов, создавая полный список ключей на ходу;затем повторите цикл, чтобы произвести вывод.Я взглянул на groovy, и синтаксис Map полностью сработал.

Может ли кто-нибудь указать мне на несколько более ясных примеров итераций на Groovy Map, чтобы я мог хотя бы получить удар по воде с помощьюthis?

Любая помощь, с благодарностью полученная.

(Если кто-то думает, что это просто рыбалка, я собрал PHP-версию, которая добилась цели - по сути, мне нужночтобы перевести это на отличный ...)

<?php
$rows           = explode("\n", $data);
$group          = "";
$subgroup       = "";
$table          = array();
$totals         = array();
$subgroup_names = array();
$group_names    = array();
foreach($rows as $key => $row) { 
  $rowData = explode("\t", $row);
  if($rowData[0]) {
    $group = $rowData[0];
    $group_names[$rowData[0]] = true;  
  }
  if($rowData[1]) {
    $subgroup = $rowData[1];
    $subgroup_names[$rowData[1]] = true;
  }
  $table[$group][$subgroup]['jobcount'] = $rowData[2];
  if($rowData[3]) $totals[$group]['jobcount'] = $rowData[3];
  if($rowData[4]) $totals['grandtotal'] = $rowData[4];
}
$group_names    = array_keys($group_names);
asort($group_names);
$subgroup_names = array_keys($subgroup_names);
asort($subgroup_names);  

$result = array();
$result['header'] = "<th>&nbsp;</th>"; 
foreach ($subgroup_names as $subgroup) {
  $result['header'] .= "<th>$subgroup</th>"; 
}
$result['header'] .= "<th>Subtotals</th>"; 

foreach ($group_names as $group) {
  $result[$group] = "<th>$group</th>";
  foreach ($subgroup_names as $subgroup) {
    $value = isset($table[$group][$subgroup]['jobcount'])?
      $table[$group][$subgroup]['jobcount']:
      '&nbsp;';
    $result[$group] .= "<td>".$value."</td>"; 
  }
  $result[$group] .= "<th>".$totals[$group]['jobcount']."</th>";
}

$result['footer'] = "";
foreach ($subgroup_names as $subgroup) {
  $result['footer'] .= "<th>&nbsp;</th>"; 
}
$result['footer'] .= "<th>Total</th>"; 
$result['footer'] .= "<th>".$totals['grandtotal']."</th>"; 

echo "<table><tr>".join($result, "</tr>\n<tr>")."</tr></table>";

?>

1 Ответ

1 голос
/ 28 марта 2011

Хорошо, собери это вместе ... Возможно, это не самый красивый - но тогда и формат ввода не будет; -)

String txt = '''Big Co.\t2009 Q4\t29\t88\t308
    \t2010 Q1\t38\t\t          
    \t2010 Q4\t21\t\t          
    Gargantua Inc.\t2009 Q4\t33\t139       
    \t2010 Q1\t31\t\t          
    \t2010 Q2\t36\t\t          
    \t2010 Q3\t39\t\t          
    Mediocre Ltd.\t2009 Q4\t39\t81     
    \t2010 Q4\t42\t\t'''

// Parse the text
def summary = txt.split( '\n' ).inject( [] ) { list, row ->
  def split = row.split( '\t' ).collect { it.trim() }
  while( split.size() < 5 ) split << ''
  if( split[ 0 ] ) {
    list << [ group:split[ 0 ], grouptotal:split[ 3 ], grandtotal:split[ 4 ], subtotals:[:] ]
  }
  list[ -1 ].subtotals << [ (split[ 1 ]):split[ 2 ] ]
  list
}

// Get a sorted set of all available quarters
def allQuarters = summary.subtotals*.keySet().flatten() as TreeSet

// Then build our html output
def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder( writer )
builder.table() {
  // Header row
  tr {
    th "group"
    allQuarters.each { q ->
      th q
    }
    th "summary"
  }
  // Row for each group
  summary.each { group ->
    tr {
      td group.group
      allQuarters.each { q ->
        def v = group.subtotals."$q"
        if( v )
          td v
        else
          td { mkp.yieldUnescaped '&nbsp;' }
      }
      td group.grouptotal
    }
  }
  // Footer row
  tr {
    td { mkp.yieldUnescaped '&nbsp;' }
    allQuarters.each { q ->
      td { mkp.yieldUnescaped '&nbsp;' }
    }
    td summary.grandtotal.find { it } // Get the first non-empty grandtotal
  }
}
println writer.toString()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...