- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2024 12:48 AM
Use Case:
Attach CSV with tabs.
I know how to attach CSV but how to do tabs?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:04 AM - edited 07-03-2024 06:06 AM
To Generate CSV Files with Multiple Tabs in ServiceNow :
- 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.
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");
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 02:53 AM
What I meant was creating a CSV/Excel programmatically / through code with multiple tabs in the excel.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 03:29 AM - edited 07-03-2024 03:58 AM
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:
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 .
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-03-2024 06:04 AM - edited 07-03-2024 06:06 AM
To Generate CSV Files with Multiple Tabs in ServiceNow :
- 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.
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");
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