conversikit
Advanced tools
Comparing version 1.8.14 to 1.8.15
116
index.js
const fs = require('fs'); | ||
const zlib = require('zlib'); | ||
@@ -242,118 +243,3 @@ module.exports = { | ||
} catch (error) { throw error; } | ||
}, | ||
toConvertExcelToCSV: async (excelFile) => { | ||
try { | ||
const XLSX = require('xlsx'); | ||
if (!excelFile.endsWith(".xlsx")) { | ||
throw new Error("invalid file name, its must be .xlsx"); | ||
} | ||
// Read the Excel file | ||
const workbook = await XLSX.readFile(excelFile); | ||
let allData = []; | ||
for (let i = 0; i < workbook.SheetNames.length; i++) { | ||
let sheetName = workbook.SheetNames[i]; | ||
let sheet = workbook.Sheets[sheetName]; | ||
// Convert the sheet to CSV format | ||
const csvData = XLSX.utils.sheet_to_csv(sheet); | ||
// console.log("data csv = ", csvData) | ||
await fs.writeFileSync(`${sheetName}.csv`, csvData); | ||
} | ||
} catch (error) { | ||
console.log("error = ", error.message) | ||
throw Error(error.message); | ||
} | ||
}, | ||
toConvertExcelToJSON: async (excelFile) => { | ||
try { | ||
async function toConvertCSVToJson (data) { | ||
try { | ||
// console.log("Convert CSV to Json"); | ||
const rows = data.split('\n'); | ||
const header = rows[0].split(','); | ||
//let create a json data for this csv file | ||
let jsonArray = []; | ||
for (let i = 1; i < rows.length; i++) { | ||
let csvData = rows[i].split(','); | ||
let obj = {}; | ||
let concatData = ""; | ||
let dataOccur = 0; | ||
let occure = false; | ||
for (let j = 0; j < csvData.length; j++) { | ||
if (occure) { | ||
if (csvData[j].endsWith('"')) { | ||
concatData += csvData[j]; | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = concatData || ""; | ||
concatData = ""; | ||
occure = false; | ||
} else { | ||
dataOccur++; | ||
concatData += csvData[j] + ","; | ||
} | ||
continue; | ||
} | ||
if (csvData[j].startsWith('"')) { | ||
concatData += csvData[j] + ","; | ||
dataOccur++; | ||
occure = true; | ||
continue; | ||
} | ||
if (header[j - dataOccur]) { | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = csvData[j] || ""; | ||
} | ||
if (j === csvData.length - 1) { | ||
obj[JSON.stringify(header[j - dataOccur].split('\r')[0])] = csvData[j].split('\r')[0] || ""; | ||
} | ||
} | ||
if (obj) jsonArray.push(obj) | ||
} | ||
return jsonArray; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
const XLSX = require('xlsx'); | ||
if (!excelFile.endsWith(".xlsx")) { | ||
throw new Error("invalid file name, its must be .xlsx"); | ||
} | ||
// Read the Excel file | ||
const workbook = await XLSX.readFile(excelFile); | ||
let allData = []; | ||
for (let i = 0; i < workbook.SheetNames.length; i++) { | ||
let sheetName = workbook.SheetNames[i]; | ||
let sheet = workbook.Sheets[sheetName]; | ||
// Convert the sheet to CSV format | ||
const csvData = XLSX.utils.sheet_to_csv(sheet); | ||
// console.log("data csv = ", csvData); | ||
allData.push({sheetName : `${sheetName}.csv`, data : await toConvertCSVToJson(csvData)}); | ||
} | ||
return allData; | ||
} catch (error) { | ||
console.log("error = ", error.message) | ||
throw Error(error.message); | ||
} | ||
} | ||
} |
{ | ||
"name": "conversikit", | ||
"version": "1.8.14", | ||
"version": "1.8.15", | ||
"description": "convert anything", | ||
@@ -58,3 +58,3 @@ "main": "index.js", | ||
"Workbook to CSV" | ||
], | ||
], | ||
"repository": { | ||
@@ -70,5 +70,3 @@ "type": "git", | ||
"homepage": "https://github.com/pradhan-arun/coversikit#readme", | ||
"dependencies": { | ||
"xlsx": "^0.18.5" | ||
} | ||
"dependencies": {} | ||
} |
@@ -70,10 +70,2 @@ <div align="center"> | ||
### `toConvertExcelToCSV('your_excel_file.xlsx')` | ||
- Here you convert a excel file in to csv format where its take all sheet, which is present in your excel file. | ||
#### `toConvertExcelToJSON('your_excel_file.xlsx')` | ||
- Converts Excel files into JSON format. | ||
- **path (String):** The file path to the Excel file you want to convert to JSON. | ||
## Examples | ||
@@ -206,24 +198,2 @@ | ||
``` | ||
# Excel to CSV Converter | ||
This Node.js script allows you to convert an Excel file (`.xlsx`) into one or more CSV files, one for each sheet in the Excel workbook. | ||
## Prerequisites | ||
Make sure you have the `xlsx` library installed. You can install it using npm: | ||
```bash | ||
npm install xlsx | ||
``` | ||
## Usage | ||
1. Replace 'your_excel_file.xlsx' with the path to your Excel file. | ||
2. Run the script. | ||
The script will read the Excel file and save each sheet as a separate CSV file. | ||
## Example | ||
To convert an Excel file to CSV, you can call the toConvertExcelToCSV function with the path to your Excel file: | ||
```javascript | ||
await toConvertExcelToCSV('your_excel_file.xlsx'); | ||
``` | ||
# Author | ||
@@ -230,0 +200,0 @@ |
0
14808
200
206
- Removedxlsx@^0.18.5
- Removedadler-32@1.3.1(transitive)
- Removedcfb@1.2.2(transitive)
- Removedcodepage@1.15.0(transitive)
- Removedcrc-32@1.2.2(transitive)
- Removedfrac@1.1.2(transitive)
- Removedssf@0.11.2(transitive)
- Removedwmf@1.0.2(transitive)
- Removedword@0.3.0(transitive)
- Removedxlsx@0.18.5(transitive)