2
votes

I use Cloud Functions for Firebase in Node.js where I export a excel doc from a object using excel4node lib, now the problem is that after wb.write('Excel.xlsx'); I have to actually save that excel file somewhere before I send it on the front end where user can download it.

What I tried to do:

  1. I know there is Cloud Storage for Firebase, but for some weird reason that only google knows and according to this post: TypeError: firebase.storage is not a function Firebase Storage is not longer used with Node.js.

  2. I also read about Google Cloud storage, but that is only via paid service.

  3. There is a tutorial here: https://mashe.hawksey.info/2017/06/cloud-functions-for-firebase-in-google-apps-script/ but again you need to have a paid plan.

My question is: Is there any free way to make this export with firebase and Nodejs so that I will be able to save somewhere the exported excel and output it on the front end so that it will be downloaded from the user?

1

1 Answers

4
votes

The link you posted from the tutorial in number 3 only requires a paid plan because it makes an external HTTP request. If you're just using the internal storage, you can do this:

const path = require('path');
const os = require('os');
const fs = require('fs');

var bucket = admin.storage().bucket();

//cloud functions can only write to the temporary directory
const tempFilePath = path.join(os.tmpdir(), "Excel.xlsx");

//creates and writes the file to the local temporary directory 
//and then retrieves the newly created file for uploading
wb.write(tempFilePath, function (err, stats) {
          if (err) {
              console.error(err);
          }  else {
              console.log("File written.");
              fs.readFile(tempFilePath, function (err, data) {
                if (err) {
                  throw err;
                }
                var uploadTask = bucket.upload(tempFilePath, 
               { destination: "excelFileFolder/Excel.xlsx"});
// Uploads the excel file to a folder called excelFileFolder in your firebase storage