Skip to content

Commit

Permalink
Merge pull request #356 from alitaheri/support-rtl-sheet
Browse files Browse the repository at this point in the history
Fix right to left issues
  • Loading branch information
guyonroche authored Jul 16, 2017
2 parents 9802435 + a78f39e commit 4916ead
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ Each view also supports various properties:
| Name | Default | Description |
| ----------------- | --------- | ----------- |
| state | 'normal' | Controls the view state - one of normal, frozen or split |
| rightToLeft | false | Sets the worksheet view's orientation to right-to-left |
| activeCell | undefined | The currently selected cell |
| showRuler | true | Shows or hides the ruler in Page Layout |
| showRowColHeaders | true | Shows or hides the row and column headers (e.g. A1, B1 at the top and 1,2,3 on the left |
Expand Down
4 changes: 2 additions & 2 deletions lib/doc/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ module.exports = {
Xlsx: 1,
},
ReadingOrder: {
RightToLeft: 1,
LeftToRight: 2,
LeftToRight: 1,
RightToLeft: 2,
},
ErrorValue: {
NotApplicable: '#N/A',
Expand Down
4 changes: 4 additions & 0 deletions lib/xlsx/xform/sheet/sheet-view-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ utils.inherits(SheetViewXform, BaseXform, {
xmlStream.addAttribute(name, value);
}
};
add('rightToLeft', '1', model.rightToLeft === true);
add('tabSelected', '1', model.tabSelected);
add('showRuler', '0', model.showRuler === false);
add('showRowColHeaders', '0', model.showRowColHeaders === false);
Expand Down Expand Up @@ -110,6 +111,7 @@ utils.inherits(SheetViewXform, BaseXform, {
case 'sheetView':
this.sheetView = {
workbookViewId: parseInt(node.attributes.workbookViewId, 10),
rightToLeft: node.attributes.rightToLeft === '1',
tabSelected: node.attributes.tabSelected === '1',
showRuler: !(node.attributes.showRuler === '0'),
showRowColHeaders: !(node.attributes.showRowColHeaders === '0'),
Expand Down Expand Up @@ -150,6 +152,7 @@ utils.inherits(SheetViewXform, BaseXform, {
if (this.sheetView && this.pane) {
model = this.model = {
workbookViewId: this.sheetView.workbookViewId,
rightToLeft: this.sheetView.rightToLeft,
state: VIEW_STATES[this.pane.state] || 'split', // split is default
xSplit: this.pane.xSplit,
ySplit: this.pane.ySplit,
Expand All @@ -173,6 +176,7 @@ utils.inherits(SheetViewXform, BaseXform, {
} else {
model = this.model = {
workbookViewId: this.sheetView.workbookViewId,
rightToLeft: this.sheetView.rightToLeft,
state: 'normal',
showRuler: this.sheetView.showRuler,
showRowColHeaders: this.sheetView.showRowColHeaders,
Expand Down
15 changes: 6 additions & 9 deletions lib/xlsx/xform/style/alignment-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,12 @@ var validation = {
value = utils.validInt(value);
return Math.max(0, value);
},
readingOrderValues: [
{k: 'r2l', v: 1},
{k: 'l2r', v: 2},
{k: Enums.ReadingOrder.RightToLeft, v: 1},
{k: Enums.ReadingOrder.LeftToRight, v: 2}
].reduce((p, v) => { p[v.k] = v.v; return p; }, {}),

readingOrder: function(value) {
return this.readingOrderValues[value];
switch (value) {
case 'ltr': return Enums.ReadingOrder.LeftToRight;
case 'rtl': return Enums.ReadingOrder.RightToLeft;
default: return undefined;
}
}
};

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

this.model = valid ? model : null;
},
Expand Down
11 changes: 10 additions & 1 deletion spec/unit/xlsx/xform/sheet/data/sheet.1.3.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@
"tabColor": {"argb": "FF00FF00"},
"outlineLevelCol": 0, "outlineLevelRow": 0
},
"views": [{"state": "normal", "showRuler": true, "showGridLines": true, "showRowColHeaders": true, "zoomScale": 100, "zoomScaleNormal": 100, "workbookViewId": 0}],
"views": [{
"state": "normal",
"showRuler": true,
"showGridLines": true,
"showRowColHeaders": true,
"zoomScale": 100,
"zoomScaleNormal": 100,
"workbookViewId": 0,
"rightToLeft": false
}],
"pageSetup": {
"margins": {"left": 0.7, "right": 0.7, "top": 0.75, "bottom": 0.75, "header": 0.3, "footer": 0.3 },
"firstPageNumber": 5, "useFirstPageNumber": true, "usePrinterDefaults": true, "copies": 3,
Expand Down
26 changes: 17 additions & 9 deletions spec/unit/xlsx/xform/sheet/sheet-view-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var expectations = [
xml: '<sheetView workbookViewId="0">' +
'<selection activeCell="G4" sqref="G4"/>' +
'</sheetView>',
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -23,7 +23,7 @@ var expectations = [
xml: '<sheetView workbookViewId="0" zoomScale="60" zoomScaleNormal="80">' +
'<selection activeCell="G4" sqref="G4"/>' +
'</sheetView>',
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 60, zoomScaleNormal: 80},
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 60, zoomScaleNormal: 80},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -33,7 +33,7 @@ var expectations = [
xml: '<sheetView workbookViewId="0" showRuler="0" showGridLines="0" showRowColHeaders="0">' +
'<selection activeCell="G4" sqref="G4"/>' +
'</sheetView>',
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: false, showGridLines: false, showRowColHeaders: false, zoomScale: 100, zoomScaleNormal: 100},
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: false, showGridLines: false, showRowColHeaders: false, zoomScale: 100, zoomScaleNormal: 100},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -43,7 +43,7 @@ var expectations = [
xml: '<sheetView workbookViewId="0" view="pageBreakPreview">' +
'<selection activeCell="G4" sqref="G4"/>' +
'</sheetView>',
parsedModel: {workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100, style: 'pageBreakPreview'},
parsedModel: {workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100, style: 'pageBreakPreview'},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -54,7 +54,7 @@ var expectations = [
'<pane xSplit="1234" ySplit="3456" topLeftCell="C3" activePane="bottomRight"/>' +
'<selection pane="bottomRight" activeCell="B1" sqref="B1"/>' +
'</sheetView>',
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},
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},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -65,7 +65,7 @@ var expectations = [
'<pane xSplit="1234" ySplit="3456" topLeftCell="C3"/>' +
'<selection activeCell="A1" sqref="A1"/>' +
'</sheetView>',
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},
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},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -76,7 +76,7 @@ var expectations = [
'<pane xSplit="2" ySplit="3" topLeftCell="C4" activePane="bottomRight" state="frozen"/>' +
'<selection pane="bottomRight" activeCell="D5" sqref="D5"/>' +
'</sheetView>',
parsedModel: {workbookViewId: 0, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
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},
tests: ['render', 'renderIn', 'parse']
},
{
Expand All @@ -96,10 +96,18 @@ var expectations = [
'</sheetView>' +
'</sheetViews>',
parsedModel: [
{workbookViewId: 0, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
{workbookViewId: 1, state: 'frozen', xSplit: 2, ySplit: 3, topLeftCell: 'C4', activeCell: 'D5', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100}
{workbookViewId: 0, rightToLeft: false, state: 'normal', activeCell: 'G4', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 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}
],
tests: ['render', 'renderIn', 'parse']
},
{
title: 'Right To Left',
create: () => new SheetViewXform(),
preparedModel: {rightToLeft: true},
xml: '<sheetView workbookViewId="0" rightToLeft="1"></sheetView>',
parsedModel: {workbookViewId: 0, rightToLeft: true, state: 'normal', showRuler: true, showGridLines: true, showRowColHeaders: true, zoomScale: 100, zoomScaleNormal: 100},
tests: ['render', 'renderIn', 'parse']
}
];

Expand Down
16 changes: 16 additions & 0 deletions spec/unit/xlsx/xform/style/alignment-xform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,22 @@ var expectations = [
get parsedModel() { return this.preparedModel; },
tests: ['render', 'renderIn', 'parse']
},
{
title: 'Reading Order [Left To Right]',
create: () => new AlignmentXform(),
preparedModel: { readingOrder: 'ltr' },
xml: '<alignment readingOrder="1"/>',
get parsedModel() { return this.preparedModel; },
tests: ['render', 'renderIn', 'parse']
},
{
title: 'Reading Order [Right To Left]',
create: () => new AlignmentXform(),
preparedModel: { readingOrder: 'rtl' },
xml: '<alignment readingOrder="2"/>',
get parsedModel() { return this.preparedModel; },
tests: ['render', 'renderIn', 'parse']
},
{
title: 'Vertical Text',
create: () => new AlignmentXform(),
Expand Down

0 comments on commit 4916ead

Please sign in to comment.