JavaScript csv export

Solved
Traker12
Shopify Partner
6 0 1

Hi, I'm developing an app that help the merchants to integrate data with other program. I want that the app make a query to get, for example, all the customers of the shop. Then I want to write the result of this query in a csv text file.
Then I would like to upload this file in a folder in a cloud system, for example Google Drive.
I'm block after the query of the customers.
Can you help me to write the result in a csv?

I'm developing with Node and React

0 Likes
sunitha_nair
Excursionist
15 2 5

Hi,

I hope this helps:

 

function convertToCSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            if (line != '') line += ','

            line += array[i][index];
        }

        str += line + '\r\n';
    }

    return str;
}

function exportCSVFile(customers) {
  
    // Convert Object to JSON
    var jsonObject = JSON.stringify(customers);

    var csv = this.convertToCSV(jsonObject);
// You can write this to drive return new Blob([csv], { type: 'text/csv;charset=utf-8;' }); }
Traker12
Shopify Partner
6 0 1

Thank you.

 

But can you explain to me how to upload to a cloud system (dropbox, gdrive for example). I think that I have to write somewhere where I want to save that. 

 

 

sunitha_nair
Excursionist
15 2 5

This is an accepted solution.

Hi, 

You can googleapis npm package to write to google drive. This would cover your requirement. However you need to authenticate to google first.

0 Likes
Traker12
Shopify Partner
6 0 1
Thank you so much
0 Likes