within GET :
// Serialize the array to a JSON string
let itemsArrayString = JSON.stringify(itemsArray);
// Create a temporary file in the File Cabinet
let fileObj = file.create({
name: ‘temp_jsondata.json’,
fileType: file.Type.JSON,
contents: itemsArrayString,
folder: –15 // SuiteScripts folder
});
let fileId = fileObj.save();
// Add a hidden field to store the file ID
let fileIdField = form.addField({
id: ‘custpage_fileid’,
type: serverWidget.FieldType.TEXT,
label: ‘File ID’
}).updateLayoutType({ layoutType: serverWidget.FieldLayoutType.ENDROW });
fileIdField.defaultValue = fileId;
fileIdField.updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});
form.addSubmitButton({
label: ‘Export as Excel’
});
within POST :
// let objdata = context.request.parameters.custpage_jsondata;
// downloadExcel(context, objdata);
let fileId = context.request.parameters.custpage_fileid;
// Load the file and read its contents
let fileObj = file.load({ id: fileId });
let objdata = fileObj.getContents();
// Delete the temporary file if no longer needed
file.delete({ id: fileId });
downloadExcel(context, objdata);
function downloadExcel(context, objdata) {
try {
let obj = JSON.parse(objdata);
let renderObj = render.create();
renderObj.templateContent = file.load({
id: ‘./jj_template_inventory_usage_report_gtilc_527.xml’
}).getContents();
renderObj.addCustomDataSource({
format: render.DataSource.OBJECT,
alias: “record”,
data: { objdata: obj }
});
let renderAsString = renderObj.renderAsString();
let strXmlEncoded = encode.convert({
string: renderAsString,
inputEncoding: encode.Encoding.UTF_8,
outputEncoding: encode.Encoding.BASE_64
});
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, ‘0’); // Months are 0-based
const day = String(date.getDate()).padStart(2, ‘0’);
const formattedDate = `${month}–${day}–${year}`;
const fileName = `Inventory Usage Report ${formattedDate}.xls`;
let objXlsFile = file.create({
name: fileName,
fileType: file.Type.EXCEL,
contents: strXmlEncoded
});
context.response.writeFile({
file: objXlsFile,
isInline: false
});
return;
} catch (e) {
log.error(“error@download”, e)
}
}