| 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 SYLKReader extends Reader { |
| 6 | 6 |
| 7 SYLKReader() : super() { } | 7 SYLKReader() : super() { } |
| 8 | 8 |
| 9 void loadSpreadsheet(Spreadsheet spreadsheet, List<String> sylk) { | 9 void loadSpreadsheet(Spreadsheet spreadsheet, List<String> sylk) { |
| 10 int row, col; | 10 int row, col; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 String part = parts[j]; | 37 String part = parts[j]; |
| 38 if (part.startsWith("Y")) { | 38 if (part.startsWith("Y")) { |
| 39 row = Math.parseInt(part.substring(1, part.length)); | 39 row = Math.parseInt(part.substring(1, part.length)); |
| 40 } else if (part.startsWith("X")) { | 40 } else if (part.startsWith("X")) { |
| 41 col = Math.parseInt(part.substring(1, part.length)); | 41 col = Math.parseInt(part.substring(1, part.length)); |
| 42 } if (part.startsWith("K")) { | 42 } if (part.startsWith("K")) { |
| 43 contents = StringUtils.stripQuotes(part.substring(1, part.length)); | 43 contents = StringUtils.stripQuotes(part.substring(1, part.length)); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 if (row == -1) { | 46 if (row == -1) { |
| 47 throw new RuntimeException("No row (Y) specified in ${sylk[i]}"); | 47 throw new RuntimeException("${i}: No row (Y) specified in ${sylk[i]}")
; |
| 48 } | 48 } |
| 49 if (col == -1) { | 49 if (col == -1) { |
| 50 throw new RuntimeException("No col (X) specified in ${sylk[i]}"); | 50 throw new RuntimeException("${i}: No col (X) specified in ${sylk[i]}")
; |
| 51 } | 51 } |
| 52 lastLocation = new CellLocation(spreadsheet, new RowCol(row, col)); | 52 lastLocation = new CellLocation(spreadsheet, new RowCol(row, col)); |
| 53 spreadsheet.execute(new SetCellContentsCommand(lastLocation, contents)); | 53 spreadsheet.execute(new SetCellContentsCommand(lastLocation, contents)); |
| 54 break; | 54 break; |
| 55 case "F": | 55 case "F": |
| 56 Cell lastCell = lastLocation.getCell(); | 56 Cell lastCell = lastLocation.getCell(); |
| 57 if (lastCell == null) { | 57 if (lastCell == null) { |
| 58 break; | 58 break; |
| 59 } | 59 } |
| 60 Style style = lastCell.style; | 60 Style style = lastCell.style; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 87 break; | 87 break; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 spreadsheet.setCellStyle(lastLocation.rowCol, style); | 91 spreadsheet.setCellStyle(lastLocation.rowCol, style); |
| 92 break; | 92 break; |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| 97 List<String> makeExample(String id) { | 97 void request(String name, void callback(String spreadsheet)) { |
| 98 if (id == "mortgage") { | 98 String url = '/spreadsheet/get?name=$name'; |
| 99 return _makeMortgageExample(); | 99 XMLHttpRequest req = new XMLHttpRequest(); |
| 100 } else if (id == "insertTest") { | 100 req.on.readyStateChange.add((Event event) { |
| 101 return _makeInsertTest(); | 101 if (req.readyState != XMLHttpRequest.DONE) { |
| 102 } else if (id == "collatz") { | 102 return; |
| 103 return _makeCollatzTest(); | 103 } |
| 104 } | |
| 105 } | |
| 106 | 104 |
| 107 List<String> _makeCollatzTest() { | 105 String text; |
| 108 List<String> sylk = new List<String>(); | 106 switch (true) { |
| 109 sylk.add('ID;P'); | 107 case req.status == 200 && req.responseText != null : |
| 110 sylk.add('B;X1;Y180'); | 108 text = req.responseText; |
| 111 sylk.add("C;X1;Y1;K871"); | 109 break; |
| 112 for (int row = 2; row < 180; row++) { | |
| 113 // x even ==> x / 2 | |
| 114 // x odd ==> 3 * x + 1 | |
| 115 // Note that (x - 2*trunc(x/2)) is 0 when x is even and 1 when x is odd | |
| 116 sylk.add("C;X1;Y${row};K=(R[-1]C-2*TRUNC(R[-1]C/2))*(3*R[-1]C+1)+" | |
| 117 + "(1-(R[-1]C-2*TRUNC(R[-1]C/2)))*(R[-1]C/2)"); | |
| 118 } | |
| 119 return sylk; | |
| 120 } | |
| 121 | 110 |
| 122 List<String> _makeInsertTest() { | 111 default: |
| 123 List<String> sylk = new List<String>(); | 112 text = '''ID;P |
| 113 B;X1;Y3 |
| 114 C;Y1;X1;K${req.status} |
| 115 C;Y2;X1;K"spreadsheet/get" |
| 116 C;Y3;X1;K"not found" |
| 117 '''; |
| 118 break; |
| 119 } |
| 124 | 120 |
| 125 sylk.add('ID;P'); | 121 try { |
| 126 sylk.add('B;X6;Y3'); | 122 callback(text); |
| 123 } catch (var e, var s) { |
| 124 print(e); |
| 125 } |
| 126 }); |
| 127 | 127 |
| 128 sylk.add('C;Y1;X1;K"InsertTest"'); | 128 req.open('GET', url, true); |
| 129 sylk.add('F;SD;FD0C'); | 129 req.send(); |
| 130 sylk.add('C;Y2;X1;K"Absolute"'); | |
| 131 sylk.add('F;SD;FD0C'); | |
| 132 sylk.add('C;Y3;X1;K"Relative"'); | |
| 133 sylk.add('F;SD;FD0C'); | |
| 134 sylk.add('C;Y1;X2;K"Fixed"'); | |
| 135 sylk.add('F;SD;FD0C'); | |
| 136 sylk.add('C;Y1;X3;K"NS to NS"'); | |
| 137 sylk.add('F;SD;FD0C'); | |
| 138 sylk.add('C;Y1;X4;K"NS to S"'); | |
| 139 sylk.add('F;SD;FD0C'); | |
| 140 sylk.add('C;Y1;X5;K"S to NS"'); | |
| 141 sylk.add('F;SD;FD0C'); | |
| 142 sylk.add('C;Y1;X6;K"S to S"'); | |
| 143 sylk.add('F;SD;FD0C'); | |
| 144 | |
| 145 sylk.add('C;Y2;X2;K1'); | |
| 146 sylk.add('F;P1;FD0C'); | |
| 147 sylk.add('C;Y3;X2;K2'); | |
| 148 sylk.add('F;P1;FD0C'); | |
| 149 | |
| 150 sylk.add('C;Y2;X3;K=(R2C2+10)'); | |
| 151 sylk.add('F;P1;FD0C'); | |
| 152 sylk.add('C;Y3;X3;K=(R[0]C[-1] + 10)'); | |
| 153 sylk.add('F;P1;FD0C'); | |
| 154 | |
| 155 sylk.add('C;Y2;X4;K=(R2C5-1000)'); | |
| 156 sylk.add('F;P1;FD0C'); | |
| 157 sylk.add('C;Y3;X4;K=(R[0]C[1] - 1000)'); | |
| 158 sylk.add('F;P1;FD0C'); | |
| 159 | |
| 160 sylk.add('C;Y2;X5;K=(R2C3+1100)'); | |
| 161 sylk.add('F;P1;FD0C'); | |
| 162 sylk.add('C;Y3;X5;K=(R[0]C[-2] + 1100)'); | |
| 163 sylk.add('F;P1;FD0C'); | |
| 164 | |
| 165 sylk.add('C;Y2;X6;K=(R2C5+10000)'); | |
| 166 sylk.add('F;P1;FD0C'); | |
| 167 sylk.add('C;Y3;X6;K=(R[0]C[-1] + 10000)'); | |
| 168 sylk.add('F;P1;FD0C'); | |
| 169 | |
| 170 sylk.add('C;Y4;X1;K"Sums Relative"'); | |
| 171 sylk.add('F;SD;FD0C'); | |
| 172 sylk.add('C;Y4;X2;K=SUM(R[-2]C[0]:R[-1]C[0])'); | |
| 173 sylk.add('F;P1;FD0C'); | |
| 174 sylk.add('C;Y4;X3;K=SUM(R[-2]C[0]:R[-1]C[0])'); | |
| 175 sylk.add('F;P1;FD0C'); | |
| 176 sylk.add('C;Y4;X4;K=SUM(R[-2]C[0]:R[-1]C[0])'); | |
| 177 sylk.add('F;P1;FD0C'); | |
| 178 sylk.add('C;Y4;X5;K=SUM(R[-2]C[0]:R[-1]C[0])'); | |
| 179 sylk.add('F;P1;FD0C'); | |
| 180 sylk.add('C;Y4;X6;K=SUM(R[-2]C[0]:R[-1]C[0])'); | |
| 181 sylk.add('F;P1;FD0C'); | |
| 182 | |
| 183 sylk.add('C;Y5;X1;K"Sums Absolute"'); | |
| 184 sylk.add('F;SD;FD0C'); | |
| 185 sylk.add('C;Y5;X2;K=SUM(B2:B3)'); | |
| 186 sylk.add('F;P1;FD0C'); | |
| 187 sylk.add('C;Y5;X3;K=SUM(C2:C3)'); | |
| 188 sylk.add('F;P1;FD0C'); | |
| 189 sylk.add('C;Y5;X4;K=SUM(D2:D3)'); | |
| 190 sylk.add('F;P1;FD0C'); | |
| 191 sylk.add('C;Y5;X5;K=SUM(E2:E3)'); | |
| 192 sylk.add('F;P1;FD0C'); | |
| 193 sylk.add('C;Y5;X6;K=SUM(F2:F3)'); | |
| 194 sylk.add('F;P1;FD0C'); | |
| 195 | |
| 196 return sylk; | |
| 197 } | |
| 198 | |
| 199 List<String> _makeMortgageExample() { | |
| 200 int years = 8; | |
| 201 int payments = years * 12; | |
| 202 List<String> sylk = new List<String>(); | |
| 203 sylk.add('ID;P'); | |
| 204 sylk.add('B;X4;Y${payments + 4}'); | |
| 205 sylk.add('C;Y1;X1;K"Loan Amount"'); | |
| 206 sylk.add('F;SD;FD0C'); | |
| 207 sylk.add('C;Y1;X2;K"Interest Rate"'); | |
| 208 sylk.add('F;SD;FD0C'); | |
| 209 sylk.add('C;Y1;X3;K"Years"'); | |
| 210 sylk.add('F;SD;FD0C'); | |
| 211 sylk.add('C;Y1;X4;K"\$/Month"'); | |
| 212 sylk.add('F;SD;FD0C'); | |
| 213 sylk.add('C;Y2;X1;K30000'); | |
| 214 sylk.add('F;P5;F\$2R'); | |
| 215 sylk.add('C;Y2;X2;K.05375'); | |
| 216 sylk.add('F;P8;F%3R'); | |
| 217 sylk.add('C;Y2;X3;K${years}'); | |
| 218 sylk.add('F;P2;FD0R'); | |
| 219 sylk.add('C;Y2;X4;K=ROUND((R2C1 * ((R2C2/12)/(1 - POWER((1 + (R2C2/12)), (-1
2 * R2C3))))), 2)'); | |
| 220 sylk.add('F;P5;F\$2R'); | |
| 221 sylk.add('C;Y3;X1;K"Payment #"'); | |
| 222 sylk.add('F;SD;FD0C'); | |
| 223 sylk.add('C;Y3;X2;K"Balance"'); | |
| 224 sylk.add('F;SD;FD0C'); | |
| 225 sylk.add('C;Y3;X3;K"Interest"'); | |
| 226 sylk.add('F;SD;FD0C'); | |
| 227 sylk.add('C;Y3;X4;K"Principal"'); | |
| 228 sylk.add('F;SD;FD0C'); | |
| 229 sylk.add('C;Y4;X1;K1'); | |
| 230 sylk.add('F;P2;FD0C'); | |
| 231 sylk.add('C;Y4;X2;K=R2C1'); | |
| 232 sylk.add('F;P5;F\$2R'); | |
| 233 sylk.add('C;Y4;X3;K=(RC[-1] * R2C2)/12'); | |
| 234 sylk.add('F;P5;F\$2R'); | |
| 235 sylk.add('C;Y4;X4;K=R2C4 - RC[-1]'); | |
| 236 sylk.add('F;P5;F\$2R'); | |
| 237 for (int j = 5; j < payments + 4; j++) { | |
| 238 sylk.add('C;Y${j};X1;K=R[-1]C + 1'); | |
| 239 sylk.add('F;P2;FD0C'); | |
| 240 sylk.add('C;Y${j};X2;K=R[-1]C - R[-1]C[2]'); | |
| 241 sylk.add('F;P5;F\$2R'); | |
| 242 sylk.add('C;Y${j};X3;K=(RC[-1] * R2C2)/12'); | |
| 243 sylk.add('F;P5;F\$2R'); | |
| 244 sylk.add('C;Y${j};X4;K=R2C4 - RC[-1]'); | |
| 245 sylk.add('F;P5;F\$2R'); | |
| 246 } | |
| 247 | |
| 248 int p3 = payments + 3; | |
| 249 int p4 = payments + 4; | |
| 250 sylk.add('C;Y${p4};X1;K"Totals"'); | |
| 251 sylk.add('F;SD;FD0C'); | |
| 252 sylk.add('C;Y${p4};X2;K=C${p4} + D${p4}'); | |
| 253 sylk.add('F;P5;F\$2R'); | |
| 254 sylk.add('C;Y${p4};X3;K=SUM(C4:C${p3})'); | |
| 255 sylk.add('F;P5;F\$2R'); | |
| 256 sylk.add('C;Y${p4};X4;K=SUM(D4:D${p3})'); | |
| 257 sylk.add('F;P5;F\$2R'); | |
| 258 return sylk; | |
| 259 } | 130 } |
| 260 } | 131 } |
| OLD | NEW |