PageSourceSearch

https://aerc.org/wp-content/themes/aerc/js/xls-export.js?ver=6.7

js aerc.org collected 2026-09-24 20:16:53 UTC 4,228 bytes, 119 lines download raw bytes

1/**
2 * __  ___     _____                       _
3 * \ \/ / |___| ____|_  ___ __   ___  _ __| |_
4 *  \  /| / __|  _| \ \/ / '_ \ / _ \| '__| __|
5 *  /  \| \__ \ |___ >  <| |_) | (_) | |  | |_
6 * /_/\_\_|___/_____/_/\_\ .__/ \___/|_|   \__|
7 *                       |_|
8 * 6/12/2017
9 * Daniel Blanco Parla
10 * https://github.com/deblanco/xlsExport
11 */
12
13class XlsExport {
14  // data: array of objects with the data for each row of the table
15  // name: title for the worksheet
16  constructor(data, title = 'Worksheet') {
17    // input validation: new xlsExport([], String)
18    if (!Array.isArray(data) || (typeof title !== 'string' || Object.prototype.toString.call(title) !== '[object String]')) {
19     // throw new Error('Invalid input types: new xlsExport(Array [], String)');
20    }
21
22    this._data = data;
23    this._title = title;
24  }
25
26  set setData(data) {
27    if (!Array.isArray(data)) throw new Error('Invalid input type: setData(Array [])');
28
29    this._data = data;
30  }
31
32  get getData() {
33    return this._data;
34  }
35
36  exportToXLS(fileName = 'export.xls', rtl = false) {
37    if (typeof fileName !== 'string' || Object.prototype.toString.call(fileName) !== '[object String]') {
38      throw new Error('Invalid input type: exportToCSV(String)');
39    }
40
41    var TEMPLATE_XLS_val = `
42    <html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
43    <meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"/>
44    <head><!--[if gte mso 9]><xml>
45    <x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{title}</x:Name><x:WorksheetOptions>`;
46
47    if (rtl)
48        TEMPLATE_XLS_val = TEMPLATE_XLS_val + `<x:DisplayRightToLeft/>`;
49
50    TEMPLATE_XLS_val = TEMPLATE_XLS_val +
51    `<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml>
52    <![endif]--></head>
53    <body>{table}</body></html>`;
54
55    const TEMPLATE_XLS = TEMPLATE_XLS_val;
56    
57    const MIME_XLS = 'application/vnd.ms-excel;base64,';
58
59    const parameters = {
60      title: this._title,
61      table: this.objectToTable(),
62    };
63    const computeOutput = TEMPLATE_XLS.replace(/{(\w+)}/g, (x, y) => parameters[y]);
64
65    const computedXLS = new Blob([computeOutput], {
66      type: MIME_XLS,
67    });
68    const xlsLink = window.URL.createObjectURL(computedXLS);
69    this.downloadFile(xlsLink, fileName);
70  }
71
72  exportToCSV(fileName = 'export.csv') {
73    if (typeof fileName !== 'string' || Object.prototype.toString.call(fileName) !== '[object String]') {
74      throw new Error('Invalid input type: exportToCSV(String)');
75    }
76    const computedCSV = new Blob([this.objectToSemicolons()], {
77      type: 'text/csv;charset=utf-8',
78    });
79    const csvLink = window.URL.createObjectURL(computedCSV);
80    this.downloadFile(csvLink, fileName);
81  }
82
83  downloadFile(output, fileName) {
84    const link = document.createElement('a');
85    document.body.appendChild(link);
86    link.download = fileName;
87    link.href = output;
88    link.click();
89  }
90
91  toBase64(string) {
92    return window.btoa(unescape(encodeURIComponent(string)));
93  }
94
95  objectToTable() {
96    // extract keys from the first object, will be the title for each column
97    const colsHead = `<tr>${Object.keys(this._data[0]).map(key => `<td>${key}</td>`).join('')}</tr>`;
98
99    const colsData = this._data.map(obj => [`<tr>
100                ${Object.keys(obj).map(col => `<td>${(obj[col] === 0 || obj[col]) ? obj[col] : ''}</td>`).join('')}
101            </tr>
101`]) // 'null' values not showed but zero value is showed
102      .join('');
103
104    return `<table>${colsHead}${colsData}</table>`.trim(); // remove spaces...
105  }
106
107  objectToSemicolons() {
108    const colsHead = Object.keys(this._data[0]).map(key => [key]).join(';');
109    const colsData = this._data.map(obj => [ // obj === row
110      Object.keys(obj).map(col => [
111        obj[col], // row[column]
112      ]).join(';'), // join the row with ';'
113    ]).join('\n'); // end of row
114
115    return `${colsHead}\n${colsData}`;
116  }
117}
118
119//export default XlsExport; // comment this line to babelize

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.