|
From: Fan Fan on 20 May 2005 13:41 hi, I have a comma delimited file contains contacts. I would like to fill the html table with the data from the file instead of hard-code them to the html table by using vbscript in-beded in html code. The html entry maybe like this one. Could someone help me how to automate the process? <tr> <td>Joe</td> <td>(111) 111-1111Cell</td> <td>Accounting Dept Cell</td> </tr> Fan
From: McKirahan on 20 May 2005 14:47 "Fan Fan" <ffan102(a)hotmail.com> wrote in message news:uboJ2MWXFHA.2540(a)tk2msftngp13.phx.gbl... > hi, > > I have a comma delimited file contains contacts. I would like to fill the > html table with the data from the file instead of hard-code them to the html > table by using vbscript in-beded in html code. > > The html entry maybe like this one. Could someone help me how to automate > the process? > <tr> > <td>Joe</td> > <td>(111) 111-1111Cell</td> > <td>Accounting Dept Cell</td> > </tr> > > Fan > Here's one approach. Generate a file containing an HTML table from your CSV file then cut-and-paste it into your Web page. Option Explicit '**** '* This VBS (Visual Basic Script) program does the following: '* Reads a CSV file, formats a HTML table, and writes a HTM file. '*** '* '* Declare Constants '* Const cVBS = "csv2html.vbs" Const cCSV = "csv2html.csv" Const cHTM = "csv2html.htm" '* '* Declare Variables '* Dim arrCSV Dim intCSV Dim strCSV Dim arrDAT Dim intDAT Dim strHTM Dim strSFN strSFN = WScript.ScriptFullName strSFN = Left(strSFN,InStrRev(strSFN,"\")) Dim arrSTR() ReDim arrSTR(100) Dim intSTR intSTR = 0 '* '* Declare Objects '* Dim objFSO Set objFSO = CreateObject("Scripting.FileSystemObject") Dim objOTF '* '* Read CSV Page '* Set objOTF = objFSO.OpenTextFile(strSFN & cCSV,1) strCSV = objOTF.ReadAll() Set objOTF = Nothing '* '* Build table '* Append "<table border=`1` cellpadding=`0` cellspacing=`1`>" arrCSV = Split(strCSV,vbCrLf) For intCSV = 0 To UBound(arrCSV) arrDAT = Split(arrCSV(intCSV),",") Append "</tr>" For intDAT = 0 To UBound(arrDAT) Append "<td>" & arrDAT(intDAT) & "</td>" Next Append "</tr>" Next Append "</table>" '* '* Write HTM File '* strHTM = Concat() Set objOTF = objFSO.OpenTextFile(strSFN & cHTM,2,true) objOTF.WriteLine(strHTM) Set objOTF = Nothing '* '* Destroy Objects '* Set objFSO = Nothing '* '* Finish '* MsgBox UBound(arrCSV)-1 & " rows in table",vbInformation,cVBS Sub Append(strSTR) '**** '* Append() '* '* Appends strings to array entries ReDim as needed; (see "Concat()"). '**** strSTR = strSTR & "" If intSTR > UBound(arrSTR) Then ReDim Preserve arrSTR(UBound(arrSTR) + 100) End If arrSTR(intSTR) = strSTR & vbCrLf intSTR = intSTR + 1 End Sub Function Concat() '**** '* Concat() '* '* Concatenates array entries into a single string; (see "Append()"). '**** Redim Preserve arrSTR(intSTR) Concat = Replace(Join(arrSTR, ""),"`",Chr(34)) Erase arrSTR ReDim arrSTR(100) intSTR = 0 End Function This could be done server-side if you use ASP. Note that if your CSV file contains commas within fields then the approach above won't work; for example, Joe,(111) 111-1111,Accounting Dept,"a,b,c" "a,b,c" will not be treated as a single field.
From: Fan Fan on 20 May 2005 15:11 Hello, McKirahan, Thank you so much for sharing your knowledge. I think many others will benefit from your sample script too. Yes, it is exactly the solution I've been looking for. Thank you again. Fan "McKirahan" <News(a)McKirahan.com> wrote in message news:CJGdnT0k7IH0rRPfRVn-jA(a)comcast.com... > "Fan Fan" <ffan102(a)hotmail.com> wrote in message > news:uboJ2MWXFHA.2540(a)tk2msftngp13.phx.gbl... >> hi, >> >> I have a comma delimited file contains contacts. I would like to fill the >> html table with the data from the file instead of hard-code them to the > html >> table by using vbscript in-beded in html code. >> >> The html entry maybe like this one. Could someone help me how to automate >> the process? >> <tr> >> <td>Joe</td> >> <td>(111) 111-1111Cell</td> >> <td>Accounting Dept Cell</td> >> </tr> >> >> Fan >> > > Here's one approach. > > Generate a file containing an HTML table from your CSV file then > cut-and-paste it into your Web page. > > Option Explicit > '**** > '* This VBS (Visual Basic Script) program does the following: > '* Reads a CSV file, formats a HTML table, and writes a HTM file. > '*** > '* > '* Declare Constants > '* > Const cVBS = "csv2html.vbs" > Const cCSV = "csv2html.csv" > Const cHTM = "csv2html.htm" > '* > '* Declare Variables > '* > Dim arrCSV > Dim intCSV > Dim strCSV > Dim arrDAT > Dim intDAT > Dim strHTM > Dim strSFN > strSFN = WScript.ScriptFullName > strSFN = Left(strSFN,InStrRev(strSFN,"\")) > Dim arrSTR() > ReDim arrSTR(100) > Dim intSTR > intSTR = 0 > '* > '* Declare Objects > '* > Dim objFSO > Set objFSO = CreateObject("Scripting.FileSystemObject") > Dim objOTF > '* > '* Read CSV Page > '* > Set objOTF = objFSO.OpenTextFile(strSFN & cCSV,1) > strCSV = objOTF.ReadAll() > Set objOTF = Nothing > '* > '* Build table > '* > Append "<table border=`1` cellpadding=`0` cellspacing=`1`>" > arrCSV = Split(strCSV,vbCrLf) > For intCSV = 0 To UBound(arrCSV) > arrDAT = Split(arrCSV(intCSV),",") > Append "</tr>" > For intDAT = 0 To UBound(arrDAT) > Append "<td>" & arrDAT(intDAT) & "</td>" > Next > Append "</tr>" > Next > Append "</table>" > '* > '* Write HTM File > '* > strHTM = Concat() > Set objOTF = objFSO.OpenTextFile(strSFN & cHTM,2,true) > objOTF.WriteLine(strHTM) > Set objOTF = Nothing > '* > '* Destroy Objects > '* > Set objFSO = Nothing > '* > '* Finish > '* > MsgBox UBound(arrCSV)-1 & " rows in table",vbInformation,cVBS > > Sub Append(strSTR) > '**** > '* Append() > '* > '* Appends strings to array entries ReDim as needed; (see "Concat()"). > '**** > strSTR = strSTR & "" > If intSTR > UBound(arrSTR) Then > ReDim Preserve arrSTR(UBound(arrSTR) + 100) > End If > arrSTR(intSTR) = strSTR & vbCrLf > intSTR = intSTR + 1 > End Sub > > Function Concat() > '**** > '* Concat() > '* > '* Concatenates array entries into a single string; (see "Append()"). > '**** > Redim Preserve arrSTR(intSTR) > Concat = Replace(Join(arrSTR, ""),"`",Chr(34)) > Erase arrSTR > ReDim arrSTR(100) > intSTR = 0 > End Function > > > This could be done server-side if you use ASP. > > Note that if your CSV file contains commas within fields > then the approach above won't work; for example, > Joe,(111) 111-1111,Accounting Dept,"a,b,c" > "a,b,c" will not be treated as a single field. > >
From: caglaror on 20 May 2005 15:25 ""table by using vbscript in-beded in html code."" Where do you wish the script to do that? A client side or server side? Where the vbs script is working in? Or is it stand alone .vbs file? If it will be only a script file on server you can read the csv and write it on html writing time with serverside scripting. Else you may hang the data any method you chosen and put it into <td> with dhtml. Give your workaround details please. Caglar Orhan
From: Fan Fan on 20 May 2005 16:34
Caglar, Here is the scenario: My company maintains a client contact in a cvs file and that is constantly updated by few admins in the Marketing dept. The majority of employees in the company don't access to this cvs file directly but via the web page in the Intranet. Whenever the cvs file gets modified, the entry then passes to the web admin and in turn, he modifies the html file with that entry. My view is that if the web page, at load-time, can process that csv file and put the data into the html table of that web page, then the web admin (well, at this point, he is only leaning to be fully qualify for that title) should not re-touch the data again. To get my point clear, I will list some code I know of and you may help me to fill-in the the one that I don't know. McKirahan gave me a sample script and that will work. But with his sample script, I think someone has to run it to generate the html file before the user opens it through the Intranet. I welcome any other idea. Again, I am only at the entry level of both the html and the vbs programing. I appreciate any input. Example: Set oSFO = CreateObject("Scripting.FileSystemObject") Set oCSV = oSFO.OpenTextFile("Contacts.csv") Do While NOT oCSV.AtEndOfStream strLine = oCSV.ReadLine arrContacts = Split(strLine,",") For iCnt = 0 to arrContacts(Ubound) IneedTheCodesToFillTheHtmlTable arrContacts(iCnt) Next Loop Would that be possible? "caglaror" <caglaror(a)gmail.com> wrote in message news:1116617104.629420.158190(a)g44g2000cwa.googlegroups.com... > ""table by using vbscript in-beded in html code."" > > Where do you wish the script to do that? A client side or server side? > Where the vbs script is working in? Or is it stand alone .vbs file? > If it will be only a script file on server you can read the csv and > write it on html writing time with serverside scripting. Else you may > hang the data any method you chosen and put it into <td> with dhtml. > Give your workaround details please. > Caglar Orhan > |