Guest User

table-export.util.js

a guest
Sep 4th, 2023
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { csvInjectionProtector } from '~/common/utils/common.utils.js';
  2.  
  3. export async function exportTableAsXLSX(
  4.   table,
  5.   filename,
  6.   applyFilters = true,
  7. ) {
  8.   const XLSX = await import('xlsx-js-style');
  9.   const wb = XLSX.utils.book_new();
  10.  
  11.   const lastHeaderGroup = table.getHeaderGroups().at(-1);
  12.   if (!lastHeaderGroup) {
  13.     console.error('No header groups found', table.getHeaderGroups());
  14.     return;
  15.   }
  16.  
  17.   const parent_headers = [];
  18.   let actual_headers = [];
  19.   let is_add_parent_headers = false;
  20.   actual_headers = lastHeaderGroup.headers
  21.     .filter(h => h.column.getIsVisible())
  22.     .map((header) => {
  23.       if (header.column.parent === undefined) {
  24.         parent_headers.push({
  25.           v: '',
  26.           s: { font: { bold: true } },
  27.         });
  28.       }
  29.       else {
  30.         parent_headers.push({
  31.           v: header.column.parent.columnDef.header,
  32.           s: { font: { bold: true } },
  33.         });
  34.         is_add_parent_headers = true;
  35.       }
  36.  
  37.       return {
  38.         v: header.column.columnDef.header,
  39.         s: { font: { bold: true } },
  40.       };
  41.     });
  42.  
  43.   const exportRows = applyFilters ? table.getFilteredRowModel().rows : table.getCoreRowModel().rows;
  44.  
  45.   const data = [];
  46.   exportRows.forEach((row) => {
  47.     const cells = row.getVisibleCells();
  48.     const values = cells.map(cell => cell.getValue() ?? '');
  49.     data.push(values);
  50.   });
  51.   const all_headers = [];
  52.   if (is_add_parent_headers)
  53.     all_headers.push(parent_headers);
  54.   all_headers.push(actual_headers);
  55.  
  56.   for (const arr of all_headers)
  57.     for (let j = 0; j < arr.length; j++)
  58.       if (typeof arr[j] === 'string' || arr[j] instanceof String)
  59.         arr[j] = csvInjectionProtector(arr[j]);
  60.   for (const arr of data)
  61.     for (let j = 0; j < arr.length; j++)
  62.       if (typeof arr[j] === 'string' || arr[j] instanceof String)
  63.         arr[j] = csvInjectionProtector(arr[j]);
  64.  
  65.   const ws = XLSX.utils.aoa_to_sheet([
  66.     ...all_headers,
  67.     ...data,
  68.   ]);
  69.  
  70.   const to_merge_pairs = [];
  71.   let current_pair = { first: null, second: null };
  72.   for (let i = 0; i < parent_headers.length - 1; i++) {
  73.     if (parent_headers[i].v === '') {
  74.       if (current_pair.first !== null) {
  75.         to_merge_pairs.push(current_pair);
  76.         current_pair = { first: null, second: null };
  77.       }
  78.       continue;
  79.     }
  80.     if (parent_headers[i].v === parent_headers[i + 1].v) {
  81.       if (current_pair.first === null)
  82.         current_pair.first = i;
  83.       current_pair.second = i + 1;
  84.     }
  85.     else {
  86.       if (current_pair.first !== null)
  87.         to_merge_pairs.push(current_pair);
  88.       current_pair = { first: null, second: null };
  89.     }
  90.   }
  91.  
  92.   const mergeCell = [];
  93.   to_merge_pairs.forEach((item) => {
  94.     mergeCell.push({ s: { r: 0, c: item.first }, e: { r: 0, c: item.second } });
  95.   });
  96.   ws['!merges'] = mergeCell;
  97.  
  98.   // for csv: await wb.csv.writeBuffer();
  99.   XLSX.utils.book_append_sheet(wb, ws, 'Sheet 1');
  100.   XLSX.writeFile(wb, `${filename}.xlsx`);
  101. }
  102.  
Advertisement
Add Comment
Please, Sign In to add comment