* 对于每个table,如果有合并的单元格,需要检查所有的tr td是否具有相同的行数和列数
* @param element
*/
public static void adjustColspanAndRowspan(Element element){
if(element!=null&&element.tagName().equalsIgnoreCase("table")){
Elements trs = element.select("tr");
int totalMaxColumns = 0;
final int[] colspans = new int[trs.size()];
for(int tri=0;tri<trs.size();tri++){
Element tr = trs.get(tri);
// Elements tds = tr.select("td");
Elements tds = tr.children();
int totalColumns = 0;
for(int tdi=0;tdi<tds.size();tdi++){
Element td = tds.get(tdi);
int colspan = 1;
if(td.hasAttr("colspan")){
try{
colspan = Integer.parseInt(td.attr("colspan"));
}catch(NumberFormatException e){
e.printStackTrace();
}
}
totalColumns += colspan;
}
if(totalColumns>totalMaxColumns){
totalMaxColumns = totalColumns;
}
}
for(int tri=0;tri<trs.size();tri++){
Element tr = trs.get(tri);
Elements tds = tr.select("td");
// if(tds.size()<totalMaxColumns){
int totalColumns = 0;
for(int tdi=0;tdi<tds.size();tdi++){
Element td = tds.get(tdi);
int colspan = 1;
if(td.hasAttr("colspan")){
try{
colspan = Integer.parseInt(td.attr("colspan"));
}catch(NumberFormatException e){
e.printStackTrace();
}
for(int coli=0;coli<colspan-1;coli++){
td.after("<td style=\"display: none;\"/>");
}
}
int rowspan = 1;
if(td.hasAttr("rowspan")){
try{
rowspan = Integer.parseInt(td.attr("rowspan"));
}catch(NumberFormatException e){
e.printStackTrace();
}
for(int rowi=1;rowi<rowspan;rowi++){
if(tri+rowi<trs.size()){
Element nextRow = trs.get(tri+rowi);
if(nextRow!=null){
Elements nextRowCells = nextRow.select("td");
for(int coli=0;coli<colspan;coli++){
if(nextRowCells.size()>tdi){
nextRowCells.get(tdi).before("<td style=\"display: none;\"/>");
}
}
}
}else{
Element newTr = element.child(0).append("<tr/>");
for(int newtdi=0;newtdi<tdi;newtdi++){
newTr.append("<td/>");
}
Elements nextRowCells = newTr.select("td");
for(int coli=0;coli<colspan;coli++){
if(nextRowCells.size()>tdi){
nextRowCells.get(tdi).before("<td style=\"display: none;\"/>");
}
}
}
}
}