Advertisement
rg443

aspExl.class.asp

Sep 1st, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <%
  2. ' Classic ASP CSV creator 1.0
  3.  
  4. ' Format constants
  5. const ASPXLS_CSV = 1    ' CSV format
  6. const ASPXLS_TSV = 2    ' Tab separeted format
  7. const ASPXLS_HTML = 3   ' HTML table format
  8.  
  9.  
  10. ' Main class
  11. class aspExl
  12.     dim lines(), curBoundX, curBoundY
  13.     dim headers()
  14.     dim m_prettyPrintHTML
  15.    
  16.    
  17.     ' A flag for outputing more readable HTML
  18.     public property get prettyPrintHTML()
  19.         prettyPrintHTML = m_prettyPrintHTML
  20.     end property
  21.    
  22.     public property let prettyPrintHTML(byval value)
  23.         m_prettyPrintHTML = value
  24.     end property
  25.    
  26.    
  27.     ' Initialization and destruction
  28.     sub class_initialize()
  29.         curBoundX = -1
  30.         curBoundY = -1
  31.         m_prettyPrintHTML = false
  32.     end sub
  33.    
  34.     sub class_terminate()
  35.         ' Destroy all elements of the arrays
  36.         redim lines(-1)
  37.         redim headers(-1)
  38.     end sub
  39.    
  40.    
  41.     ' Resizes the columns and header arrays to fit a new size
  42.     private sub resizeCols(byval newSize)
  43.         dim i, cols
  44.        
  45.         for i = 0 to curBoundY
  46.             cols = lines(i)
  47.             redim preserve cols(newSize)
  48.             lines(i) = cols
  49.         next
  50.        
  51.         redim preserve headers(newSize)
  52.        
  53.         curBoundX = newSize
  54.     end sub
  55.    
  56.    
  57.     ' Resizes the lines array to fit a new size
  58.     private sub resizeRows(byval newSize)
  59.         dim i
  60.         redim preserve lines(newSize)
  61.        
  62.         for i = curBoundY + 1 to newSize
  63.             if i >= 0 then lines(i) = Array()
  64.         next
  65.        
  66.         curBoundY = newSize
  67.        
  68.         resizeCols curBoundX
  69.     end sub
  70.    
  71.    
  72.     ' Contatenates and return the values in a string using a separator string
  73.     private function toString(byval separator)
  74.         dim output, headersString, i
  75.         output = ""
  76.         headersString = join(headers, separator)
  77.        
  78.         if replace(headersString, separator, "") <> "" then output = headersString & vbCrLf
  79.        
  80.         for i = 0 to curBoundY
  81.             output = output & join(lines(i), separator) & vbCrLf
  82.         next
  83.        
  84.         toString = output
  85.     end function
  86.    
  87.    
  88.     ' Sets a header value
  89.     public sub setHeader(byval x, byval value)
  90.         if x > curBoundX then resizeCols(x)
  91.        
  92.         headers(x) = value
  93.     end sub
  94.    
  95.    
  96.     ' Sets the value of a cell
  97.     public sub setValue(byval x, byval y, byval value)
  98.         dim cols
  99.        
  100.         if y > curBoundY then resizeRows y     
  101.         if x > curBoundX then resizeCols x
  102.        
  103.         cols = lines(y)
  104.         cols(x) = value
  105.        
  106.         lines(y) = cols
  107.     end sub
  108.    
  109.    
  110.     ' Sets the values of a range of cells starting at the specified coordinates
  111.     public sub setRange(byval x, byval y, byval arr)
  112.         if y > curBoundY then resizeRows y
  113.        
  114.         dim arrBound
  115.         arrBound = ubound(arr)
  116.        
  117.         if arrBound + x > curBoundX then resizeCols(arrBound + x)
  118.        
  119.         dim i, cols
  120.         cols = lines(y)
  121.        
  122.         for i = 0 to arrBound
  123.             cols(x + i) = arr(i)
  124.         next
  125.        
  126.         lines(y) = cols
  127.     end sub
  128.    
  129.    
  130.     ' Returns a string formatted output of the data
  131.     public function outputTo(byval format)
  132.         dim output, headersString, i
  133.        
  134.         select case format
  135.             case ASPXLS_HTML:
  136.                 output = "<table>"
  137.                 headersString = join(headers, "</th><th>")
  138.                
  139.                 if replace(headersString, "</th><th>", "") <> "" then output = output & "<thead><tr><th>" & headersString & "</th></tr></thead>"
  140.                
  141.                 output = output & "<tbody>"
  142.                
  143.                 for i = 0 to curBoundY
  144.                     output = output & "<tr><td>" & join(lines(i), "</td><td>") & "</td></tr>"
  145.                 next
  146.                
  147.                 output = output & "</tbody></table>"
  148.                
  149.                
  150.                 ' Prettify HTML for easy reading
  151.                 if m_prettyPrintHTML then
  152.                     dim lineSeparator, indentChar
  153.                     dim regex, breakAndIndent, doubleIndent
  154.  
  155.                     lineSeparator = vbCrLf
  156.                     indentChar = vbTab
  157.                    
  158.                     breakAndIndent = lineSeparator & indentChar
  159.                     doubleIndent = indentChar & indentChar
  160.                    
  161.                     set regex = new regexp
  162.                     regex.global = true
  163.                    
  164.                     regex.pattern = "(</?(?:thead|tbody)>)"
  165.                     output = regex.replace(output, breakAndIndent & "$1")
  166.                    
  167.                     regex.pattern = "(</?tr>)"
  168.                     output = regex.replace(output, breakAndIndent & indentChar & "$1")
  169.                    
  170.                     regex.pattern = "(</table>)"
  171.                     output = regex.replace(output, lineSeparator & "$1")
  172.                    
  173.                     regex.pattern = ">(<(?:th|td)>)"
  174.                     output = regex.replace(output, ">" & breakAndIndent & doubleIndent & "$1")
  175.                    
  176.                     set regex = nothing
  177.                 end if
  178.                
  179.             case ASPXLS_CSV:
  180.                 output = toString(";")
  181.                
  182.             case ASPXLS_TSV:
  183.                 output = toString(vbTab)
  184.            
  185.             case default:
  186.                 output = ""
  187.         end select
  188.        
  189.         outputTo = output
  190.     end function
  191.    
  192.    
  193.     ' Returns a semi-colon separated string for each row
  194.     public function toCSV()
  195.         toCSV = outputTo(ASPXLS_CSV)
  196.     end function
  197.    
  198.    
  199.     ' Returns a TAB separated string for each row
  200.     public function toTabSeparated()
  201.         toTabSeparated = outputTo(ASPXLS_TSV)
  202.     end function
  203.    
  204.    
  205.     ' Returns a HTML table formatted string
  206.     public function toHtmlTable()
  207.         toHtmlTable = outputTo(ASPXLS_HTML)
  208.     end function
  209.    
  210.    
  211.     ' Writes a string fomatted output to a file
  212.     public sub writeToFile(byval filePath, byval format)
  213.         dim fso, file
  214.         dim i
  215.         set fso = createObject("scripting.filesyStemObject")
  216.        
  217.         set file = fso.createTextFile(filePath, true)
  218.        
  219.         file.writeLine outputTo(format)
  220.        
  221.         file.close
  222.         set file = nothing
  223.        
  224.         set fso = nothing
  225.     end sub
  226. end class
  227. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement