Hi there
I have the following script which I am trying to use to export from the saved search results in a Suitelet to Excel.
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(["N/search", "N/file", "N/https"], function (search, file, https) {
function onRequest(context) {
// Load the library file from the filing cabinet
var libraryFile = file.load({
id: 21973,
});
// Read the contents of the library file
var libraryContents = libraryFile.getContents();
// Evaluate the contents of the library file to make the functions it contains available in your script
eval(libraryContents);
// Replace this with the ID of the saved search you want to run
var searchId = "customsearch_my_saved_search";
// Run the search and get the results
var searchResult = search.load({
id: searchId,
});
var searchRows = searchResult.run().getRange({
start: 0,
end: 1000,
});
// Create an array to hold the data for the Excel file
var data = [];
// Add the column names as the first row
var columnNames = [];
searchResult.columns.forEach(function (column) {
columnNames.push(column.label);
});
data.push(columnNames);
// Add the search results to the data array
searchRows.forEach(function (row) {
var rowData = [];
searchResult.columns.forEach(function (column) {
rowData.push(
row.getValue({
name: column.name,
})
);
});
data.push(rowData);
});
// Wait for the SheetJS library to be fully loaded
new Promise(function (resolve) {
if (XLSX.write) {
resolve();
} else {
setTimeout(function () {
resolve();
}, 100);
}
}).then(function () {
// Create the Excel file
var ws = XLSX.utils.aoa_to_sheet(data);
var wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "Sheet1");
var binaryData = XLSX.write(wb, {
type: "binary",
bookType: "xlsx",
});
var fileName = "search_results.xlsx";
var folderId = -4; // -4 represents the "Home" folder in the file cabinet
var file = file.create({
name: fileName,
fileType: file.Type.EXCEL,
contents: binaryData,
folder: folderId,
});
// Send the file as a response to the user
context.response.writeFile({
file: file,
isInline: true,
});
});
}
return {
onRequest: onRequest,
};
});
However, I am getting the following error message:
Notice (SuiteScript)
org.mozilla.javascript.EcmaError: TypeError: Cannot set property "JSZipSync" of undefined to "org.mozilla.javascript.InterpretedFunction@1e000f45" (/SuiteScripts/sdf_ignore/Suitelet to Excel.js#16(eval)#2)
What would be causing that?
The JSZipSync that is being referred to here is related to a function in the xlsx.full.min.js library