0

I'm trying to create an Excel file in a firebase function.

Running locally, this writes an xls file.

const xls = json2xls(data);
fs.writeFileSync(`${filename}.xlsx`, xls, 'binary');

But run in a firebase function, this downloads a broken file.

const xls = json2xls(data);

res
  .status(200)
  .set('Content-Type', 'application/vnd.openxmlformats')
  .set("Content-Disposition", `attachment; filename=${filename}.xlsx`)
  .send(xls);

Both files are named correctly. They're 'Office Open XML spreadsheet' files. The one from firebase is 30% larger than the once created locally, but otherwise I can't see any difference.

What am I doing wrong here? How do I go about debugging this?

nicholas
  • 14,184
  • 22
  • 82
  • 138
  • Please edit your question to say exactly what "a broken file" is. Please be specific about the input and output you're seeing. – Doug Stevenson Mar 07 '18 at 04:06
  • @DougStevenson I've edited the question to include everything I can see about the 2 files. It's not much, which is the problem. What other info. might help debug this problem? – nicholas Mar 07 '18 at 17:03
  • How do you know the file is "broken"? Walk us through the steps of what you're doing. Also try writing a regular node.js program outside of Cloud Functions to help possibly eliminate CF from the picture. Maybe it's just your conversion process. – Doug Stevenson Mar 07 '18 at 17:15
  • My first code snippet is from a node.js program running locally. It converts `data` to `xls` and writes a file. That file will open normally and has data. The second example is running in the cloud function. But the file downloaded won't open, can't be imported, etc., it's just broken; not a valid xls file. `data` is the same in both cases. – nicholas Mar 07 '18 at 17:24
  • Can you examine the contents of the file? What is actually in it? – Doug Stevenson Mar 07 '18 at 17:25
  • They're both encoded the same way, but not readable. Just `504b 0304 0a00 0000 0800 137b 7942 c2bd 675d 2e39 0100 0035 0400 0013 0000 005b 436f 6e74 656e 745f 5479 7065 735d 2e78`... It's different in each file, and there's more of it in the file downloaded from the cloud function. (With my test data, the file from nodejs is 9kb, while the one from the cloud is 13kb.) – nicholas Mar 07 '18 at 17:27
  • You're probably going to have to do some debugging here. Try saving the file to CF locally on disk and examine/debug its file properties there. Try also uploading it to Cloud Storage, then examining it. Also, what exactly is the `xls` object? Should it be converted to something else suitable for the express API used to send a response? – Doug Stevenson Mar 07 '18 at 17:43

0 Answers0