Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Format created Excel file

DoDo labs___
Mega Sage

“I have the script below that creates an Excel file and attaches it to an Incident. How can I place the last three cells (Test 7, Test 8, Test 9) under the Test6 cell? Just like in the picture.”

 

DoDolabs____0-1763315168954.png

 

var rows_data = "";
var columns_of_excel = "<?xml version='1.0' encoding='UTF-8'?>";
columns_of_excel += "<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' ";
columns_of_excel += "xmlns:x='urn:schemas-microsoft-com:office:excel' ";
columns_of_excel += "xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>";

columns_of_excel += "<Styles>";

columns_of_excel += "<Style ss:ID='BoldHeader'>";
columns_of_excel += " <Font ss:Bold='1'/>";
columns_of_excel += "</Style>";

columns_of_excel += "<Style ss:ID='BlueCell'>";
columns_of_excel += "<Interior ss:Color='#0000FF' ss:Pattern='Solid'/>";
columns_of_excel += "<Font ss:Color='#FFFFFF' ss:Bold='1'/>";
columns_of_excel += "<Alignment ss:Horizontal='Center' ss:Vertical='Center' ss:WrapText='1'/>";
columns_of_excel += "<Borders>";
columns_of_excel += "<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "</Borders>";
columns_of_excel += "</Style>";

columns_of_excel += "<Style ss:ID='BorderedCell'>";
columns_of_excel += " <Borders>";
columns_of_excel += " <Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += " <Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += " <Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += " <Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += " </Borders>";
columns_of_excel += "</Style>";

columns_of_excel += "</Styles>";

columns_of_excel += "<Worksheet ss:Name='NMHH havi támogatási riport'>";
columns_of_excel += "<Table>";

columns_of_excel += "<Row>";
columns_of_excel += "<Cell ss:StyleID='BoldHeader'><Data ss:Type='String'>Átalánydíjas teljesítés részletezése:</Data></Cell>";
columns_of_excel += "</Row>";

rows_data += "<Row>";
rows_data += "<Cell ss:StyleID='BlueCell' ss:MergeAcross='17'><Data ss:Type='String'>A teljesítési időszakban lezárt hibák</Data></Cell>";
rows_data += "</Row>";

rows_data += "<Row>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test1</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test2</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test3</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test4</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test5</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeAcross='3'><Data ss:Type='String'>Test6</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell'><Data ss:Type='String'>Test7</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell'><Data ss:Type='String'>Test8</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell'><Data ss:Type='String'>Test9</Data></Cell>";
rows_data += "</Row>";

columns_of_excel += rows_data;
columns_of_excel += "</Table>";
columns_of_excel += "</Worksheet>";
columns_of_excel += "</Workbook>";

var columns_and_rows = columns_of_excel;

var inc = new GlideRecord('incident');
inc.get('e61cf9248751761003560dc8cebb35b8');

var attachment = new GlideSysAttachment();
attachment.write(inc, "NMHH.xls", "application/vnd.ms-excel", columns_and_rows);

1 ACCEPTED SOLUTION

Hi can this updated code: 

 

var rows_data = "";
var columns_of_excel = "<?xml version='1.0' encoding='UTF-8'?>";
columns_of_excel += "<Workbook xmlns='urn:schemas-microsoft-com:office:spreadsheet' ";
columns_of_excel += "xmlns:x='urn:schemas-microsoft-com:office:excel' ";
columns_of_excel += "xmlns:ss='urn:schemas-microsoft-com:office:spreadsheet'>";

columns_of_excel += "<Styles>";
columns_of_excel += "<Style ss:ID='BoldHeader'>";
columns_of_excel += " <Font ss:Bold='1'/>";
columns_of_excel += "</Style>";

columns_of_excel += "<Style ss:ID='BlueCell'>";
columns_of_excel += "<Interior ss:Color='#0000FF' ss:Pattern='Solid'/>";
columns_of_excel += "<Font ss:Color='#FFFFFF' ss:Bold='1'/>";
columns_of_excel += "<Alignment ss:Horizontal='Center' ss:Vertical='Center' ss:WrapText='1'/>";
columns_of_excel += "<Borders>";
columns_of_excel += "<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='2'/>";
columns_of_excel += "</Borders>";
columns_of_excel += "</Style>";

columns_of_excel += "<Style ss:ID='BorderedCell'>";
columns_of_excel += "<Borders>";
columns_of_excel += "<Border ss:Position='Bottom' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += "<Border ss:Position='Top' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += "<Border ss:Position='Left' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += "<Border ss:Position='Right' ss:LineStyle='Continuous' ss:Weight='1'/>";
columns_of_excel += "</Borders>";
columns_of_excel += "</Style>";

columns_of_excel += "</Styles>";

// CORRETTO: Nome worksheet senza caratteri speciali
columns_of_excel += "<Worksheet ss:Name='Report'>";
columns_of_excel += "<Table>";

// Prima riga
columns_of_excel += "<Row>";
columns_of_excel += "<Cell ss:StyleID='BoldHeader'><Data ss:Type='String'>Atalanyijas teljesites reszletezese:</Data></Cell>";
columns_of_excel += "</Row>";

// Riga blu
rows_data += "<Row>";
rows_data += "<Cell ss:StyleID='BlueCell' ss:MergeAcross='7'><Data ss:Type='String'>A teljesitesi idoszakban lezart hibak</Data></Cell>";
rows_data += "</Row>";

// CORRETTO: Prima riga header con merge
rows_data += "<Row>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test1</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test2</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test3</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test4</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeDown='1'><Data ss:Type='String'>Test5</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell' ss:MergeAcross='2'><Data ss:Type='String'>Test6</Data></Cell>";
rows_data += "</Row>";

// CORRETTO: Seconda riga - NON mettere celle per Test1-5 (sono merged)
rows_data += "<Row>";
rows_data += "<Cell ss:Index='6' ss:StyleID='BorderedCell'><Data ss:Type='String'>Test7</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell'><Data ss:Type='String'>Test8</Data></Cell>";
rows_data += "<Cell ss:StyleID='BorderedCell'><Data ss:Type='String'>Test9</Data></Cell>";
rows_data += "</Row>";

columns_of_excel += rows_data;
columns_of_excel += "</Table>";
columns_of_excel += "</Worksheet>";
columns_of_excel += "</Workbook>";

var columns_and_rows = columns_of_excel;

var inc = new GlideRecord('incident');
inc.get('e61cf9248751761003560dc8cebb35b8');

var attachment = new GlideSysAttachment();
attachment.write(inc, "NMHH.xls", "application/vnd.ms-excel", columns_and_rows);

 

View solution in original post

7 REPLIES 7

Thank you!

Nice to help

Ankur Bawiskar
Tera Patron
Tera Patron

@DoDo labs___ 

are you able to create XLSX file or it's just XLS?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader