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 
相關問題