Skip to content

Commit 4916ead

Browse files
authored
Merge pull request #356 from alitaheri/support-rtl-sheet
Fix right to left issues
2 parents 9802435 + a78f39e commit 4916ead

File tree

7 files changed

+56
-21
lines changed

7 files changed

+56
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ Each view also supports various properties:
332332
| Name | Default | Description |
333333
| ----------------- | --------- | ----------- |
334334
| state | 'normal' | Controls the view state - one of normal, frozen or split |
335+
| rightToLeft | false | Sets the worksheet view's orientation to right-to-left |
335336
| activeCell | undefined | The currently selected cell |
336337
| showRuler | true | Shows or hides the ruler in Page Layout |
337338
| showRowColHeaders | true | Shows or hides the row and column headers (e.g. A1, B1 at the top and 1,2,3 on the left |

lib/doc/enums.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ module.exports = {
3939
Xlsx: 1,
4040
},
4141
ReadingOrder: {
42-
RightToLeft: 1,
43-
LeftToRight: 2,
42+
LeftToRight: 1,
43+
RightToLeft: 2,
4444
},
4545
ErrorValue: {
4646
NotApplicable: '#N/A',

lib/xlsx/xform/sheet/sheet-view-xform.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ utils.inherits(SheetViewXform, BaseXform, {
4343
xmlStream.addAttribute(name, value);
4444
}
4545
};
46+
add('rightToLeft', '1', model.rightToLeft === true);
4647
add('tabSelected', '1', model.tabSelected);
4748
add('showRuler', '0', model.showRuler === false);
4849
add('showRowColHeaders', '0', model.showRowColHeaders === false);
@@ -110,6 +111,7 @@ utils.inherits(SheetViewXform, BaseXform, {
110111
case 'sheetView':
111112
this.sheetView = {
112113
workbookViewId: parseInt(node.attributes.workbookViewId, 10),
114+
rightToLeft: node.attributes.rightToLeft === '1',
113115
tabSelected: node.attributes.tabSelected === '1',
114116
showRuler: !(node.attributes.showRuler === '0'),
115117
showRowColHeaders: !(node.attributes.showRowColHeaders === '0'),
@@ -150,6 +152,7 @@ utils.inherits(SheetViewXform, BaseXform, {
150152
if (this.sheetView && this.pane) {
151153
model = this.model = {
152154
workbookViewId: this.sheetView.workbookViewId,
155+
rightToLeft: this.sheetView.rightToLeft,
153156
state: VIEW_STATES[this.pane.state] || 'split', // split is default
154157
xSplit: this.pane.xSplit,
155158
ySplit: this.pane.ySplit,
@@ -173,6 +176,7 @@ utils.inherits(SheetViewXform, BaseXform, {
173176
} else {
174177
model = this.model = {
175178
workbookViewId: this.sheetView.workbookViewId,
179+
rightToLeft: this.sheetView.rightToLeft,
176180
state: 'normal',
177181
showRuler: this.sheetView.showRuler,
178182
showRowColHeaders: this.sheetView.showRowColHeaders,

lib/xlsx/xform/style/alignment-xform.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,12 @@ var validation = {
5555
value = utils.validInt(value);
5656
return Math.max(0, value);
5757
},
58-
readingOrderValues: [
59-
{k: 'r2l', v: 1},
60-
{k: 'l2r', v: 2},
61-
{k: Enums.ReadingOrder.RightToLeft, v: 1},
62-
{k: Enums.ReadingOrder.LeftToRight, v: 2}
63-
].reduce((p, v) => { p[v.k] = v.v; return p; }, {}),
64-
6558
readingOrder: function(value) {
66-
return this.readingOrderValues[value];
59+
switch (value) {
60+
case 'ltr': return Enums.ReadingOrder.LeftToRight;
61+
case 'rtl': return Enums.ReadingOrder.RightToLeft;
62+
default: return undefined;
63+
}
6764
}
6865
};
6966

@@ -155,7 +152,7 @@ utils.inherits(AlignmentXform, BaseXform, {
155152
add(node.attributes.shrinkToFit, 'shrinkToFit', !!node.attributes.shrinkToFit);
156153
add(node.attributes.indent, 'indent', parseInt(node.attributes.indent, 10));
157154
add(node.attributes.textRotation, 'textRotation', textRotationXform.toModel(node.attributes.textRotation));
158-
add(node.attributes.readingOrder, 'readingOrder', node.attributes.readingOrder);
155+
add(node.attributes.readingOrder, 'readingOrder', node.attributes.readingOrder === '2' ? 'rtl' : 'ltr');
159156

160157
this.model = valid ? model : null;
161158
},

spec/unit/xlsx/xform/sheet/data/sheet.1.3.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@
55
"tabColor": {"argb": "FF00FF00"},
66
"outlineLevelCol": 0, "outlineLevelRow": 0
77
},
8-
"views": [{"state": "normal", "showRuler": true, "showGridLines": true, "showRowColHeaders": true, "zoomScale": 100, "zoomScaleNormal": 100, "workbookViewId": 0}],
8+
"views": [{
9+
"state": "normal",
10+
"showRuler": true,
11+
"showGridLines": true,
12+
"showRowColHeaders": true,
13+
"zoomScale": 100,
14+
"zoomScaleNormal": 100,
15+
"workbookViewId": 0,
16+
"rightToLeft": false
17+
}],
918
"pageSetup": {
1019
"margins": {"left": 0.7, "right": 0.7, "top": 0.75, "bottom": 0.75, "header": 0.3, "footer": 0.3 },
1120
"firstPageNumber": 5, "useFirstPageNumber": true, "usePrinterDefaults": true, "copies": 3,

spec/unit/xlsx/xform/sheet/sheet-view-xform.spec.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ var expectations = [
1313
xml: '<sheetView workbookViewId="0">' +
1414
'<selection activeCell="G4" sqref="G4"/>' +
1515
'</sheetView>',
16-
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
16+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
1717
tests: ['render', 'renderIn', 'parse']
1818
},
1919
{
@@ -23,7 +23,7 @@ var expectations = [
2323
xml: '<sheetView workbookViewId="0" zoomScale="60" zoomScaleNormal="80">' +
2424
'<selection activeCell="G4" sqref="G4"/>' +
2525
'</sheetView>',
26-
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 60, zoomScaleNormal: 80},
26+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 60, zoomScaleNormal: 80},
2727
tests: ['render', 'renderIn', 'parse']
2828
},
2929
{
@@ -33,7 +33,7 @@ var expectations = [
3333
xml: '<sheetView workbookViewId="0" showRuler="0" showGridLines="0" showRowColHeaders="0">' +
3434
'<selection activeCell="G4" sqref="G4"/>' +
3535
'</sheetView>',
36-
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: false, showGridLines: false, showRowColHeaders: false, zoomScale: 100, zoomScaleNormal: 100},
36+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: false, showGridLines: false, showRowColHeaders: false, zoomScale: 100, zoomScaleNormal: 100},
3737
tests: ['render', 'renderIn', 'parse']
3838
},
3939
{
@@ -43,7 +43,7 @@ var expectations = [
4343
xml: '<sheetView workbookViewId="0" view="pageBreakPreview">' +
4444
'<selection activeCell="G4" sqref="G4"/>' +
4545
'</sheetView>',
46-
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100, style: 'pageBreakPreview'},
46+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100, style: 'pageBreakPreview'},
4747
tests: ['render', 'renderIn', 'parse']
4848
},
4949
{
@@ -54,7 +54,7 @@ var expectations = [
5454
'<pane xSplit="1234" ySplit="3456" topLeftCell="C3" activePane="bottomRight"/>' +
5555
'<selection pane="bottomRight" activeCell="B1" sqref="B1"/>' +
5656
'</sheetView>',
57-
parsedModel: {workbookViewId: 0, state: 'split', xSplit: 1234, ySplit: 3456, topLeftCell: 'C3', activeCell: 'B1', activePane: 'bottomRight', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
57+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'split', xSplit: 1234, ySplit: 3456, topLeftCell: 'C3', activeCell: 'B1', activePane: 'bottomRight', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
5858
tests: ['render', 'renderIn', 'parse']
5959
},
6060
{
@@ -65,7 +65,7 @@ var expectations = [
6565
'<pane xSplit="1234" ySplit="3456" topLeftCell="C3"/>' +
6666
'<selection activeCell="A1" sqref="A1"/>' +
6767
'</sheetView>',
68-
parsedModel: {workbookViewId: 0, state: 'split', xSplit: 1234, ySplit: 3456, topLeftCell: 'C3', activeCell: 'A1', activePane: 'topLeft', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
68+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'split', xSplit: 1234, ySplit: 3456, topLeftCell: 'C3', activeCell: 'A1', activePane: 'topLeft', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
6969
tests: ['render', 'renderIn', 'parse']
7070
},
7171
{
@@ -76,7 +76,7 @@ var expectations = [
7676
'<pane xSplit="2" ySplit="3" topLeftCell="C4" activePane="bottomRight" state="frozen"/>' +
7777
'<selection pane="bottomRight" activeCell="D5" sqref="D5"/>' +
7878
'</sheetView>',
79-
parsedModel: {workbookViewId: 0, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
79+
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
8080
tests: ['render', 'renderIn', 'parse']
8181
},
8282
{
@@ -96,10 +96,18 @@ var expectations = [
9696
'</sheetView>' +
9797
'</sheetViews>',
9898
parsedModel: [
99-
{workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
100-
{workbookViewId: 1, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100}
99+
{workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
100+
{workbookViewId: 1, rightToLeft: false, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100}
101101
],
102102
tests: ['render', 'renderIn', 'parse']
103+
},
104+
{
105+
title: 'Right To Left',
106+
create: () => new SheetViewXform(),
107+
preparedModel: {rightToLeft: true},
108+
xml: '<sheetView workbookViewId="0" rightToLeft="1"></sheetView>',
109+
parsedModel: {workbookViewId: 0, rightToLeft: true, state: 'normal', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
110+
tests: ['render', 'renderIn', 'parse']
103111
}
104112
];
105113

spec/unit/xlsx/xform/style/alignment-xform.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,22 @@ var expectations = [
155155
get parsedModel() { return this.preparedModel; },
156156
tests: ['render', 'renderIn', 'parse']
157157
},
158+
{
159+
title: 'Reading Order [Left To Right]',
160+
create: () => new AlignmentXform(),
161+
preparedModel: { readingOrder: 'ltr' },
162+
xml: '<alignment readingOrder="1"/>',
163+
get parsedModel() { return this.preparedModel; },
164+
tests: ['render', 'renderIn', 'parse']
165+
},
166+
{
167+
title: 'Reading Order [Right To Left]',
168+
create: () => new AlignmentXform(),
169+
preparedModel: { readingOrder: 'rtl' },
170+
xml: '<alignment readingOrder="2"/>',
171+
get parsedModel() { return this.preparedModel; },
172+
tests: ['render', 'renderIn', 'parse']
173+
},
158174
{
159175
title: 'Vertical Text',
160176
create: () => new AlignmentXform(),

0 commit comments

Comments
 (0)