【问题标题】:Export excel with Cloud Functions for Firebase using spark (free) firebase plan使用 spark(免费)firebase 计划使用 Cloud Functions for Firebase 导出 excel
【发布时间】:2018-03-20 14:53:05
【问题描述】:

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

我想做什么:

  1. 我知道 Firebase 有 Cloud Storage,但由于某些奇怪的原因,只有 google 知道,根据这篇文章:TypeError: firebase.storage is not a function Firebase Storage 不再与 Node.js 一起使用。

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

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

我的问题是:是否有任何免费的方法可以使用 firebase 和 Nodejs 进行此导出,以便我能够将导出的 excel 保存在某处并将其输出到前端,以便从用户那里下载?

【问题讨论】:

    标签: node.js firebase google-cloud-platform google-cloud-functions


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2018-12-03
      • 2018-12-13
      • 1970-01-01
      • 2021-08-14
      • 2018-01-14
      • 2017-09-27
      • 2017-08-02
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多