代码之家  ›  专栏  ›  技术社区  ›  Utkarsh Bhatt

无法使用FireBase云功能创建XLSX文件

  •  2
  • Utkarsh Bhatt  · 技术社区  · 7 年前

    我跟着 basic usage tutorial for excel4node 包裹。

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

    但是,问题是每次调用函数时,都是一个零字节 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 .

    2 回复  |  直到 7 年前
        1
  •  2
  •   Utkarsh Bhatt    7 年前

    /tmp

    C:/tmp

    tmpdir() os

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

    os.tmpdir()

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

        2
  •  1
  •   Doug Stevenson    7 年前

    workbook.write('Excel.xlsx');

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

    wb.write('ExcelFile.xlsx', res);
    
    推荐文章