So, I have found several other SO questions with related answers and tried most of them but nothing seems to be working for me. May be because my case is much complicated than the previous ones.
Here is my csv file which has several special characters [, ' "]
ID,Name,Executable,Host,Timeout,Tags,Visible,
65,one test,SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams "one,two",example.com,1800,nothing,true,
My Expected excel format is:
ID: 65
Name: one test
Executable: SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams "one,two"
Host: example.com
Timeout: 1800
Tags: nothing
Visible: true
But due to comma in Executable field, "two" comes in new column.
I tried quoting everything under """ (as suggested in other answers) but that doesn't seems worked.
Can anyone please help me here.
Here is my code, which I am trying to use here (for ag-grid table )
$scope.export_data_csv = function(){
var LINE_SEPARATOR = '\r\n';
var COLUMN_SEPARATOR = ',';
var fileName = 'export.csv';
let csvString = '';
let columnsToExport = $scope.ag_grid_options.api.columnController.getAllDisplayedColumns();
// adding column headers.
columnsToExport.map((column) => {
csvString+= column.colDef.headerName;
csvString+= COLUMN_SEPARATOR;
});
csvString+= LINE_SEPARATOR;
// adding content of data currently loaded in the grid.
$scope.ag_grid_options.api.forEachNode( function(node) {
node.columnController.allDisplayedColumns.map((column) => {
let cellContent = node.valueService.getValue(column, node);
if(typeof(cellContent) == 'object'){
cellContent = cellContent.join("; ")
}
csvString+= (cellContent != null) ? cellContent : "";
csvString+= COLUMN_SEPARATOR;
});
csvString+= LINE_SEPARATOR;
});
// for Excel, we need \ufeff at the start
var blobObj = new Blob(["\ufeff", csvString], {
type: "text/csv;charset=utf-8;"
});
// Internet Explorer
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blobObj, fileName);
}
else {
// Chrome
var downloadLink = document.createElement("a");
downloadLink.href = window.URL.createObjectURL(blobObj);
downloadLink.download = fileName;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
;
You have to declare the whole value of 'Excecutable' as string and escape the double quotes inside with an additional double quote.
ID,Name,Executable,Host,Timeout,Tags,Visible,
65,one test,"SomePROXY=http://10.10.10.10 /abc/cde/efg/some.py -u tom -g 'some-abc-test' --passparams ""one,two""",example.com,1800,nothing,true,
Related
I've created a number of JS scripts similar to below which generate a .tsv download of the webscraped data (this particular example assumes you're on the URL of a repo's Contributors page on Gitlab). Everything outputs fine when I open the .tsv in Microsoft Excel, except that the string 'undefined' appears prepended to every value after the header row in the first column only
How do I edit the script to omit undefined from appearing? Even if it's a simple fix, it will allow me to clean up a bunch of scripts' similar output scraping other websites.
javascript:(function(){
var arr = new Array, i, commitsemail, commitsnum, email, fullname, matchnum;
var regexnum = /.+?(?=commits)/g; var regexemail = /(?<=\().*(?=\))/g;
var glab = document.querySelectorAll('div.col-lg-6.col-12.gl-my-5');
var strings='Full name'+'\t'+'Email'+'\t'+'# of commits'+'\r\n';
var endurl = document.URL.split(/[/]+/).pop(); if (endurl != 'master') {
alert('You are not on the contributors page of a Gitlab repo. Press Esc key, go to URL ending with /master and re-run this bookmarklet'); } else {
for (i = 0; i<glab.length; i++) {
fullname = glab[i].getElementsByTagName('h4')[0].textContent;
commitsemail = glab[i].getElementsByTagName('p')[0].textContent;
commitsnum = [...commitsemail.match(regexnum)];
email = [...commitsemail.match(regexemail)];
arr[i] += fullname + '\t' + email + '\t' + commitsnum;
strings += arr[i]+'\r\n'; }
var pom = document.createElement('a');
var csvContent = strings; var blob = new Blob([csvContent],{type: 'text/tsv;charset=utf-8;'});
var url = URL.createObjectURL(blob); pom.href = url; pom.setAttribute('download','gitlab-contributors.tsv'); pom.click(); }
})();
It's because of the += on the line with arr[i] += fullname + '\t' + email + '\t' + commitsnum;. Change that to an = instead.
Before the assignment, arr[i] is undefined. Maybe you mixed up the syntax for assigning an array entry by index, with appending to an array (arr.push(...)), thinking += would push, but it doesn't. It appends the new value to the current value. And since that line is the first time arr[i] is assigned anything, the current value is undefined.
I wrote a script for InDesign and used doScript to run a bat file. This script works on some systems and not on others.
The error is in the image.
I run as admin InDesign. But it gives another error.
How can I fix the error?
function batFile(str) {
var path = "~\\AppData\\Roaming\\test\\";
var filename = 'b1.bat';
var file1 = new File(path + filename);
file1.encoding = 'UTF-8';
file1.open('w');
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt"
file1.write(txt);
file1.close();
var cmdcode = "CreateObject(\"WScript.Shell\").Run \"%appdata%\\test\\b1.bat\", 0, True";
app.doScript(cmdcode, ScriptLanguage.visualBasic, undefined, UndoModes.FAST_ENTIRE_SCRIPT);
var result = path + "t1.txt";
var arry = openFile2(result);
if (arry.length != 0) {
return arry;
} else {
return "null";
}
}
**
Update
I find the problem.
When a user name is composed of two parts, such as "your name" causes this problem. To address this issue, we need to put the address in two double quotations.
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">\"%appdata%\\test\\t1.txt"\"
**
Update 2
In Windows, when the user uses OneDrive, the AppData path also changes, which causes doScript not to run. for example:
c: \ users \ username \ appdata
Changes to:
c: \ users \ username \ onedrive \ appdata
This is a known bug reported in the UserVoice: https://indesign.uservoice.com/forums/601180-adobe-indesign-bugs/suggestions/41072476-type-library-is-not-automatically-created-by-cc202. A common trick to solve this is, run InDesign at least once as administrator on the client machine. This activates VBS support.
I can't tell what the problem with your script. But just in case, you can run bat file this way:
var bat_file = File("your_file.bat");
bat_file.execute();
Update
Based on your code I can offer the workaround: add & echo ok > %appdata%\\test\\ok.txt in your bat file and check if the file ok.txt exists before going further.
function batFile(str) {
var path = "~\\AppData\\Roaming\\test\\";
var filename = 'b1.bat';
var file1 = new File(path + filename);
file1.encoding = 'UTF-8';
file1.open('w');
var txt = "systeminfo | findstr /B /C:\"OS Name\" /C:\"OS Version\">%appdata%\\test\\t1.txt";
// add the file 'ok.txt' after the previous command is finished
txt += " & echo ok > %appdata%\\test\\ok.txt";
file1.write(txt);
file1.close();
// run the bat file
file1.execute();
// check if the file 'ok.txt' exists before going further
var ok = File(path + "ok.txt");
while (!ok.exists) $.sleep(100);
ok.remove();
// do stuff
var result = path + "t1.txt";
var arry = openFile2(result);
if (arry.length != 0) {
return arry;
} else {
return "null";
}
}
I have one doubt because I need to read a local file and I have been studying some threads, and I have seen various ways to handle it, in most of the cases there is an input file.
I would need to load it directly through code.
I have studied this thread:
How to read a local text file?
And I could read it.
The surprising part was when I tried to split the lines and words, it showed: � replacing accent letters.
The code I have right now is:
myFileReader.js
function readTextFile(file) {
var rawFile = new XMLHttpRequest();
rawFile.open("GET", file, false);
rawFile.onreadystatechange = function () {
if (rawFile.readyState === 4) {
if (rawFile.status === 200 || rawFile.status == 0) {
allText = rawFile.responseText;
console.log('The complete text is', allText);
let lineArr = intoLines(allText);
let firstLineWords = intoWords(lineArr[0]);
let secondLineWords = intoWords(lineArr[1]);
console.log('Our first line is: ', lineArr[0]);
let atlas = {};
for (let i = 0; i < firstLineWords.length; i++) {
console.log(`Our ${i} word in the first line is : ${firstLineWords[i]}`);
console.log(`Our ${i} word in the SECOND line is : ${secondLineWords[i]}`);
atlas[firstLineWords[i]] = secondLineWords[i];
}
console.log('The atlas is: ', atlas);
let atlasJson = JSON.stringify(atlas);
console.log('Atlas as json is: ', atlasJson);
download(atlasJson, 'atlasJson.txt', 'text/plain');
}
}
};
rawFile.send(null);
}
function download(text, name, type) {
var a = document.getElementById("a");
var file = new Blob([text], {type: type});
a.href = URL.createObjectURL(file);
a.download = name;
}
function intoLines(text) {
// splitting all text data into array "\n" is splitting data from each new line
//and saving each new line as each element*
var lineArr = text.split('\n');
//just to check if it works output lineArr[index] as below
return lineArr;
}
function intoWords(lines) {
var wordsArr = lines.split('" "');
return wordsArr;
}
The doubt is: how could we handle those special character which are the vowels with accent?
I ask this, because even in the IDE thet interrogation marks appeared if we load the txt in UTF-8, so then I changed to ISO-8859-1 and it loaded well.
Also I have studied:
Read UTF-8 special chars from external file using Javascript
Convert special characters to HTML in Javascript
Reading a local text file from a local javascript file?
In addition, could you explain if there is a shorter way to load files in client javascript. For example in Java there is the FileReader / FileWriter / BufferedWriter. Is theren in Javascript something similar?
Thank you for you help!
It sounds like the file is encoded with ISO-8859-1 (or possibly the very-similar Windows-1252).
There's no BOM or equivalent for those encodings.
The only solutions I can see are:
Use a (local) server and have it return the HTTP Content-Type header with the encoding identified as a charset, e.g. Content-Type: text/plain; encoding=ISO-8859-1
Use UTF-8 instead (e.g., open the file in an editor as ISO-8859-1, then save it as UTF-8 instead), as that's the default encoding for XHR response bodies.
Put your text in an .html file with the corresponding content type,
for example:
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
enclose the text between two tags ("####" in my example) (or put in a div)
Read the html page, extract the content and select the text:
window.open(url); //..
var content = newWindow.document.body.innerHTML;
var strSep="####";
var x = content.indexOf(strSep);
x=x+strSep.length;
var y = content.lastIndexOf(strSep);
var points=content.slice(x, y);
I am using a nodeJS program as a server and an AngularJS web application as the client.
To create the CSV I'm using the "express-csv" library (https://www.npmjs.com/package/express-csv)
Here is my server side code:
Defines:
var app = express();
var csv = require('express-csv');
Get code:
app.get('/exportDB', function(req, res){
res.csv([
["a", "b", "c"]
, ["d", "e", "f"]
]);
Here is my client side code:
$http.get("http://"+$localStorage.ip+":"+$localStorage.port+"/exportDB").success(function(response){
// HERE I NEED A WAY TO DOWNLOAD THE RECEIVED CSV
});
Needless to say it reaches the server and everything else is working just fine, but I couldnt find a way to download the CSV. Help please.
P.S
Please don't say it's a duplicate of Prompt a csv file to download as pop up using node.js and node-csv-parser (node module) since the client side isn't really mentioned there.
Also, other questions are focused on server side instead of client.
There is no other question referring to AngularJS client.
You can just navigate:
location.href = "http://"+$localStorage.ip+":"+$localStorage.port+"/exportDB";
I faced same issue that the solutions mentioned above with works well with Chrome and Firefox, but not with safari/IE.
Tried out following hack and it worked for me-
var url="http://'+$localStorage.ip+':"+$localStorage.port+'/exportDB';
var form = angular.element("<form action='" + url +"'></form>");
form.submit();
File download will be handled by browser itself.
though following is limitation to it -
You won't be able to handle the errors (if any) from your http get call :( For this tried using try-catch block of javascript, but didn't work well...
..you wont be able to unit test this piece of code :( :(
There is another approach that worked and it was -
var url="http://'+$localStorage.ip+':"+$localStorage.port+'/exportDB';
$window.location.href = url;
Suggestions and Discussions welcome!
You can create a tag and click on it:
$http.get("http://"+$localStorage.ip+":"+$localStorage.port+"/exportDB").success(function(response) {
var dataURI = 'data:application/octet-stream;base64,' + btoa(response);
$('<a></a>').attr({
download: 'db.csv',
href: dataURI
})[0].click();
});
There are ways of downloading csv. First approach is to create a tag and click it
Add the mimeType in below code data:application/octet-stream
var a = document.createElement('a');
a.href = 'data:'+mimeType+';charset=utf-8;base64,' + response;
a.target = '_blank';
a.download = "name the file here";
document.body.appendChild(a);
a.click();
But this solution doesn't work on IE>9 and safari>6
because safari doesn't follow download attribute for anchor tag
so for safari you can use filesaver.js
and IE this solution will work
if (window.navigator.msSaveOrOpenBlob){
// base64 string
var base64str = response;
// decode base64 string, remove space for IE compatibility
var newstr =base64str.replace(/\s/g, '');
var binary = atob(newstr);
// get binary length
var len = binary.length;
// create ArrayBuffer with binary length
var buffer = new ArrayBuffer(len);
// create 8-bit Array
var view = new Uint8Array(buffer);
// save unicode of binary data into 8-bit Array
for (var i = 0; i < len; i++) {
view[i] = binary.charCodeAt(i);
}
// create the blob object with content-type "application/csv"
var blob = new Blob( [view], { type: mimeType });
window.navigator.msSaveOrOpenBlob(blob, "Name your file here");
}
Here I tried to make it simple. Assign all the back end records that you want to display in the file in the variable called obj. I will just say it as var obj = []. And inside the function just add the below code.
var a = document.createElement("a");
var csvContent = "Name, Address\n";
for(var i =0; i <obj.lenght; i++ ) {
var dataString = obj[i].name + ", " + obj[i].address + "\n";
csvContent += dataString;
}
a.href = 'data:attachment/csv;charset=utf-8,' + encodeURI(csvContent);
a.target = '_blank';
a.download = 'myFile.csv';
document.body.appendChild(a);
a.click();
I have this code to export a filtered html table. I have two issues with it which I can't figure out:
1) I get "•" instead of a bullet point • in my exported .csv file. I've tried changing charset from UTF-8 to iso-8859 in meta head and this script but this didn't help.
2) When I have quotes in the sentence it treats it as a comma and breaks the sentence, putting it into a next cell. Example:
I am a "good" man with a good reputation would be broken into something like:
A1 I am a "good" man with
B1 good reputation
I can't work it out why this happens?Thanks!
<script type='text/javascript'>//<![CDATA[
$(function(){
$('#downloadButton').click(function () {
var keepOnlyA1= true // put this true to remove unused numeric A0 field from each row [ A0, A1=[field,field,...]]
//
var a= tf_table1.GetFilteredData(true) // a= [ [A0,A1=[field,field,...]], [A0,A1=[field,field,...]], ...[..,[...]] ]
// ^a[0] ^a[1] ^a[n]
for (var i=0,row,r1; i<a.length; i++){ //
row= a[i] // row= [A0, A1=[field,field,...]]
r1= row[1] // ^r1
for (var j=0; j<r1.length; j++){ // surround each field with quotes "field"
r1[j]= '"'+r1[j].replace('"','""')+'"' // if there is any " already inside field string, it needs to be doubled. per csv rfc.
} //
if (keepOnlyA1) a[i]= r1 // if true, just replace each row array with its single interior A1=[field,field,...] array
} //
var colvals = a.join("\r\n") +"\r\n" //
var blob = new Blob([ colvals ], {type: 'text/csv;charset=ISO-8859;'});
//var filename = $('#fileName').val();
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, "S7_Won_Tendes_Export_Filtered.csv");
}
else {
var a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.style.display= "none" //added// invisible
document.body.appendChild(a) //added// firefox wouldn't click() it without being appended
a.click();
//if (a.remove) a.remove();
a.parentNode.removeChild(a) //added//
}
});
});//]]>
</script>
Ok I managed to find answers:
Replace
var blob = new Blob([ colvals ],
with
var blob = new Blob(["\uFEFF" + buffer],
and
r1[j]= '"'+r1[j].replace('"','""')+'"'
with
r1[j]= '"'+r1[j].replace(/"/g, '""')+'"'