QQ, creating an XLSX file. I have a CSV but the cu...
# suitescript
n
QQ, creating an XLSX file. I have a CSV but the customer wants it saving as an XLXS file. Am I correct thinking I should use an XML template and transpose the CSV data in to it and use that as the content of the XLSX file? Or is there an easier way?e
m
You can use sheet.js with SS2.1
n
ooh! thanks @michoel I'll have a look
Hmm, I have taken my input CSV and created an array. I have run that through sheets:
Copy code
let wb = XLSX.utils.book_new();
wb.Props = {
    Title: "SheetJS Tutorial",
    Subject: "Test",
    Author: "Neil Elliott",
    CreatedDate: new Date()
};
wb.SheetNames.push("Test Sheet");
let ws = XLSX.utils.aoa_to_sheet(csvArray);
wb.Sheets["Test Sheet"] = ws;
I'm now struggling to see how I might create this as a file in the filecabinet. Anyone advise please or have an example?
d
I created this extension earlier this year for the exact same reason (link is to reddit so you have a bit more info) ... https://www.reddit.com/r/Netsuite/comments/ksfgyt/download_files_as_xlsx_chrome_extension/
👀 1
I think there is an issue with it running on a mac though
Just dont have a mac to troubleshoot 😞
n
@dynamicl Thanks for the response. I'll be doing this server side so an extension may not help but if your code is available maybe it'll shed some light on where I'm going wrong. Incidentally I did go back to using an xml template and switching in the data and managed to get it working. I'm not sure what was wrong the first time around... netsuite
m
@NElliott
Copy code
const contents = XLSX.write(workbook, { bookType: "xlsx", type: "base64" });

const fileObj = file.create({
  name: 'report.xlsx',
  fileType: file.Type.EXCEL,
  contents,
});

fileObj.folder = scriptParameters.reportFolderId;
return fileObj.save();
n
Thank you @michoel I may revisit this since the format written out as it stand is "XML Spreadsheet 2003" format in actual fact which Excel warns may be corrupt (it's not) and I am concerned this may cause issues for the end user.
Interesting if I state the file being created is xlsx and save to the filecabinet as EXCEL the file looks like the image when I try and open it and Excel complains about the file extension not matching the content. If I state it's "xls" at this point:
Copy code
const contents = XLSX.write(workbook, { bookType: "xls", type: "base64" });
It works fine although I get a warning about protected mode blah blah blah which I'm hoping isn't an issue. @michoel Thank you very much for your help.