【问题标题】:Can't create xlsx files with Firebase Cloud Functions无法使用 Firebase Cloud Functions 创建 xlsx 文件
【发布时间】:2018-12-14 06:05:52
【问题描述】:

我关注了basic usage tutorial for excel4node 包。

为了运行代码,我有一个 https 函数,它将在我的本地系统上与 index.js 相同的目录中创建一个 Excel.xlsx 文件。

然而,问题是每次我调用该函数时,都会创建一个零字节的Excel.xls 文件。

函数体是这样的:

const createXlxsFile = (req, res) => {
  const xl = require('excel4node');

  // Create a new instance of a Workbook class
  const workbook = new xl.Workbook();

  // Add Worksheets to the workbook
  const worksheet = workbook.addWorksheet('Sheet 1');
  const worksheet2 = workbook.addWorksheet('Sheet 2');

  // Create a reusable style
  const style = workbook.createStyle({
    font: {
      color: '#FF0800',
      size: 12
    },
    numberFormat: '$#,##0.00; ($#,##0.00); -'
  });

  // Set value of cell A1 to 100 as a number type styled with paramaters of style
  worksheet.cell(1, 1).number(100).style(style);

  // Set value of cell B1 to 300 as a number type styled with paramaters of style
  worksheet.cell(1, 2).number(200).style(style);

  // Set value of cell C1 to a formula styled with paramaters of style
  worksheet.cell(1, 3).formula('A1 + B1').style(style);

  // Set value of cell A2 to 'string' styled with paramaters of style
  worksheet.cell(2, 1).string('string').style(style);

  // Set value of cell A3 to true as a boolean type styled with paramaters of style but with an adjustment to the font size.
  worksheet.cell(3, 1).bool(true).style(style).style({ font: { size: 14 } });

  workbook.write('Excel.xlsx');

  res.end('DOC CREATED');
};

此代码适用于标准 Node.js,但不适用于 Firebase 云函数。用函数写文件有限制吗?

即使使用Xlsx-populate package,我也遇到了同样的问题。

【问题讨论】:

  • workbook.write() 是异步的吗?它会返回一个承诺吗?
  • @DougStevenson 不。在其上使用 .then() 会导致错误。 workbook.write() 不返回任何内容。

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


【解决方案1】:

好的。解决了问题。

问题是云功能不允许您写入操作系统中的任何目录。

您拥有写入权限的唯一位置是云函数中的/tmp

但是,在您的本地 PC 上,这也会崩溃(在 Windows 10 中测试)。可能是因为我还没有创建C:/tmp 文件夹。

要解决这个问题,您可以使用 Node.js 中 os 模块的 tmpdir() 方法

const os = require('os');
const path = require('path');
const pathToSave = path.join(os.tmpdir(), 'Excel.xlsx');

在部署代码时,您需要将 os.tmpdir() 替换为 `/tmp'。

const pathToSave = path.join('/tmp', 'Excel.xlsx');

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    workbook.write('Excel.xlsx'); 是异步的。文档说它需要一个在完成后调用的回调。使用它来终止函数。现在,您将在写入完成之前提前终止。

    wb.write();

    write() 方法可以接受单个文件名,一个 带有回调函数或 HTTP 响应对象的文件名。

    wb.write('ExcelFile.xlsx', function (err, stats) {
        if (err) {
            res.send(500);
        } else {
            res.end('DOC CREATED');
        }
    });
    

    看起来这也可以:

    wb.write('ExcelFile.xlsx', res);
    

    【讨论】:

    • 好的。谢谢。添加了这个,现在它在控制台中没有任何日志就崩溃了。 {"error":{"code":500,"status":"INTERNAL","message":"function crashed","errors":["read ECONNRESET"]}}。我对xlsx-populate 包也有同样的问题,它会在没有任何日志记录的情况下崩溃并创建一个零字节的 xlsx 文件。我的整个代码就是这个函数,没有别的。我也在if(err) 子句中添加了console.log(err)
    • 改为将您的文件写入/tmp/ExeclFile.xlsx/tmp 是 Cloud Functions 中唯一可写的区域。
    • 为了在本地测试代码?现在,它正在与index.js 相同的目录中创建此文件。问题是文件总是零字节。
    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 2020-09-30
    • 2019-09-27
    • 2018-01-02
    • 2019-07-09
    • 2018-11-29
    • 2017-09-01
    • 2018-09-10
    相关资源
    最近更新 更多