style.awk 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. BEGIN{
  2. styles=0
  3. in_style=0
  4. rows=-1
  5. in_cell=0
  6. }
  7. {
  8. if(match($0,"^ *<Table")){
  9. rows=0
  10. print
  11. next
  12. }
  13. if(match($0,"^ *</Table")){
  14. rows=-1
  15. print
  16. next
  17. }
  18. if(match($0,"^ *<Row[ >]")){
  19. if(rows<4)rows++
  20. }
  21. # if(rows>-1){
  22. # if(rows<4)
  23. # gsub(" ss:StyleID=\"\\w+\""," ss:StyleID=\"s1\"",$0)
  24. # else
  25. # gsub(" ss:StyleID=\"\\w+\"","",$0)
  26. # }
  27. # if(match($0,"^ *<Style ")){
  28. # if(styles<3) styles++
  29. # }
  30. if(match($0,"^ *<Cell\\W.*</Cell>$")||match($0,"^ *<Cell\\W.*/>$")||match($0,"^ *<Cell/>$")){
  31. print
  32. next
  33. }
  34. if(match($0,"^ *<Cell(\\W|$)")){
  35. ORS=""
  36. in_cell=1
  37. print
  38. next
  39. }
  40. if(in_cell==1){
  41. gsub("^ +"," ",$0)
  42. }
  43. if(match($0,"</Cell>$")){
  44. in_cell=0
  45. }
  46. if(in_cell==1){
  47. ORS=""
  48. gsub("\r?\n","",$0)
  49. }else{
  50. ORS="\n"
  51. }
  52. if(match($0,"^ *<Style ")){
  53. in_style=1
  54. if(styles==0){
  55. styles++
  56. print
  57. next
  58. }
  59. else if(styles==1) {
  60. gsub("ss:ID=\"\\w+\"","ss:ID=\"s1\"",$0)
  61. styles++
  62. print $0
  63. next
  64. } else {
  65. if(styles<3) styles++
  66. in_style=1
  67. }
  68. }
  69. if(match($0,"^ *</Style>")){
  70. in_style=0
  71. if(styles>2 )next
  72. }
  73. if(in_style && styles>2 )next
  74. print $0
  75. }