Skip to content

Commit

Permalink
Add RTL support
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaister committed Jan 26, 2022
1 parent 36af415 commit 3892bfd
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
2 changes: 2 additions & 0 deletions dist/excellentexport.d.ts
Expand Up @@ -11,6 +11,7 @@ export interface ConvertOptions {
openAsDownload?: boolean;
format: ('csv' | 'xls' | 'xlsx');
filename?: string;
rtl?: boolean;
}
export interface FromOptions {
table?: (string | HTMLTableElement);
Expand All @@ -23,6 +24,7 @@ export interface SheetOptions {
filterRowFn?(row: any[]): boolean;
fixValue?(value: any, row: number, column: number): any;
fixArray?(array: any[][]): any[][];
rtl?: boolean;
}
declare const ExcellentExport: {
version: () => string;
Expand Down
2 changes: 1 addition & 1 deletion dist/excellentexport.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions dist/utils.d.ts
Expand Up @@ -17,6 +17,11 @@ export declare const getTable: (element: (HTMLTableElement | string)) => HTMLTab
* @param {*} element
*/
export declare const getAnchor: (element: (HTMLAnchorElement | string)) => HTMLAnchorElement;
/**
* Encode a value for CSV.
* @param {*} value
*/
export declare const fixCSVField: (value: string, csvDelimiter: string) => string;
export declare const tableToArray: (table: HTMLTableElement) => any[][];
export declare const tableToCSV: (table: HTMLTableElement, csvDelimiter?: string, csvNewLine?: string) => string;
export declare const createDownloadLink: (anchor: HTMLAnchorElement, base64data: string, exporttype: string, filename: string) => boolean;
Expand Down
69 changes: 69 additions & 0 deletions index.rtl.html
@@ -0,0 +1,69 @@
<html>
<head>
<meta charset="utf-8">
<title>Export to excel test</title>
<script src="dist/excellentexport.js"></script>
<style>
table, tr, td {
border: 1px black solid;
}
</style>
<script>
function openFile(format) {
return ExcellentExport.convert({
anchor: 'anchorNewApi-' + format,
filename: 'data_123.' + format,
format: format,
rtl: true,
}, [{
name: 'Sheet Name Here 1',
from: {
table: 'datatable'
}
}]);
}
</script>
</head>
<body>
<h1>ExcellentExport.js RTL text</h1>

Check on <a href="http://jordiburgos.com">jordiburgos.com</a> and <a href="https://github.com/jmaister/excellentexport">GitHub</a>.

<h3>Test page</h3>

Test table
<table id="datatable">
<thead>
<tr>
<th>االلغة</th>
<th>عدد الأحرف</th>
<th>Country</th>
<th>miص-ص</th>
</tr>
</thead>
<tbody>
<tr>
<td>العربية</td>
<td>٢٨</td>
<td>Earth</td>
<td>ن0ن</td>
</tr>
<tr>
<td>العبرية</td>
<td>٢٢</td>
<td>Isreal</td>
<td>ضSض</td>
</tr>
</tbody>
</table>

<br/>

<a download="data_123.xls" href="#" id="anchorNewApi-xls" onclick="return openFile('xls');">Export to Excel: XLS format</a>
<br/>
<a download="data_123.xlsx" href="#" id="anchorNewApi-xlsx" onclick="return openFile('xlsx');">Export to Excel: XLSX format</a>
<br/>
<a download="data_123.csv" href="#" id="anchorNewApi-csv" onclick="return openFile('csv');">Export to CSV</a>

</body>
</html>
14 changes: 10 additions & 4 deletions src/excellentexport.ts
Expand Up @@ -16,6 +16,7 @@ export interface ConvertOptions {
openAsDownload?: boolean,
format: ('csv' | 'xls' | 'xlsx'),
filename?: string,
rtl?: boolean,
}
export interface FromOptions {
table?: (string|HTMLTableElement),
Expand All @@ -28,6 +29,7 @@ export interface SheetOptions {
filterRowFn?(row:any[]): boolean ,
fixValue?(value:any, row:number, column:number): any,
fixArray?(array:any[][]): any[][],
rtl?: boolean
}


Expand All @@ -43,7 +45,8 @@ const ExcellentExport = function() {
anchor: String or HTML Element,
openAsDownload: boolean, // Use this options if not using an anchor tag
format: 'xlsx' or 'xls' or 'csv',
filename: String
filename: String,
rtl: boolean (optional), specify if all the workbook has text in RTL mode
}
Sheets must be an array of sheet configuration objects. Sheet description:
Expand All @@ -58,6 +61,7 @@ const ExcellentExport = function() {
filterRowFn: function(row) {return true}, // Function to decide which rows are returned
fixValue: function(value, row, column) {return fixedValue} // Function to fix values, receiving value, row num, column num
fixArray: function(array) {return array} // Function to manipulate the whole data array
rtl: boolean // optional: specify if the sheet has text in RTL mode
...
},
{
Expand All @@ -68,7 +72,8 @@ const ExcellentExport = function() {
const convert = function(options:ConvertOptions, sheets:SheetOptions[]) {
const workbook = {
SheetNames: [],
Sheets: {}
Sheets: {},
Views: []
};

if (!options.format) {
Expand All @@ -85,7 +90,7 @@ const ExcellentExport = function() {
}

// Select data source
let dataArray;
let dataArray: any[][];
if (sheetConf.from && sheetConf.from.table) {
dataArray = utils.tableToArray(utils.getTable(sheetConf.from.table));
} else if(sheetConf.from && sheetConf.from.array) {
Expand All @@ -107,7 +112,7 @@ const ExcellentExport = function() {
utils.removeColumns(dataArray, sheetConf.removeColumns);
}

// Convert data, by value
// Convert data. Function applied to each value independently, receiving (value, rownum, colnum)
if (sheetConf.fixValue && typeof sheetConf.fixValue === 'function') {
const fn = sheetConf.fixValue;
dataArray.map((r, rownum) => {
Expand All @@ -127,6 +132,7 @@ const ExcellentExport = function() {
workbook.SheetNames.push(name);
const worksheet = XLSX.utils.aoa_to_sheet(dataArray, {sheet: name} as XLSX.AOA2SheetOpts);
workbook.Sheets[name] = worksheet;
workbook.Views.push({RTL: options.rtl || sheetConf.rtl || false});
});

const wbOut:string = XLSX.write(workbook, {bookType: options.format, bookSST:true, type: 'binary'});
Expand Down

0 comments on commit 3892bfd

Please sign in to comment.