Export XML to Excel spreadsheet using Javascript/classic-ASP - javascript

I'm trying to export XML data from a web page to a spreadsheet using javascript. So far I've just tried the very simple:
Sub Export
Response.ContentType = "application/vnd.ms-excel"
<script>
var XML = document.getElementById("xmldata");
window.open(XML);
</script>
End Sub
This tries to open the XML file as an excel spreadsheet, however it doesn't open because it has invalid characters. I've tracked down these characters and the main culprit tends to be the horizontal dash "–". There may be other invalid characters, but If I remove them manually, the XML file opens fine.
How would I go about formatting the XML content to remove or replace the invalid characters to display properly to excel? The XML sheet is built from fields in a database, should I format it as it's built or format it with javascript? I'm trying to find the simplest solution possible as I'm fairly new to web programming. Also, I'm using classic ASP/VBscript.

Managed to solve it by getting the HTML table output from the XML data then putting that into Excel using ActiveX.
var x=listingTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++)
{
var y = x[i].cells
for (j = 0; j < y.length; j++)
{
xls.Cells( i+1, j+1).Value = y[j].innerTex
}
}

You have a mismatch in character encoding. Likely the client is assuming UTF-8 character encoding but you are sending from the server an ANSI encoding such as Windows-1252.
You need to ensure that the client knows that the server is sending Windows-1252 using Response.CharSet = "Windows-1252" in you server side code and including the following at the start of your xml:-
<?xml version="1.0" encoding="Windows-1252" ?>
Alternatively if you are using Response.Write to send the XML content you could use:-
Response.Codepage = 65001
Response.CharSet ="UTF-8"

Related

Can I extract a formatted string from a PDF File name in JavaScript?

I have seen some questions regarding extracting information from a PDF file using JS.
I am wondering if there would be anyway to build a program where I can drag upload a file ( or group of files ) into a GUI and then convert the PDF file names into a table of strings... but formatted.
I have a bunch of PDF formatted like
“First.Last.Date”
My job requires me to go through a long list of PDF files, manually read the names within, and then search for them in a database, and if I could do this in a JavaScript program then my workflow time would be cut in half ( if not more ).
Sorry if this question lacks any code. I am not sure if this is worth perusing in JavaScript and not sure where to start with file uploads.
I'm not exactly clear on what you want to do with the file names once you've got them, but the following code will get you the file names.
document.getElementById('myfiles').onchange = function (e) {
var fileList = [];
for (var i = 0; i < e.target.files.length; i++) {
fileList.push(e.target.files[i].name);
}
console.log(fileList);
};
<label for="myfile">Select PDF file(s):</label>
<input type="file" id="myfiles" name="myfiles" multiple>

Google Scripts: XML parsing errors

I have a google script that locates a specific .zip folder on a server, extracts the files, and takes a specific .xml file to be processed. My problem is getting this file into the proper format.
The applicable snippet of code:
var dir = UrlFetchApp.fetch(url);
var b = dir.getBlob();
var files = Utilities.unzip(b);
var vesselDataBlob;
for (var i = 0; i < files.length; i++) {
if (files[i].getName().equals("dat/vesselDataMod.xml")) { //finds file with appropriate name
vesselDataBlob = files[i];
break;
}
}
var vesselData = vesselDataBlob.getDataAsString(); // Returns FULL document as a string.
var data = XmlService.parse(vesselData); // Throws error.
vesselData is in xml format, and vesselData.getContentType() returns "text/xml".
However, I'm struggling to find a way to parse the data. XmlService.parse(vesselData) throws an error: "Content is not allowed in prolog." I tried using DOMParser, which also throws an error. Is there something wrong with how I've set up my code? Is the data not actually in xml format?
The obvious difference between what most people probably do and my situation is that I'm pulling a file from a zipped folder, instead of just straight from a website. That's not the problem, I've tried just using a xml file uploaded to Drive, and the same problem occurs.
I can set up string manipulation to get the data I need, but I'd rather not go through the effort if someone can help out. Thanks!
I've been using this snippet of xml for debugging:
<?xml version="1.0" encoding="UTF-8"?>
<vessel_data version="2.1">
<hullRace ID="0" name="TNS" keys="player">
<taunt immunity="Yadayada" text="More yadayada"/>
</hullRace>
</vessel_data>
The following function works for me with a very simple zip file. I recommend that you try getDataAsString("UTF-8") and see if that resolves the issue.
function test() {
var f = DriveApp.getFilesByName("ingest.zip").next();
var files = Utilities.unzip(f.getBlob());
for(var i=0; i<files.length; i++) {
var ff = files[i];
if (/\.xml$/.test(ff.getName())){
var s = XmlService.parse(ff.getDataAsString());
Logger.log(s);
s = XmlService.parse(ff.getDataAsString("UTF-8"));
Logger.log(s);
break;
}
}
}
I put your XML file into a gist (as XML, not zip) and it parses.
function test2() {
var f = UrlFetchApp.fetch("...gisturl.../test.xml").getBlob(
);
var s = XmlService.parse(f.getDataAsString());
Logger.log(s.getDescendants().length);
}
Unfortunately, I am now having trouble getting Utilities.unzip() to run on a zip file uploaded to Google Drive. Hopefully another user will give you a better solution.

