formatting for XLS export

AndersonHenriqu
Tera Contributor

I created a related list in a table, and I would like to export this data via email notification in XLS format, the sending is being done and it is arriving in the correct format, but I cannot handle the special characters and it is not respecting the columns correctly from the table.

 

Notification script: 

 

 

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
/* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
/* Optional GlideRecord */ event) {

var msg = 'PRESTADOR\tExame\tEMPRESA\tCPFFUNCIONARIO\tNOMEFUNCIONARIO\tCategoria do serviço\tDATAEXAME_str\tDATAFICHA_str\tCusto total (R$)\n';

var gr = new GlideRecord('u_provisionamentos_mensais');
gr.addEncodedQuery('u_pgf=' + current.sys_id + '^ORDERBYDESCu_prestador');
gr.query();

while (gr.next()) {
msg += gr.u_prestador.getDisplayValue() + '\t' + gr.u_exame.getDisplayValue() + '\t' + gr.u_empresa.getDisplayValue() + '\t' + gr.u_cpffuncionario.getDisplayValue() + '\t' + gr.u_nomefuncionario.getDisplayValue() + '\t' + gr.u_categoria_do_servi_o.getDisplayValue() + '\t' + gr.u_dataexame_str.getDisplayValue() + '\t' + gr.u_dataficha_str.getDisplayValue() + '\t' + gr.u_custo_total.getDisplayValue() + '\n';
}

var attachment = new GlideSysAttachment();

var rec = new GlideRecord('sn_customerservice_pgf_case');
rec.get('sys_id', current.sys_id);
var fileName = 'Teste.xls';
var contentType = 'application/vnd.ms-excel';
var content = msg;

var agr = attachment.write(rec, fileName, contentType, content);

gs.info('O sys_id do anexo é: ' + agr);

var tableName = current.getTableName();
g_navigation.open('/' + tableName + '_list.do?sysparm_query=sys_id=' + current.sys_id + '&EXCEL&sysparm_view=ess', '_blank');

})(current, template, email, email_action, event);

2 REPLIES 2

Tony Chatfield1
Kilo Patron

Hi, what exactly do you mean by 'but I cannot handle the special characters' and 'it is not respecting the columns correctly from the table'.
Excel files use a propriety format and creating a tab delimited text file and naming it with an xls extension\file type, does not make the file an excel file, as a result MS Excel may not read it correctly when opening.

I would consider creating a CSV, you could also look at encapsulating all values in double quotes so that when the file is opened the content is seen by Excel as strings.

So, in CSV I can do it, but the file needs to be XLS or XLSX