0

I have shopping cart in my android App. I am using Firebase as database. I want to mail cart items as CSV / Excel file as attachment.

2 Answers2

0

First you have to fetch all data from firebase.

Read Data From Firebase database

Then you have to generate csv file from the data.

How to create a .csv on android

After that you can send csv file from its path as an attachment to mail

How to send an email with a file attachment in Android

Ankit
  • 1,068
  • 6
  • 10
0

first install excel4node package in your firebase project, then import this in your index.js

const xl = require('excel4node');

also import these for file handling

const os = require('os');
const path = require('path');
const fs = require('fs');
const tempFilePath = path.join(os.tmpdir(), 'Excel.xlsx');
const storage = admin.storage();
const bucket = storage.bucket();

This is how you return the function should look

exports.writeFireToExcel = functions.https.onCall(async(data, context) => {

    // Create a new instance of a Workbook class
    const workbook = new xl.Workbook();
    // Add Worksheets to the workbook
    const worksheet = workbook.addWorksheet('Visa Work List');

    const ref = firebaseDb.ref('path');

    //firebase functions that return stuff must be done in a transactional way

    //start by getting snap
    return await ref.once('value').then(snapshot =>{

    var style = workbook.createStyle({
        font: {
          bold : true,
        },
      });

    //write workbook
        worksheet.cell(1, 1).string('input').style(style);
        //....write the rest of your excel
        return
        //
    }).then(function (){
        console.log('workbook filled');
        //second part of transation - write the excel file to the temp storage in firebase
        //workbook.write doesnt return a promise so ive turned it into a promise function
        return new Promise((resolve,reject) =>{
            workbook.write(tempFilePath, function (err, stats) {
                if (err) {
                    console.error(err);
                    reject(err)
                }else{
                    resolve()
                }
            });
        })
    }).then(function(){
        console.log("File written to: " + tempFilePath);
        //read the file and check it exists
        return new Promise((resolve,reject) =>{
            fs.readFile(tempFilePath, function (err, data) {
                if (err) {
                    reject(err)
                }else{
                    resolve()
                }
            })

        })

    }).then(function(){
        console.log("writing to bucket");
        //write the file to path in firebase storage 
        var fileName = 'VisaSummaryList.xlsx';
        var folderPath = uid + "/excelFile/";
        var filePathString = folderPath + fileName;

        return bucket.upload(tempFilePath, 
            { destination: filePathString}).then(function(){
                return filePathString;
            })

    }).catch(err => {
        throw err;
    });
});

the function returns a filepath in the firebase storage. In your android app just:

 //firebase storage reference, result being whats returned from the firebase function
 val fbstore = FirebaseStorage.getInstance().reference.child(result)
 fbstore.getFile(myFile)
Haider Malik
  • 1,581
  • 1
  • 20
  • 23