【发布时间】: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