RE: Export an Excel on Client Script in SS2.x

I’ve created a button on the User Server Event labelled ‘Export’ to enable a function upon clicking so that I may export the items in a Transfer Order as an Excel file.

Export an Excel on Client Script in SS2.x

I would like a download popup to prompt me to save the file.

I successfully created the button, but now on the client script side I am merely testing just the creation of the XLS file and trying to get the popup initiated for me to download but I keep on running into errors: latest ‘Uncaught ReferenceError: xmlString is not defined’

Server User Event

define(['N/record','N/ui/serverWidget'],
function(record,serverWidget) {
/**

* Function definition to be triggered before record is loaded.

*

* @param {Object} scriptContext

* @param {Record} scriptContext.newRecord - New record

* @param {string} scriptContext.type - Trigger type

* @param {Form} scriptContext.form - Current form

* @Since 2015.2

*/

function beforeLoad(scriptContext) {
var forms = scriptContext.form;
if (scriptContext.type == scriptContext.UserEventType.VIEW){
forms.clientScriptFileId=30592;

forms.addButton({

id : 'custpage_buttonid', //always prefix with 'custpage_'

label : 'Export', //label of the button

functionName: "onButtonClick()"

});

//forms.clientScriptModulePath = "SuiteScripts/exportToXLS.js";

}

}

Client Side Script

define(['N/ui/dialog'],
function(dialog) {
function pageInit(scriptContext) {}
function onButtonClick(scriptContext) {
dialog.alert({

title: "Alert",

message: "You click this export button!"
});
// XML content of the file


var xmlStr = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>';

xmlStr += '<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" ';

xmlStr += 'xmlns:o="urn:schemas-microsoft-com:office:office" ';

xmlStr += 'xmlns:x="urn:schemas-microsoft-com:office:excel" ';

xmlStr += 'xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" ';

xmlStr += 'xmlns:html="http://www.w3.org/TR/REC-html40">';
xmlStr += '<Worksheet ss:Name="Sheet1">';

xmlStr += '<Table>' +

'<Row>' +

'<Cell><Data ss:Type="String"> First Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Second Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Third Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Fourth Header </Data></Cell>' +

'<Cell><Data ss:Type="String"> Fifth Header </Data></Cell>' +

'</Row>';
xmlStr += '</Table></Worksheet></Workbook>';
//encode contents

var base64EncodedString = encode.convert({

string: xmlString,

inputEncoding: encode.Encoding.UTF_8,

outputEncoding: encode.Encoding.BASE_64
});
//create file

var xlsFile = file.create({

name: 'TEST.xls',

fileType: 'EXCEL',

contents: base64EncodedString
});
log.debug({

details: "File ID: " + fileid

});
scriptContext.response.writeFile({

file : xlsFile

});
}

 

mcfly Rookie Asked on September 24, 2019 in SuiteScript.

Is it work on the client script?

on September 16, 2020.
Add Comment
2 Answers

You have your string variable defined as xmlStr initially but later you reference it as xmlString.

Regardless, I would recommend probably using an Excel JavaScript library instead instead of manually forming strings from scratch.
I’ve used https://github.com/SheetJS/js-xlsx  in the past and liked it

Intermediate Answered on September 24, 2019.

Sorry I forgot to invert my commenting blocks. I fixed it now.

on September 24, 2019.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.