OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 class SYLKReader extends Reader { | 5 class SYLKProducer { |
| 6 const SYLKProducer(); |
6 | 7 |
7 SYLKReader() : super() { } | 8 String listSamples() { |
| 9 return '''[ |
| 10 "empty", |
| 11 "mortgage", |
| 12 "insertTest", |
| 13 "collatz", |
| 14 ]'''; |
| 15 } |
8 | 16 |
9 void loadSpreadsheet(Spreadsheet spreadsheet, List<String> sylk) { | 17 String makeExample(String id) { |
10 int row, col; | |
11 String contents; | |
12 CellLocation lastLocation; | |
13 | 18 |
14 for (int i = 0; i < sylk.length; i++) { | 19 switch (id) { |
15 List<String> parts = StringUtils.split(sylk[i], StringUtils.SEMICOLON); | 20 case 'mortgage': |
16 String cmd = parts[0]; | 21 return Strings.join(_makeMortgageExample(), '\n'); |
17 switch (cmd) { | 22 |
18 case "ID": | 23 case 'insertTest': |
19 break; | 24 return Strings.join(_makeInsertTest(), '\n'); |
20 case "B": | 25 |
21 for (int j = 1; j < parts.length; j++) { | 26 case 'collatz': |
22 String part = parts[j]; | 27 return Strings.join(_makeCollatzTest(), '\n'); |
23 if (part.startsWith("X")) { | 28 |
24 col = Math.parseInt(part.substring(1, part.length)); | 29 default: |
25 spreadsheet.setColumnCount(col); | 30 return Strings.join(_makeEmpty(), '\n'); |
26 } else if (part.startsWith("Y")) { | |
27 row = Math.parseInt(part.substring(1, part.length)); | |
28 spreadsheet.setRowCount(row); | |
29 } | |
30 } | |
31 break; | |
32 case "C": | |
33 row = -1; | |
34 col = -1; | |
35 contents = ""; | |
36 for (int j = 1; j < parts.length; j++) { | |
37 String part = parts[j]; | |
38 if (part.startsWith("Y")) { | |
39 row = Math.parseInt(part.substring(1, part.length)); | |
40 } else if (part.startsWith("X")) { | |
41 col = Math.parseInt(part.substring(1, part.length)); | |
42 } if (part.startsWith("K")) { | |
43 contents = StringUtils.stripQuotes(part.substring(1, part.length)); | |
44 } | |
45 } | |
46 if (row == -1) { | |
47 throw new RuntimeException("No row (Y) specified in ${sylk[i]}"); | |
48 } | |
49 if (col == -1) { | |
50 throw new RuntimeException("No col (X) specified in ${sylk[i]}"); | |
51 } | |
52 lastLocation = new CellLocation(spreadsheet, new RowCol(row, col)); | |
53 spreadsheet.execute(new SetCellContentsCommand(lastLocation, contents)); | |
54 break; | |
55 case "F": | |
56 Cell lastCell = lastLocation.getCell(); | |
57 if (lastCell == null) { | |
58 break; | |
59 } | |
60 Style style = lastCell.style; | |
61 for (int j = 1; j < parts.length; j++) { | |
62 String part = parts[j]; | |
63 if (part.startsWith("P")) { | |
64 int formatIndex = Math.parseInt(part.substring(1, part.length)); | |
65 style = style.setNumericFormatByIndex(formatIndex); | |
66 } else if (part.startsWith("S")) { | |
67 switch (part[1]) { | |
68 case "I"[0]: | |
69 style = style.setTextFormatByIndex(Style.ITALIC); | |
70 break; | |
71 case "D"[0]: | |
72 style = style.setTextFormatByIndex(Style.BOLD); | |
73 break; | |
74 } | |
75 } else if (part.startsWith("F")) { | |
76 // F;...;F<format><decimal_places><alignment> | |
77 // Only handle alignment for now | |
78 switch (part[3]) { | |
79 case "L"[0]: | |
80 style = style.setTextAlignment(Style.LEFT); | |
81 break; | |
82 case "C"[0]: | |
83 style = style.setTextAlignment(Style.CENTER); | |
84 break; | |
85 case "R"[0]: | |
86 style = style.setTextAlignment(Style.RIGHT); | |
87 break; | |
88 } | |
89 } | |
90 } | |
91 spreadsheet.setCellStyle(lastLocation.rowCol, style); | |
92 break; | |
93 } | |
94 } | 31 } |
95 } | 32 } |
96 | 33 |
97 List<String> makeExample(String id) { | 34 List<String> _makeEmpty() { |
98 if (id == "mortgage") { | 35 List<String> sylk = new List<String>(); |
99 return _makeMortgageExample(); | 36 sylk.add('ID;P'); |
100 } else if (id == "insertTest") { | 37 return sylk; |
101 return _makeInsertTest(); | |
102 } else if (id == "collatz") { | |
103 return _makeCollatzTest(); | |
104 } | |
105 } | 38 } |
106 | 39 |
107 List<String> _makeCollatzTest() { | 40 List<String> _makeCollatzTest() { |
108 List<String> sylk = new List<String>(); | 41 List<String> sylk = new List<String>(); |
109 sylk.add('ID;P'); | 42 sylk.add('ID;P'); |
110 sylk.add('B;X1;Y180'); | 43 sylk.add('B;X1;Y180'); |
111 sylk.add("C;X1;Y1;K871"); | 44 sylk.add("C;X1;Y1;K871"); |
112 for (int row = 2; row < 180; row++) { | 45 for (int row = 2; row < 180; row++) { |
113 // x even ==> x / 2 | 46 // x even ==> x / 2 |
114 // x odd ==> 3 * x + 1 | 47 // x odd ==> 3 * x + 1 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 sylk.add('F;SD;FD0C'); | 183 sylk.add('F;SD;FD0C'); |
251 sylk.add('C;Y${p4};X2;K=C${p4} + D${p4}'); | 184 sylk.add('C;Y${p4};X2;K=C${p4} + D${p4}'); |
252 sylk.add('F;P5;F\$2R'); | 185 sylk.add('F;P5;F\$2R'); |
253 sylk.add('C;Y${p4};X3;K=SUM(C4:C${p3})'); | 186 sylk.add('C;Y${p4};X3;K=SUM(C4:C${p3})'); |
254 sylk.add('F;P5;F\$2R'); | 187 sylk.add('F;P5;F\$2R'); |
255 sylk.add('C;Y${p4};X4;K=SUM(D4:D${p3})'); | 188 sylk.add('C;Y${p4};X4;K=SUM(D4:D${p3})'); |
256 sylk.add('F;P5;F\$2R'); | 189 sylk.add('F;P5;F\$2R'); |
257 return sylk; | 190 return sylk; |
258 } | 191 } |
259 } | 192 } |
OLD | NEW |