Parsing CSV Contents using Papa Parse

Solution

We can parse CSV contents using papa parse library script and update the contents of CSV. Library script is added below (one drive link)

papaparse.min.js

You can add this script to the file cabinet and add the script to the define function.

Sample code shows parsing a csv and updating contents. Converting a column to number format

let fileToGcs = file.load({
                        id: fileId
                    });
                    let csvParse = papa.parse(fileToGcs.getContents());
                    log.debug('csv parsed', csvParse)

                    for (let i = 7; i < csvParse.data.length; i++) {
                        
                        let debitData = csvParse.data[i][5]
                        if (debitData) {
                            let newData = debitData.replace(",", "")
                            csvParse.data[i][5] = Number(newData)
                        }
                        let creditData = csvParse.data[i][6]
                        if (creditData) {
                            let newData = creditData.replace(",", "")
                            csvParse.data[i][6] = Number(newData)
                        }
                    }
                    let sendData=papa.unparse(csvParse)
                    log.debug("CSVDATA",sendData)

Leave a comment

Your email address will not be published. Required fields are marked *