From d85183c75f266c6e04df132dfb29cbd00ff0a79b Mon Sep 17 00:00:00 2001 From: SheetJS Date: Fri, 3 May 2013 10:23:19 -0400 Subject: [PATCH] version bump 0.2.7-d: CSV generation issue double-quotes are emitted as \"\" --- bits/90_utils.js | 2 +- index.html | 33 +++++++++++++++------------------ package.json | 2 +- xlsx.js | 2 +- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/bits/90_utils.js b/bits/90_utils.js index c25f93f..1823195 100644 --- a/bits/90_utils.js +++ b/bits/90_utils.js @@ -81,7 +81,7 @@ function sheet_to_csv(sheet) { var row = []; for(var C = r.s.c; C <= r.e.c; ++C) { var val = sheet[utils.encode_cell({c:C,r:R})]; - row.push(val ? stringify(val).replace(/\\r\\n/g,"\n").replace(/\\t/g,"\t").replace(/\\\\/g,"\\") : ""); + row.push(val ? stringify(val).replace(/\\r\\n/g,"\n").replace(/\\t/g,"\t").replace(/\\\\/g,"\\").replace("\\\"","\"\"") : ""); } out += row.join(",") + "\n"; } diff --git a/index.html b/index.html index ff3a1a0..765dc48 100644 --- a/index.html +++ b/index.html @@ -17,7 +17,7 @@ CSV
JSON
-
Drop an XLSX file here to see data in CSV or JSON.
+
Drop an XLSX file here to see sheet data.

@@ -34,28 +34,25 @@ function get_radio_value( radioName ) {
 	}
 }
 
-//Row object array form:
-//Each row is an object with column headers as keys
 function to_json(workbook) {
 	var result = {};
 	workbook.SheetNames.forEach(function(sheetName) {
-		var rObjArr = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
-		if(rObjArr.length > 0){
-			result[sheetName] = rObjArr;
+		var roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
+		if(roa.length > 0){
+			result[sheetName] = roa;
 		}
 	});
 	return result;
 }
 
-
 function to_csv(workbook) {
 	var result = [];
 	workbook.SheetNames.forEach(function(sheetName) {
-		var rObjArr = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);
-		if(rObjArr.length > 0){
+		var csv = XLSX.utils.sheet_to_csv(workbook.Sheets[sheetName]);
+		if(csv.length > 0){
 			result.push("SHEET: " + sheetName);
 			result.push("");
-			result.push(rObjArr);
+			result.push(csv);
 		}
 	});
 	return result.join("\n");
@@ -63,16 +60,16 @@ function to_csv(workbook) {
 
 var tarea = document.getElementById('b64data');
 function b64it() {
-	var xlsx = XLSX.read(tarea.value, {type: 'base64'});
-	process_xlsx(xlsx);
+	var wb = XLSX.read(tarea.value, {type: 'base64'});
+	process_wb(wb);
 }
 
-function process_xlsx(xlsx) {
+function process_wb(wb) {
 	var output = "";
 	if(get_radio_value("format") === "json"){
-		output = JSON.stringify(to_json(xlsx), 2, 2);
+		output = JSON.stringify(to_json(wb), 2, 2);
 	} else {
-		output = to_csv(xlsx);
+		output = to_csv(wb);
 	}
 	if(out.innerText === undefined) out.textContent = output;
 	else out.innerText = output;
@@ -89,10 +86,10 @@ function handleDrop(e) {
 		var name = f.name;
 		reader.onload = function(e) {
 			var data = e.target.result;
-			//var xlsx = XLSX.read(data, {type: 'binary'});
+			//var wb = XLSX.read(data, {type: 'binary'});
 			var arr = String.fromCharCode.apply(null, new Uint8Array(data));
-			var xlsx = XLSX.read(btoa(arr), {type: 'base64'});
-			process_xlsx(xlsx);
+			var wb = XLSX.read(btoa(arr), {type: 'base64'});
+			process_wb(wb);
 		};
 		//reader.readAsBinaryString(f);
 		reader.readAsArrayBuffer(f);
diff --git a/package.json b/package.json
index 256c050..bf9b4c5 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
 	"name": "xlsx",
-	"version": "0.2.7-c",
+	"version": "0.2.7-d",
 	"author": "Niggler",
 	"description": "(one day) a full-featured XLSX parser and writer.  For now, primitive parser",
 	"keywords": [
diff --git a/xlsx.js b/xlsx.js
index 8d36ae0..a7b2901 100644
--- a/xlsx.js
+++ b/xlsx.js
@@ -1038,7 +1038,7 @@ function sheet_to_csv(sheet) {
 			var row = [];
 			for(var C = r.s.c; C <= r.e.c; ++C) {
 				var val = sheet[utils.encode_cell({c:C,r:R})];
-				row.push(val ? stringify(val).replace(/\\r\\n/g,"\n").replace(/\\t/g,"\t").replace(/\\\\/g,"\\") : "");
+				row.push(val ? stringify(val).replace(/\\r\\n/g,"\n").replace(/\\t/g,"\t").replace(/\\\\/g,"\\").replace(/\\\"/g,"\"\"") : "");
 			}
 			out += row.join(",") + "\n";
 		}