From a script, how would you attach the results of...
# suitescript
s
From a script, how would you attach the results of a saved search to an email in an excel format?
b
I'd save the results as a file then attach the file. not sure if that's the most efficient way. This way would/could lead to lots of files in the file cabinet, so if you don't need them long term and don't want to pay for the storage of them, you'd want to delete them later.
s
How would it save the results in an excel format though?
b
you can use the task module which would save as csv, but is async. but you could add a dependency that once run will send the email. otherwise i'd look at using the n/file module to create my own file and saving as xls by setting the name of the file as filename.xls. you'd have to create the file by iterating over your results and building the file.
s
So it'd need to be in a csv format, but just saved as an xls in file.create?
b
if you use the task module, you don't get a choice, it's a csv. if you use the file module, i think you can specify the file type in file.create as excel
they are two entirely different approaches to your question
s
Yeah I need an excel unfortunately. So I created a csv (csv works), but saving it as excel did not. Any suggestions?
Copy code
var fileObj = file.create({
    //To make each file unique and avoid overwriting, append date on the title
    name: 'Saved Search Result – ' + date.toLocaleDateString() + '.xls',
    fileType: file.Type.EXCEL,
    contents: csvFile,
    description: 'This is a CSV file.',
    encoding: file.Encoding.UTF8,
    folder: fileConfig.SCRIPT_SAVED_FILES()
});

Error:
An unexpected SuiteScript error has occurred
b
you have to build the file using xml, then encode it.
🙌 1
i linked this one because it was a quick google and used 2.0 methods, but there are other similar ones that use 1.0 and the building of the xml string looks to be the same.
you can't just pop a csv into the contents property and have it work.
b
depends on what you really want
making a csv file is relatively easy
if you want an actual excel file, then you need to start using things like SheetJS
s
Nice! I'm gonna look at that battk. Have you had any familiarity of using it or converting it to work with netsuite?
b
use 2.1 and you are fine
dont try to make massive files
s
I am. I'm struggling to initialize it it seems. so i'm loading it via : 'SuiteScripts/AccentDecor/Libs/ExternalLibs/xlsx/xlsx.js', and trying to use it like xlsx.utils.sheet_to_json(mySearch1, { header: 1 })
and it keeps saying cannot use utils of undefined
Also, I have the distribution file xlsx.js uploaded into the file cabinet at the above location
b
looks like they started using a named export for their amd module
you will have to use a require config
s
are you referring to adding this?
Copy code
require(['xlsx'], function (render) {
});
b
s
Ok I am already using this.
Copy code
{
  "paths": {
    "xlsx": "/SuiteScripts/AccentDecor/Libs/xlsx/xlsx.js",
  }
}
and then using it like
Copy code
define([
    'xlsx',
],
  function (
    xlsx
) {}
b
that code doesnt do anything, so it shouldnt be saying xlxs is undefined
s
nvm. I was using the require config, changed it to the path to check it. I was not expecting it to work with the require config and not with directly importing it
b
netsuite doesnt support named exports well
👍🏼 1