Writing french characters to CSV file with CasperJS, but not displayed correctly

I'm using CasperJS, but none of this has to do with the web scraping part.
I'm printing a string (containing french characters) to a csv. When I later open that csv file, all of the french characters don't display correctly.
var fs = require('fs');
var content = 'ë,è,é';
var path = 'target.csv';
fs.write(path, content, 'w');
This ends up being written as �
Is there any way, in the JS script, I can change the encoding of the CSV? I know about the notepad save-as trick but it needs to be automated. If not, is it possible to do through command line?
Excel (at least my version) needs the Byte Order Mark to properly display UTF-8 content from a CSV file. You can achieve this by prepending the content with the \uFEFF unicode character in JavaScript.
Here's the full script in PhantomJS for simplicity (CasperJS works in the same way since it runs in PhantomJS):
var fs = require('fs');
var content = 'ë,è,é';
var path = 'data.csv';
fs.write(path, "\uFEFF" + content, 'w');
phantom.exit();
Make sure that the script file itself is also UTF-8 encoded.

Problem while parsing pixdaus feed

I am trying to perform parsing of Pixdaus feed at client side using java script.
I am facing following problems:
I am unable to perform parsing directly from this url: http://feeds.feedburner.com/PizdausPopularTodayPics
So I tried to copy the content and save the file at this location: http://db.tt/5Zq98Kw while doing so I am unable to get content within description tags
Can anyone help me to resolve this?
The sample file which I am using to parse it can be found at: http://db.tt/MyIeYrc
Note: currently it supports parsing on firefox only.
Thanks,
Miraaj
Loading XML document from Pixdaus did not work due to 'Cross-domain' issues.
Extract description
I would rather extract the description from the document in the following way:
var desc = xmlDoc.getElementsByTagName('description');
for( var i = 0, len = desc.length; i < len; ++i ) {
console.log( desc[i].firstChild.nodeValue );
}
Hope this helps.

Javascript Download to excel sheet

There is an HTML table as
<table>
<tr><th>NAME</th></tr>
<tr><td>SAL</td></tr>
<tr><td>TOM</td></tr>
<tr><td>SAM</td></tr>
<tr><td>Jenny</td></tr>
</table>
Download to excel sheet
On click hyperlink how to save the table to excel sheet
You might want to try using the XLSX.js lib http://blog.innovatejs.com/?tag=xlsx-js
There is an example there on how to export to excel which gives examples.
Note this exports to the XLSX format not XLS. But this should not be a problem for most use. The source is on GitHub: https://github.com/stephen-hardy/xlsx.js
You may want to take a look at table2CSV, since Excel can open csv files with no problem (and as a bonus other software can do it too, like OpenOffice). If you need it to be cross-browser, there's no way of generating a downloadable file, for that you need a server side script like the one in the example in the page I linked.
I have developed a product called scriptscraper which is designed to solve that problem.
Get the trial installed because the demonstration projects download data from yahoo finance and store the data in Excel. For that simple html table it would be no problem to do what you need.
Try this:
function makeSheet() {
var x = theTable.rows
var xls = new ActiveXObject("Excel.Application")
xls.visible = true
xls.Workbooks.Add
for (i = 0; i < x.length; i++){
var y = x[i].cells
for (j = 0; j < y.length; j++){
xls.Cells( i+1, j+1).Value = y[j].innerText
}
}
}

Categories

Resources