I created a code that can export a file via excel the code is working fine without error but my problem is the excel file has a lot of spaces can.
as you can see in the image row 123 data is put in column 7 not in column
heres my code for exporting the data
export function download() {
var header = [];
var finalData = []
var group = [
{ "group_name": "123" },
{ "group_name": "123b" },
{ "group_name": "123ef" },
{ "group_name": "Accounts Payable" },
{ "group_name": "ADG JET TEAM" },
{ "group_name": "001 Approval" }
]
var member = [
{"001 Approval": "083817 - Ranjeet Kumar (ranjeet.kumar3#concentrix.com)" },
{ "001 Approval": "C01747 - Abid Shaikh (abid.shaikh1#concentrix.com)"},
{ "001 Approval": "C01747 - Abid Shaikh (abid.shaikh1#concentrix.com)"},
{ "123b": "C01747 - Abid Shaikh (abid.shaikh1#concentrix.com)"},
{
"123ef": "C01747 - Abid Shaikh (abid.shaikh1#concentrix.com)"
}
]
group.forEach(data=>{
header.push(data.group_name)
})
finalData.push(header)
header.forEach(headerData=>{
var temp = []
member.forEach(memberData=>{
if (headerData === Object.keys(memberData)[0]){
temp.push(memberData[Object.keys(memberData)[0]])
}else{
temp.push("")
}
})
finalData.push(temp)
})
exportToCsv('export.csv', finalData)}
the exportToexcel code is from here https://jsfiddle.net/jossef/m3rrLzk0/
Open your CSV in a text editor and and check the output, what are you using as separator and delimiter. This can be a CSV bad format (fields not surrounded by quotes, for ex), or a problem with excel configuration (csv delimiters and separators).
If you have data like that
field1, field2, field 3 supercool, this is a phrase, ops
It can be a problem, it should be something similar to
"field1", "field2", "field 3 supercool", "this is a phrase, ops"
In addition, try to open your csv with Google Sheets (docs), which will try to recognize the delimiters and separators automatically. See if it works.
A common problem for that is that your CSV is saparated by spaces or commas, but phrase can have a space and a comma which will be interpreted as a separator, and the whole document will break.
It can be useful to take a look at that link: Write a string containing commas and double quotes to CSV
Related
I have a problem with parsing an XML file.
I want to remove strings with characters like \t\n.
XML File: http://ftp.thinkimmo.com/home/immoanzeigen24/immo.xml
{
trim: true,
normalize: true,
attrValueProcessors: [cleanValue, name => name],
valueProcessors: [cleanValue, name => name]
}
cleanValue:
const cleanValue = value => {
return value.toString().trim().replace("\t","atest");
};
I tried cleaning it with a lot of regex I've found online - but value always stays like following:
"verwaltung_objekt": {
"objektadresse_freigeben": "0",
"verfuegbar_ab": "nachaasjkdhkjshadjkashdAbsprache",
"bisdatum": "2016-01-15",
"min_mietdauer": "\n\t\t\t\t",
"max_mietdauer": "\n\t\t\t\t",
}
This is a difficult one!
I'd suggest following a simple strategy and pre-processing the xml data before you parse it.
This should resolve your issue at least.
If you just do something like:
function trimXml(xml) {
return xml.replace(/>\s+</g, "><");
}
xml = trimXml(xml);
Then parse the trimmed xml data. You should see the output now looks like so:
"verwaltung_objekt": [
{
"objektadresse_freigeben": [
"1"
],
"abdatum": [
"2017-03-01"
],
"min_mietdauer": [
""
],
"max_mietdauer": [
""
]
}
],
Which is a bit more like what you want!
I have a situation where a user can upload a csv file. This CSV file contains a lot of data, but I am only interested in 2 columns (ID and Date). At the moment, I am parsing the CSV using Papaparse
Papa.parse(ev.data, {
delimiter: "",
newline: "",
quoteChar: '"',
header: true,
error: function(err, file, inputElem, reason) { },
complete: function (results) {
this.parsed_csv = results.data;
}
});
When this is run this.parsed_csv represents objects of data keyed by the field name. So if I JSON.stringify the output is something like this
[
{
"ID": 123456,
"Date": "2012-01-01",
"Irrelevant_Column_1": 123,
"Irrelevant_Column_2": 234,
"Irrelevant_Column_3": 345,
"Irrelevant_Column_4": 456
},
...
]
So my main question is how can I get rid of the columns I dont need, and just produce a new csv containing the columns ID and Date?
Thanks
One thing I realised, is there a way to add dynamic variables. For instance I am letting users select the columns I want to map. Now I need to do something like this
let ID = this.selectedIdCol;
this.parsed_csv = results.data.map(element => ({ID: element.ID, Date: element.Date}));
It is saying that ID is unused however. Thanks
let data = [
{
"ID": 123456,
"Date": "2012-01-01",
"Irrelevant_Column_1": 123,
"Irrelevant_Column_2": 234,
"Irrelevant_Column_3": 345,
"Irrelevant_Column_4": 456
},
...
]
just produce results by using the following code:
data = data.map(element => ({ID: element.ID, Date: element.Date}))
Now you have desired column, please generate a new CSV on these columns
As Serrurier pointed out above, You should use the step/chunk function to alter the data rather than after parse map as in memory data is already available.
PapaParse.parse(file, { skipEmptyLines: true, header: true, step: (results, parser) => {
results.data = _.pick(results.data , [ 'column1' 'column2']);
return results;
}});
Note that if you are loading a huge file, you will have the whole file in memory right after the parsing. Moreover it may freeze the browser due to the heavy workload. You can avoid that by reading and discarding columns :
row by row
chunk by chunk.
You should read Papaparse's FAQ before implementing that. To sum up, you will store required columns by extracting them from the step or chunk callbacks.
I am building an app with ionic 2 and I would like to translate some objects from the server response without storing it in a JSON file.
I´m using ngx-translate
For example, i am receiving an object like this:
{
"name": ["Hello", "Hola", "Bon Jour"],
"description": ["This text is in English", "This text is in Spanish", "This text is in French"]
}
I would like to set the value of the string in my app depending on the language selected. How can i achieve this?
Thanks so much.
You can create an object with 3 keys (en, es, fr) and then use it to set each language using 'setTranslation'
Example:
translate.setTranslation('en', translations['en']);
translate.setTranslation('es', translations['es']);
translate.setTranslation('fr', translations['fr']);
To generate this 'translations' you can iterate over the data you have and for each item, get the translation for each language using its index in the array.
let data = {
"name": ["Hello", "Hola", "Bon Jour"],
"description": ["This text is in English", "This text is in Spanish", "This text is in French"]
}
const translations = {en: [], es: [], fr: []};
Object.keys(data).forEach( key => {
translations['en'][key] = data[key][0];
translations['es'][key] = data[key][1];
translations['fr'][key] = data[key][2];
});
console.log(translations); //this will output you object ready to be used in 'setTranslation'
Here is a working fiddle with this code.
UPDATE
You can rewrite that hard-coded piece with setTranslation in a more dynamic way
Object.keys(translations).forEach( key => {
translate.setTranslation(key, translations[key]);
});
So you can have multiple (1..n) languages without having to repeat code.
sample json data file
{
"Includes": {
"Employees": {
"14": {
"name": "john",
"age": 12,
"activity": {
"Count": 3502,
"RatingValue": 5
}
},
"17": {
"name": "smith",
"age": 23,
"activity": {
"Count": 232,
"RatingValue": 5
}
}
}
}
}
js was written to retrieve the nested document and stored in array
var result = [];
db.details.find().forEach(function(doc) {
var Employees = doc.Includes.Employees;
if (Employees) {
for (var key in Employees) {
var Employee = Employees[key];
var item = [];
item.push(key);
item.push(Employee.name);
item.push(Employee.age);
item.push(Employee.activity.Count);
item.push(Employee.activity.RatingValue);
result.push(item.join(","));
}
}
});
print(result);
How can we store the output of array in csv with 2 rows because the data contains 2 rows by using mongoexport. In csv output must be
14,john,12,3502,5
17,smith,23,232,5
var csv=""; //this is the output file
for(var i=0;i<result.length;i++){//loop through output
csv+=result[i]+"\n"; //append the text and a newline
}
window.open('data:text/csv;' + (window.btoa?'base64,'+btoa(csv):csv)); //open in a new window, chrome will automatically download since it is a csv.
Change that final print(result); to the following:
print(result.join("\n"));
Then call your script and direct the output to a CSV file like so:
mongo --quiet "full-path-to-script.js" > "full-path-to-output.csv"
Note: The --quiet arg suppresses the standard Mongo header output (shell version and initial database).
I created a details collection, and added your JSON document to it, and then running the modified script resulted in the following CSV file content:
14,john,12,3502,5
17,smith,23,232,5
If you want a CSV header row as well, see my nearly identical answer to your nearly identical question here: https://stackoverflow.com/a/26310323/3212415
I am sending a JSON object from a servlet to JSP using AJAX. My JSON object contains a String value inside. and that string contains double quotes within that. My JSON does not parse it. I get the following error:
{"diagnosis":[{"NAME":"new_diagnosis_1 \[1020\]:2000000006001"},{"NAME":"new_diagnosis_2 \[1021\]:2000000006003"},{"NAME":"new_"dise"sed \[1023\]:2000000009001"},{"NAME":"new_d"ise"sef \[1024\]:2000000009003"}]}
note new_"dise"sed and new_d"ise"sef
I need a solution.
your json is not valid
try this
{
"diagnosis": [
{
"NAME": "new_diagnosis_1 [1020]:2000000006001"
},
{
"NAME": "new_diagnosis_2 [1021]:2000000006003"
},
{
"NAME": "new_\"dise\"sed [1023]:2000000009001"
},
{
"NAME": "new_d\"ise\"sef [1024]:2000000009003"
}
]
}
use \ to escape quotes
you can validate your json here
http://www.jsonlint.com/