Attach CSV with Tabs

ican
Tera Contributor

Use Case:

Attach CSV with tabs.

I know how to attach CSV but how to do tabs?

 

Thanks!

1 ACCEPTED SOLUTION

To Generate CSV Files with Multiple Tabs in ServiceNow :

 

  1. Create Multiple CSV Files: Each tab will be a separate CSV file. You need to write the data for each tab to a different CSV file.

Here’s an example of how you can create multiple CSV files in a ServiceNow script:

 

 

// Example to generate multiple CSV files
var csv1 = "Header1,Header2,Header3\nRow1Col1,Row1Col2,Row1Col3\nRow2Col1,Row2Col2,Row2Col3";
var csv2 = "HeaderA,HeaderB,HeaderC\nRow1ColA,Row1ColB,Row1ColC\nRow2ColA,Row2ColB,Row2ColC";

// Function to create CSV attachment
function createCSVAttachment(csvContent, fileName) {
    var attachment = new GlideSysAttachment();
    var gr = new GlideRecord('sys_attachment');
    var file = new GlideSysAttachment().write(gr, fileName, 'text/csv', csvContent);
    return file;
}

createCSVAttachment(csv1, "Tab1.csv");
createCSVAttachment(csv2, "Tab2.csv");

 

This will create two attachments in the sys_attachment table in ServiceNow which can be downloaded in excel format.

Sanjana8_0-1720011501327.png

 

Combine CSV Files (Optional): If you need to combine the CSV files into a single file, you can use a delimiter (such as ---) to separate the data from different tabs.

 

var combinedCSV = csv1 + "\n---\n" + csv2;
createCSVAttachment(combinedCSV, "CombinedTabs.csv");

 

 

Sanjana8_1-1720011656779.png

 

 

Sanjana8_2-1720011679490.png

 

However to see it as a true tabs( separate sheets under 1 workbook), 

you should use an Excel file format. ServiceNow does not natively support creating Excel files, so you might need to use a script or a custom ServiceNow integration to convert the CSV files into an Excel file with multiple sheets.

Here's an example using the excel js library in a Node.js environment (outside of ServiceNow):

 

 

const ExcelJS = require('exceljs');

async function createExcelFile() {
    const workbook = new ExcelJS.Workbook();
    const sheet1 = workbook.addWorksheet('Tab1');
    const sheet2 = workbook.addWorksheet('Tab2');
    
    // Add rows to the sheets
    sheet1.addRows([
        ['Header1', 'Header2', 'Header3'],
        ['Row1Col1', 'Row1Col2', 'Row1Col3'],
        ['Row2Col1', 'Row2Col2', 'Row2Col3']
    ]);
    
    sheet2.addRows([
        ['HeaderA', 'HeaderB', 'HeaderC'],
        ['Row1ColA', 'Row1ColB', 'Row1ColC'],
        ['Row2ColA', 'Row2ColB', 'Row2ColC']
    ]);

    // Write to file
    await workbook.xlsx.writeFile('MultiSheetExcel.xlsx');
}

createExcelFile();

 

If you want to use a similar approach within ServiceNow, you'll need to look into custom integrations or the use of REST APIs to send data to an external service that can handle Excel file creation, which will be quite a complicate process.

 

I hope this is helpful for you.

 

Thankyou

Sanjana 

 

 

 

View solution in original post

12 REPLIES 12

ican
Tera Contributor

What I meant was creating a CSV/Excel programmatically / through code with multiple tabs in the excel.

Sorry, Generating a CSV file with multiple tabs (sheets) in ServiceNow is not directly supported through native ServiceNow APIs or scripting capabilities. ServiceNow primarily supports generating and exporting data in a single CSV format for straightforward data export needs.

However, if you need to generate a CSV file with multiple tabs (sheets), you typically have a few alternative approaches:

 

Alternative Approaches:

  1. Use Third-Party Libraries or External Integrations:

    • Consider leveraging third-party JavaScript libraries or external integrations that support Excel file generation and manipulation. These libraries often provide more advanced features such as multiple sheets (tabs) in Excel files .
  2. Generate Separate CSV Files:

    • Instead of creating a single CSV file with multiple tabs, generate separate CSV files for each "tab" (sheet) of data. Users can then download and consolidate these files using Excel or other spreadsheet software.

If required I can share script that demonstrates how to generate and download multiple CSV files (each representing a "tab") in ServiceNow

 

Thankyou 

Sanjana

 

To Generate CSV Files with Multiple Tabs in ServiceNow :

 

  1. Create Multiple CSV Files: Each tab will be a separate CSV file. You need to write the data for each tab to a different CSV file.

Here’s an example of how you can create multiple CSV files in a ServiceNow script:

 

 

// Example to generate multiple CSV files
var csv1 = "Header1,Header2,Header3\nRow1Col1,Row1Col2,Row1Col3\nRow2Col1,Row2Col2,Row2Col3";
var csv2 = "HeaderA,HeaderB,HeaderC\nRow1ColA,Row1ColB,Row1ColC\nRow2ColA,Row2ColB,Row2ColC";

// Function to create CSV attachment
function createCSVAttachment(csvContent, fileName) {
    var attachment = new GlideSysAttachment();
    var gr = new GlideRecord('sys_attachment');
    var file = new GlideSysAttachment().write(gr, fileName, 'text/csv', csvContent);
    return file;
}

createCSVAttachment(csv1, "Tab1.csv");
createCSVAttachment(csv2, "Tab2.csv");

 

This will create two attachments in the sys_attachment table in ServiceNow which can be downloaded in excel format.

Sanjana8_0-1720011501327.png

 

Combine CSV Files (Optional): If you need to combine the CSV files into a single file, you can use a delimiter (such as ---) to separate the data from different tabs.

 

var combinedCSV = csv1 + "\n---\n" + csv2;
createCSVAttachment(combinedCSV, "CombinedTabs.csv");

 

 

Sanjana8_1-1720011656779.png

 

 

Sanjana8_2-1720011679490.png

 

However to see it as a true tabs( separate sheets under 1 workbook), 

you should use an Excel file format. ServiceNow does not natively support creating Excel files, so you might need to use a script or a custom ServiceNow integration to convert the CSV files into an Excel file with multiple sheets.

Here's an example using the excel js library in a Node.js environment (outside of ServiceNow):

 

 

const ExcelJS = require('exceljs');

async function createExcelFile() {
    const workbook = new ExcelJS.Workbook();
    const sheet1 = workbook.addWorksheet('Tab1');
    const sheet2 = workbook.addWorksheet('Tab2');
    
    // Add rows to the sheets
    sheet1.addRows([
        ['Header1', 'Header2', 'Header3'],
        ['Row1Col1', 'Row1Col2', 'Row1Col3'],
        ['Row2Col1', 'Row2Col2', 'Row2Col3']
    ]);
    
    sheet2.addRows([
        ['HeaderA', 'HeaderB', 'HeaderC'],
        ['Row1ColA', 'Row1ColB', 'Row1ColC'],
        ['Row2ColA', 'Row2ColB', 'Row2ColC']
    ]);

    // Write to file
    await workbook.xlsx.writeFile('MultiSheetExcel.xlsx');
}

createExcelFile();

 

If you want to use a similar approach within ServiceNow, you'll need to look into custom integrations or the use of REST APIs to send data to an external service that can handle Excel file creation, which will be quite a complicate process.

 

I hope this is helpful for you.

 

Thankyou

Sanjana