1

我在Node.js中使用Cloud Functions for Firebase,其中我使用excel4node lib从对象中导出excel文档,现在问题在于wb.write('Excel.xlsx');之后我必须在用户可以下载它的前端发送它之前,实际上将该excel文件保存在某处。使用Spark(免费)Firebase计划为Firebase导出excel功能

我试图这样做:

  1. 我知道有云存储的火力地堡,但只有谷歌知道,并根据这个帖子的一些奇怪的原因:TypeError: firebase.storage is not a function火力地堡不允许存储不再与节点使用.js文件。

  2. 我还阅读了关于Google Cloud存储的信息,但那只是通过付费服务。

  3. 这里有一个教程:https://mashe.hawksey.info/2017/06/cloud-functions-for-firebase-in-google-apps-script/但你需要有一个付费方案。

我的问题是:有没有免费的方式,使这个出口与火力和的NodeJS,这样我就可以保存在某个地方导出Excel和输出它的前端,使其从下载用户?

回答

1

您从第3号教程发布的链接仅需要付费计划,因为它会发出外部HTTP请求。如果你只是使用内部存储,你可以这样做:

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 
相关问题