This topic contains 1 reply, has 0 voices, and was last updated by FHC 7 years, 1 month ago.
-
AuthorPosts
-
October 8, 2017 at 8:39 pm #18237
FHCI’m trying to produce a Google Shopping feed of 30,000+ items, outputting this product feed as a CSV.
The problem is that the product descriptions of these items contain variables amounts of commas, double quotes, single quotes and HTML. At first, it was just the commas causing me problems, so after a bit of research, I wrapped the strings I was outputting in double quotes:
Code:
//This function isn’t terribly important, but is referenced belowfunction sanitizeString (desc) {
var itemDesc;
if (desc) {
itemDesc = desc.replace(/(rn|n|r|s+|t| )/gm,’ ‘);
itemDesc = itemDesc.replace(/,/g, ‘,’);
itemDesc = itemDesc.replace(/”/g, ‘”‘);
itemDesc = itemDesc.replace(/’/g, ”’);
itemDesc = itemDesc.replace(/ +(?= )/g,”);
if (itemDesc.length > 5000) {
itemDesc = itemDesc.substring(0,5000)
itemDesc = itemDesc.substring(0, itemDesc.lastIndexOf(‘.’));
} else {
itemDesc = itemDesc;
}
} else {
itemDesc = ”;
}
return itemDesc;
}//below is a snippet from a for loop. val is a JSON object. col is an index string. row is then appended to the file using appendLine();
row += ‘”‘ + manualGSF.sanitizeString(val[col]) + ‘”‘;
However, it seems that these double quotes are interacting strangely with double quotes within the string, causing some weird formatting, even though my sanitizeString() function should be escaping them.So, naturally, I escaped the external quotes like this:
Code:
row += ‘”‘ + manualGSF.sanitizeString(val[col]) + ‘”‘;
Doing that makes things go completely haywire and a lot of items don’t get pushed to new lines.The other natural solution would be to go edit the product descriptions, but I’m not terribly anxious to do that for 30,000+ items…
Does anybody know what might be going on here? I feel like there’s something really simple I’m overlooking…
This is a cached copy. Click here to see the original post. -
October 8, 2017 at 10:35 pm #18238
FHCOK, I figured it out. It turns out that, according to the CSV specs, to include double quotes within a string that is already quoted, you need to use two double quotes (“”). I changed:
Code:
itemDesc = itemDesc.replace(/”/g, ‘”‘);
toCode:
itemDesc = itemDesc.replace(/”/g, ‘””‘);
I also removedCode:
itemDesc = itemDesc.replace(/,/g, ‘,’);
itemDesc = itemDesc.replace(/’/g, ”’);
Since the column in the CSV is being quoted already. These are unnecessary. -
AuthorPosts
You must be logged in to reply to this topic.