I'm using VBScript to parse CSV data and display it on a table. I've got the CSV set up and when I open it in Sublime Text, the headers and content are on different lines (there's only one row of content besides the header). It shows two lines in Sublime Text.
The headers don't have a comma at the end of the row so it doesn't break it up and just basically appends the first entry of the second line on to the last entry of the headers line because it's not technically divided by a comma.
The code I'm using is:
<html>
<head>
</head>
<body>
<%#language="vbscript"%>
<table border="1">
<%
dim csv_to_read, fso, act
csv_to_read="sample.csv"
set fso = createobject("scripting.filesystemobject")
set act = fso.opentextfile(server.mappath(csv_to_read))
'Read the first line of the csv, typically these are colum headings
Response.Write "<tr><th>" & replace(act.readline,",","</th><th>") & "</th></tr>" & vbCrLf
'Read the rest of the csv
Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
%>
<caption>Total Number of Records: <%=act.Line-1%></caption>
</table>
</body>
</html>
Why is it not recognizing the line break?
Thanks for your help!
I think your problem is caused by the file not having a EOL marker of vbCrLf (but crLf probably). Evidence:
s = "1,2,3" & vbLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbLf"
WScript.Echo h
s = "1,2,3" & vbCrLf & "4,5,6"
h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
WScript.Echo "vbCrLf"
WScript.Echo h
output:
cscript 23072652.vbs
vbLf
<tr><td>1</td><td>2</td><td>3
4</td><td>5</td><td>6</td></tr>
vbCrLf
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
(I hope that the output will prove to the looping advocates that the double replace produces valid HTML)
Update wrt comment:
This version of the script:
s = "1,2,3" & vbCrLf & "4,5,6"
WScript.Echo ".ReadAll() (faked):"
WScript.Echo s
' h = "<tr><td>" & replace(replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>"),",","</td><td>") & "</td></tr>"
' step by step
h = replace(s,vbCrLf,"</td></tr>" & vbCrLf & "<tr><td>")
WScript.Echo "step 1 - EOL handling:"
WScript.Echo h
h = replace(h,",","</td><td>")
WScript.Echo "step 2 - comma handling:"
WScript.Echo h
h = "<tr><td>" & h & "</td></tr>"
WScript.Echo "step 3 - head & tail I:"
WScript.Echo h
h = "<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>" & vbCrLf & h & vbCrLf & "</table></body></html>"
WScript.Echo "step 4 - head & tail II:"
WScript.Echo h
output:
cscript 23072652.vbs
.ReadAll() (faked):
1,2,3
4,5,6
step 1 - EOL handling:
1,2,3</td></tr>
<tr><td>4,5,6
step 2 - comma handling:
1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6
step 3 - head & tail I:
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
step 4 - head & tail II:
<!DOCTYPE html><html><head><title>replace demo</title></head><body><table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
</table></body></html>
should make visible how the double replace works and that all parts are important.
Just a quick glance and it looks like you're writing out malformed HTML.
Response.Write "<tr><td>" & replace(replace(act.readall,vbCrLf,"</td></tr>"&vbCrLf&"<tr><td>"),",","</td><td>") & "</td></tr>"
I read the inner replace as replace every carriage return with a closing table cell/table row
The outer replace is replacing the commas with a closing cell/opening cell
You're then appending another closing cell/closing row.
In my opinion, looping through rows, whether or not is more performance efficient is easier to read and debug down the road. If this applies to your situation please consider something like these:
Reading csv file in classic asp. Problem: column values are truncated up to 300 characters
How to read CSV files line by line in VBScript
Related
This is how I am constructing my JSON array to be sent via the PUT method where I am taking some cell value from excel:
body = "{""note"":""" & Cells(RowNote, 3).Value & """,""uniqueIdentifier:""" & Cells(RowNote, 2).Value & ",""IdentifierType"":""ACCOUNT_ID"",""CustomerId"":" & userID & "}"
However i got an error 13 mismatch.
This is an example of the JSON string that can be PUT correctly:
{"note":"call again", "uniqueIdentifier":1716, IdentifierType":"ACCOUNT_ID", "CustomerId":927560}
What should be corrected within the brackets?
Try body = "{""note"":""" & Cells(RowNote, 3).Value & """,""uniqueIdentifier"":" & Cells(RowNote, 2).Value & ",""IdentifierType"":""ACCOUNT_ID"",""CustomerId"":" & UserId & "}" if you do not want " around your integers.
I am trying to append a CSV's last row and allow it to be read by a C# user control.
The reason we have to do that way is because the data become available at two different times.
It works fine except for the following:
It generates an empty line after appending.
After repeating it for 3-4 steps it deletes the whole file.
The code is below:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile("R:\cat3_data.csv", 1)
strContents = objInFile.ReadAll
objInFile.Close
arrRows = Split(strContents, vbNewLine)
MsgBox UBound(arrRows)
arrRows(UBound(arrRows) - 1) = arrRows(UBound(arrRows) - 1) & "," & "Hi" & _
"," & "Hi" & "," & "Hi" & "," & "Hi" & "," & "Hi"
strContents = Join(arrRows, vbNewLine)
Set objOutFile = objFSO.OpenTextFile("R:\cat3_data.csv", 2)
objOutFile.WriteLine strContents
objOutFile.Close
Set objFSO = Nothing
unfortunately I am forced to use ASP for this particular task. I need my form to post its data to a CSV and then send an email notifying the client. Now the first bit I already have working (posting the data to a CSV file) but its the email notification that is getting me, as I have no idea how or what to incorporate into my current code in order for this to happen.
Here is a link to the form in question:
http://dev.brandspank.co.za/form/index.html
And below is my ASP code:
<% option explicit
dim objFSO, objTXT, lines, newRecord, filePath, fieldNames, x, fullText, nLine %>
<h1>Form inputs posted:</h1>
<%
for each x in request.form
response.write x & ": " & Replace(request.form(x), ",","-") & "<br />" & vbNewLine
next
'set filepath for plain text db. This neds to be the absolute path of file
filePath = "\form\myCSVdb.csv"
set objFSO = server.createobject("Scripting.FileSystemObject")
if (objFSO.fileExists(filePath))=true then
set objTXT = objFSO.openTextFile(filePath, 1) 'opens a text file for
' reading, true means it will create the file if not already there
fullText = trim(objTXT.readall)
lines = split(fullText, vbNewLine) 'lines is now an array, each item is
' one line of the db file.this could now be used to list the entire db
' table, notice next 3 commented out lines
' for each x in lines
' response.write lines(x)
' next
objTXT.close
set objTXT = nothing
if trim(lines(0)) = "" then fullText = ""
else
fullText = ""
end if %>
<h1>added to the db:</h1>
<%
if fullText <> "" then 'there are already field names in the db,
' so put the new line in the same order
set objTXT = objFSO.openTextFile(filePath, 8, True) 'opens the text file for
' appending
response.write "(fields in the database: " & lines(0) & ")<br />" & vbNewLine
'split the first line, which had field names into an array -fieldNames-
fieldNames = split(lines(0), ",")
response.write "field values entered:<br />" & vbNewLine
for each x in fieldNames
if x <> "" then
nLine = nLine & Replace(request.form(x), ",","-") & ","
'adds each form input to a string
response.write x & ": " & Replace(request.form(x), ",","-") & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'removes trailing comma
objTXT.writeLine nLine
else 'there isn't anything in the textfile yet, so put in the field names first
set objTXT = objFSO.openTextFile(filePath, 2, True) 'opens the text file for
' writing
response.write "field names enterd:<br />" & vbNewLine
for each x in request.form
if Replace(lcase(x), ","," ") <> "submit" then 'or you will have a "submit" field in your db
'of course, if your submit button is named something else, that should
'be the name xcluded here
nLine = nLine & x & ","
'adds each form input name to a string which will become th first line of
'the db, the line which shows field names
response.write x & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma
objTXT.write nLine
objTXT.write vbNewLine
nLine = ""
response.write "field values entered:<br />" & vbNewLine
for each x in request.form
if lcase(x) <> "submit" then
nLine = nLine & Replace(request.form(x), ",","-") & ","
'adds each form input to a string
response.write Replace(request.form(x), ",","-") & "<br />" & vbNewLine
end if
next
nLine = left(nLine, len(nLine)-1)
'remove trailing comma
objTXT.write nLine
objTXT.write vbNewLine
end if
objTXT.close
%>
<% response.redirect "thankyou.html" %>
Any help would be very much appreciated!
I put pretty much everything you need here:
www.oceanmedia.net/files/2014-06-form-process.zip
Included:
form_process. asp
i_fn_email_cdo.asp
i_check_security.asp
i_odbc.asp
i_fn_clean.asp
i_fn_dirty.asp
Oh and the text file part:
<%
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for reading
Set f = fso.CreateTextFile(s_path & "/" & s_file_being_created, True)
f.Write(m)
f.Close
Set f = Nothing
Set fso = Nothing
%>
Sorry, I don't yet have comment privileges, so this will be a comment disguised as an answer: the specifics will depend upon how your web server is configured in terms of email, but assuming it is somewhat current, you will probably be using CDOSYS to do so, and here is a link to a very basic intro: http://www.w3schools.com/asp/asp_send_email.asp
You've navigated the FileSystemObject, so you'll probably find this fairly straightforward once you get the details right on your mail server.
I am trying to covert spread sheet with hundreds of image path to html pages. For example:
excel sheet
imageId imagePath
1 images/ima1
2 images/ima2
3 images/ima3
... .........
... ........
Are there faster ways to covert these images to html files instead of typing them 1 by 1?
The html files I need is like the following:
<img src=images/ima1 title='ima1' />
<img src=images/ima2 title='ima2' />
<img src=images/ima3 title='ima3' />
Thanks for the help.
There are several ways you can do what you want.
1 . On another sheet, create a formula that will take the information from the first sheet and produce the HTML you want.
="<img src=" & CHAR(34) & INDIRECT("Sheet1!B" &ROW()) & CHAR(34) & " title=" & CHAR(34) & INDIRECT("Sheet1!A" & ROW()) & " />"
Assuming your data is on Sheet1, every row on your second sheet (with that formula in) will contain the html of the same row on Sheet1. (You'd need to adjust the sheet name and columns if there not the same)
Doing this you can then copy and paste everything from the 2nd sheet.
2 . Using a macro to generate the file.
Public Sub GenerateHTML()
Dim Handle As Integer
Dim Sheet As Worksheet
Dim Row As Integer
Set Sheet = ThisWorkbook.ActiveSheet
Row = 2
Handle = FreeFile()
Open "output.html" For Output As Handle
Print #Handle, "<html>" & vbNewLine & "<head>" & vbNewLine & "<title>My Gallery...</title>" & vbNewLine & "</head>" & vbNewLine & "<body>"
Do
If Sheet.Cells(Row, 1) = "" Then
Exit Do
Else
Print #Handle, "<img src=" & Chr(34) & Sheet.Cells(Row, 2) & Chr(34) & " title=" & Chr(34) & "ima" & Sheet.Cells(Row, 1) & Chr(34) & "/>"
Row = Row + 1
End If
Loop
Print #Handle, "</body>" & vbNewLine & "</html>"
Close #Handle
End Sub
This example will create a html file in the same directory as the spreadsheet containing all the links. You'd need to adjust it to suit your needs as its probably not quite right.
I would create a formula in the next column. Assuming that images/ima1 is in B2 this would be something you could put into C2 to get you what you're after and then paste the formula down.
="<img src="&B2&" title='"&RIGHT(B2,LEN(B2)-4)&"' />"
Finally the administrator configured the IIS for me the error message is listed below.
Set SQLStream = CreateObject("ADODB.Stream")
Set SQLConnection = CreateObject("ADODB.Connection")
Set SQLCommand = CreateObject("ADODB.Command")
Set SQLRecordSet = CreateObject("ADODB.RecordSet")
SQLConnection.Open "Provider=sqloledb;SERVER=SQLPROD;DATABASE=MyDataBase;UID=MyUsername;PWDMyPassword;"
'Response.Write("Connection Status: " & SQLConnection.State) & vbnewline
'Response.Write("Connection Provider: " & SQLConnection.Provider) & vbnewline
'Response.Write("Version: " & SQLConnection.Version) & vbnewline
SQLCommand.ActiveConnection = SQLConnection
SQLCommand.CommandText = "SELECT Seminars.Year, Seminars.SeminarID, Seminars.Theme, Seminar_Week.First, Seminar_Week.Last, Seminar_Week.WeekID, Seminar_Week.Date, Seminar_Week.Affiliation FROM Seminars CROSS JOIN Seminar_Week"
'Response.Write("SQL Command Passed in: " & SQLCommand.CommandText)
Set adoRec = SQLCommand.Execute()
file1 = "./seminars/" & seminar_type & "/" & seminar_year & "/" & adoRec("Date") & "-" & adoRec("Year") & "_" & adoRec("Last") & ".pdf"
file2 = "./seminars/" & seminar_type & "/" & seminar_year & "/" & adoRec("Date") & "-" & seminar_year & "_" & adoRec("Last") & "(handouts).pdf"
file3 = "./seminars/" & seminar_type & "/" & seminar_year & "/" & adoRec("Date") & "-" & seminar_year & "_" & adoRec("Last") & "_Flyer.pdf"
Set fso = CreateObject("scripting.filesystemobject")
Response.Write("<p style=" & "margin-left:10px;" & "><img src=" & "./img/right_arrowblue.png" & " alt=" & "Expand/Collapse" & " id=" & "arrow_" & adoRec("Week") & " /><strong>[" & adoRec("Date") & "]</strong> " & ""&aroRec("First") & adoRec("Last") & ", " & adoRec("Affiliation") & "</p>")
The very last line of code causes this error
ADODB.Recordset error '800a0cc1'
Item cannot be found in the collection corresponding to the requested name or ordinal.
FilePath, line 244
Line 244 is the very last line of code that should write Some information about each seminar on the webpage.
I'm pretty sure at this point I am pointing to an incorrect file path because I have an extra space somewhere in all the different string.
My question now is Would the ones in the very beginning, meaning the ones used in
"<p style=" & "margin-left:10px;" & "><img src=" & "./img/right_arrowblue.png"
be causing the trouble.
I'm also unfamiliar with using the "Expand/collapse" so if someone could tell me a little more about that. I am trying to fix someone elses code so I am a little behind the 8 ball.
One small step to a solution:
Your SQL
"SELECT * FROM Seminars WHERE [SeminarID] = 5 ORDER BY DESC"
is definitely wrong: ORDER BY needs (at least) a column name: ORDER BY [SeminarID] DESC.
If that does not solve all your problems, we'll have to think about a step by step approach.
If you get errors, tell us about them (number, description, line). That's what I meant, when I ask you to publish them. If you can't better info than "There was an error when processing the URL" from IIS, then you have to write some command line script to get the database related code absolutely right.
Start with experiments.vbs:
Dim sCS : sCS = !your connection string!
Dim oCN : Set oCN = CreateObject("ADODB.Connection")
oCN.Open sCS
WScript.Echo "CN open:", oCN.State
Dim sSQL : sSQL = !your SQL statement!
Dim oRS : Set oRS = oCN.Execute(sSQL)
WScript.Echo "RS EOF:", CStr(oRS.EOF)
WScript.Echo "Frs Col:", oRS.Fields(0).Name, oRS.Fields(0).Type
Dim i : i = 0
Do Until oRS.EOF
WScript.Echo i, oRS.Fields(0).Value
i = i + 1
oRS.MoveNext
Loop
oCN.Close
and run it in a command window (DOS box): cscript experiments.vbs. This should get you either some lines like:
CN open: 1
RS EOF: False
Frs Col: Id 3
0 ...
1 ...
2 ...
or a focused/publishable error message like:
... .vbs(2465, 14) Microsoft OLE DB Provider for SQL Server: Falsche Syntax in der Nä
he des 'DESC'-Schlüsselworts.
(bad syntax near DESC), which got when I tried the statement
"SELECT * FROM Alpha ORDER BY DESC"
RS.MoveNext
Put the above code on the line before the Loop keyword to avoid an infinite loop.
Are you missing the loop keyword at the end of your loop block?
Check the syntax here: http://msdn.microsoft.com/en-us/library/eked04a7.aspx