Skip to content

Commit cf11dc7

Browse files
authored
Merge pull request #657 from jmaister/rtl-support-ts
Add RTL support
2 parents 36af415 + 3892bfd commit cf11dc7

5 files changed

+87
-5
lines changed

dist/excellentexport.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface ConvertOptions {
1111
openAsDownload?: boolean;
1212
format: ('csv' | 'xls' | 'xlsx');
1313
filename?: string;
14+
rtl?: boolean;
1415
}
1516
export interface FromOptions {
1617
table?: (string | HTMLTableElement);
@@ -23,6 +24,7 @@ export interface SheetOptions {
2324
filterRowFn?(row: any[]): boolean;
2425
fixValue?(value: any, row: number, column: number): any;
2526
fixArray?(array: any[][]): any[][];
27+
rtl?: boolean;
2628
}
2729
declare const ExcellentExport: {
2830
version: () => string;

dist/excellentexport.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/utils.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export declare const getTable: (element: (HTMLTableElement | string)) => HTMLTab
1717
* @param {*} element
1818
*/
1919
export declare const getAnchor: (element: (HTMLAnchorElement | string)) => HTMLAnchorElement;
20+
/**
21+
* Encode a value for CSV.
22+
* @param {*} value
23+
*/
24+
export declare const fixCSVField: (value: string, csvDelimiter: string) => string;
2025
export declare const tableToArray: (table: HTMLTableElement) => any[][];
2126
export declare const tableToCSV: (table: HTMLTableElement, csvDelimiter?: string, csvNewLine?: string) => string;
2227
export declare const createDownloadLink: (anchor: HTMLAnchorElement, base64data: string, exporttype: string, filename: string) => boolean;

index.rtl.html

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Export to excel test</title>
5+
<script src="dist/excellentexport.js"></script>
6+
<style>
7+
table, tr, td {
8+
border: 1px black solid;
9+
}
10+
</style>
11+
<script>
12+
function openFile(format) {
13+
return ExcellentExport.convert({
14+
anchor: 'anchorNewApi-' + format,
15+
filename: 'data_123.' + format,
16+
format: format,
17+
rtl: true,
18+
}, [{
19+
name: 'Sheet Name Here 1',
20+
from: {
21+
table: 'datatable'
22+
}
23+
}]);
24+
}
25+
</script>
26+
</head>
27+
<body>
28+
<h1>ExcellentExport.js RTL text</h1>
29+
30+
Check on <a href="http://jordiburgos.com">jordiburgos.com</a> and <a href="https://github.com/jmaister/excellentexport">GitHub</a>.
31+
32+
<h3>Test page</h3>
33+
34+
Test table
35+
<table id="datatable">
36+
<thead>
37+
<tr>
38+
<th>االلغة</th>
39+
<th>عدد الأحرف</th>
40+
<th>Country</th>
41+
<th>miص-ص</th>
42+
</tr>
43+
</thead>
44+
<tbody>
45+
<tr>
46+
<td>العربية</td>
47+
<td>٢٨</td>
48+
<td>Earth</td>
49+
<td>ن0ن</td>
50+
</tr>
51+
<tr>
52+
<td>العبرية</td>
53+
<td>٢٢</td>
54+
<td>Isreal</td>
55+
<td>ضSض</td>
56+
</tr>
57+
</tbody>
58+
</table>
59+
60+
<br/>
61+
62+
<a download="data_123.xls" href="#" id="anchorNewApi-xls" onclick="return openFile('xls');">Export to Excel: XLS format</a>
63+
<br/>
64+
<a download="data_123.xlsx" href="#" id="anchorNewApi-xlsx" onclick="return openFile('xlsx');">Export to Excel: XLSX format</a>
65+
<br/>
66+
<a download="data_123.csv" href="#" id="anchorNewApi-csv" onclick="return openFile('csv');">Export to CSV</a>
67+
68+
</body>
69+
</html>

src/excellentexport.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ConvertOptions {
1616
openAsDownload?: boolean,
1717
format: ('csv' | 'xls' | 'xlsx'),
1818
filename?: string,
19+
rtl?: boolean,
1920
}
2021
export interface FromOptions {
2122
table?: (string|HTMLTableElement),
@@ -28,6 +29,7 @@ export interface SheetOptions {
2829
filterRowFn?(row:any[]): boolean ,
2930
fixValue?(value:any, row:number, column:number): any,
3031
fixArray?(array:any[][]): any[][],
32+
rtl?: boolean
3133
}
3234

3335

@@ -43,7 +45,8 @@ const ExcellentExport = function() {
4345
anchor: String or HTML Element,
4446
openAsDownload: boolean, // Use this options if not using an anchor tag
4547
format: 'xlsx' or 'xls' or 'csv',
46-
filename: String
48+
filename: String,
49+
rtl: boolean (optional), specify if all the workbook has text in RTL mode
4750
}
4851
4952
Sheets must be an array of sheet configuration objects. Sheet description:
@@ -58,6 +61,7 @@ const ExcellentExport = function() {
5861
filterRowFn: function(row) {return true}, // Function to decide which rows are returned
5962
fixValue: function(value, row, column) {return fixedValue} // Function to fix values, receiving value, row num, column num
6063
fixArray: function(array) {return array} // Function to manipulate the whole data array
64+
rtl: boolean // optional: specify if the sheet has text in RTL mode
6165
...
6266
},
6367
{
@@ -68,7 +72,8 @@ const ExcellentExport = function() {
6872
const convert = function(options:ConvertOptions, sheets:SheetOptions[]) {
6973
const workbook = {
7074
SheetNames: [],
71-
Sheets: {}
75+
Sheets: {},
76+
Views: []
7277
};
7378

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

8792
// Select data source
88-
let dataArray;
93+
let dataArray: any[][];
8994
if (sheetConf.from && sheetConf.from.table) {
9095
dataArray = utils.tableToArray(utils.getTable(sheetConf.from.table));
9196
} else if(sheetConf.from && sheetConf.from.array) {
@@ -107,7 +112,7 @@ const ExcellentExport = function() {
107112
utils.removeColumns(dataArray, sheetConf.removeColumns);
108113
}
109114

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

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

0 commit comments

Comments
 (0)