Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Groovy从表格报表创建数据表_Php_Html_Dictionary_Groovy_Html Table - Fatal编程技术网

Php 使用Groovy从表格报表创建数据表

Php 使用Groovy从表格报表创建数据表,php,html,dictionary,groovy,html-table,Php,Html,Dictionary,Groovy,Html Table,我一直忙于将列表报告转换为表格数据,出于各种原因,我唯一的编程工具是groovy(即没有shell命令、没有C、没有perl)。数据以五列形式预先汇总 分组(如公司)、分组(如季度)、小计、分组总数、总计(汇总数据固定在每组第一行): 并且输出需要类似于: <table> <tr><th>Group</th><th>2009 Q4</th><th>2010 Q1</th><th>2

我一直忙于将列表报告转换为表格数据,出于各种原因,我唯一的编程工具是groovy(即没有shell命令、没有C、没有perl)。数据以五列形式预先汇总

分组(如公司)、分组(如季度)、小计、分组总数、总计(汇总数据固定在每组第一行):

并且输出需要类似于:

<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>

集团2009年第4季度2010年第1季度2010年第2季度2010年第3季度小计
大CO2938308
加甘图阿印加29313639 139
巨型印加39 4281
308
我希望能够在数据上运行一些循环;将事物粘贴到关联数组的关联数组中,在执行时构建完整的键列表;然后再次循环以生成输出。我已经看过groovy,映射语法已经完全抛出

有没有人能给我指出一些更清晰的groovy映射迭代的例子,这样我至少可以在这方面有所了解

感谢您的帮助

(如果有人认为这只是一次钓鱼之旅,我已经拼凑了一个PHP版本来实现这一点——基本上我需要能够将其转换为groovy…)


好的,把这些都准备好了。。。它可能不是最漂亮的——但输入格式也是如此;-)

String txt=''Big Co.\t2009 Q4\t29\t88\t308
\t2010 Q1\t38\t\t
\t2010 Q4\t21\t\t
Gargantua Inc.\t2009第4季度\t33\t139
\t2010 Q1\t31\t\t
\t2010 Q2\t36\t\t
\t2010 Q3\t39\t\t
平庸有限公司\t2009第4季度\t39\t81
\t2010 Q4\t42\t\t''
//解析文本
def summary=txt.split('\n')。注入([]){列表,行->
def split=row.split('\t')。collect{it.trim()}
而(split.size()<5)split
def v=组小计“$q”
如果(v)
td v
其他的
td{mkp.yieldUnescaped'}
}
td group.grouptotal
}
}
//页脚行
tr{
td{mkp.yieldUnescaped'}
AllQuarter.each{q->
td{mkp.yieldUnescaped'}
}
td summary.grandtotal.find{it}//获取第一个非空的grandtotal
}
}
println writer.toString()

好的,把这些都准备好了。。。它可能不是最漂亮的——但输入格式也是如此;-)

String txt=''Big Co.\t2009 Q4\t29\t88\t308
\t2010 Q1\t38\t\t
\t2010 Q4\t21\t\t
Gargantua Inc.\t2009第4季度\t33\t139
\t2010 Q1\t31\t\t
\t2010 Q2\t36\t\t
\t2010 Q3\t39\t\t
平庸有限公司\t2009第4季度\t39\t81
\t2010 Q4\t42\t\t''
//解析文本
def summary=txt.split('\n')。注入([]){列表,行->
def split=row.split('\t')。collect{it.trim()}
而(split.size()<5)split
def v=组小计“$q”
如果(v)
td v
其他的
td{mkp.yieldUnescaped'}
}
td group.grouptotal
}
}
//页脚行
tr{
td{mkp.yieldUnescaped'}
AllQuarter.each{q->
td{mkp.yieldUnescaped'}
}
td summary.grandtotal.find{it}//获取第一个非空的grandtotal
}
}
println writer.toString()

您的数据文件就是这样的吗?就像你粘贴的第一个格式化的东西一样?Ie:
Big Co.
数据分布在3行上?在每行文字前加上空格?是的:-(事实就是这样……每个组的汇总数据都在组的第一行,整个组的汇总数据都在文件的第一行。不,真的是这样。)-(实际上,空格可能是我。但是有一个用于当前组中的行的选项卡,例如上面示例数据中的第2、3、5、6、7和9行。数据文件就是这样的吗?与您粘贴的第一个格式化内容完全相同?即:
Big Co.
数据分布在3行上?每行文本前的空格?是的。)-(事实就是这样……每个组的汇总数据都在组的第一行,整个组的汇总数据都在文件的第一行。不,真的是这样。)-(实际上,空白处可能是我。但是有一个用于当前组中的行的选项卡,例如上面示例数据中的第2、3、5、6、7和9行。Tim-谢谢-它看起来很棒,而且是groovy的广告;-)但是,有一个小问题-似乎抛出了一个errorgroovy.lang.MissingMethodException:没有方法签名:java.util.LinkedHashMap.leftShift()适用于参数类型:(java.util.LinkedHashMap)值:{[“2009 Q4”:“29”]}您正在运行哪个版本的Groovy?我想map上的leftshift是在1.7.2版中添加的,如果您使用的是旧版本并且无法升级,那么该行可以从
list[-1]中更改。小计旧版本?在这项工作中,没有什么会让我感到惊讶;-)我需要运行某种导入来获取java.util.TreeSet吗?Tim-谢谢-它看起来很棒,而且是groovy的广告;-)但是,有一个小问题-似乎抛出了一个错误groovy.lang.MissingMethodException:没有方法签名:java.util.LinkedHashMap.leftShift()适用于参数类型:(java.util.LinkedHashMap)值:{[“2009 Q4”:“29”}您运行的是Groovy的哪个版本?我认为map上的leftshift是在1.7.2版中添加的,如果您使用的是旧版本并且无法升级,那么该行可以从
list[-1]更改。小计旧版本?在这个工作中,没有什么会让我感到惊讶;-)我是否需要运行某种导入来获取java.util.TreeSet?
<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>
<?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>";

?>
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()