-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostal-codes-extract.groovy
executable file
·92 lines (73 loc) · 2.47 KB
/
postal-codes-extract.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env groovy
import org.apache.poi.hssf.usermodel.HSSFWorkbook
@Grapes(
@Grab('org.apache.poi:poi:3.15-beta1')
)
import org.apache.poi.ss.usermodel.*
class PostalAddress {
String type
String name
String numberOrBuilding
String postalCode
String sector
String postOffice
String county
String city
@Override
String toString() {
return new StringBuilder()
.append(type ?: "").append(" ")
.append(name ?: "").append(" ")
.append(numberOrBuilding ?: "").append(" ")
.append(city ?: "").append(", ")
.append(sector ?: county ?: "")
.append(", ")
.append(postalCode ?: "").append(" ")
.append(postOffice ?: "")
.toString()
}
static def cellExtract(Row row) {
return { int cell -> row?.getCell(cell)?.getStringCellValue() };
}
static PostalAddress forBucharest(Row row) {
def stringValue = cellExtract(row)
return new PostalAddress(
type: stringValue(0),
name: stringValue(1),
numberOrBuilding: stringValue(2),
postalCode: stringValue(3),
sector: row?.getCell(4).getNumericCellValue(),
postOffice: stringValue(5),
city: 'București'
)
}
static PostalAddress forCity(Row row) {
def stringValue = cellExtract(row)
return new PostalAddress(
county: stringValue(0),
city: stringValue(1),
type: stringValue(2),
name: stringValue(3),
numberOrBuilding: stringValue(4),
postalCode: stringValue(5),
)
}
static PostalAddress forTown(Row row) {
def stringValue = cellExtract(row)
return new PostalAddress(
county: stringValue(0),
city: stringValue(1),
postalCode: stringValue(3),
)
}
}
def file = args[0];
def templateName = args.length == 2 ? args[1] : 'address.tpl'
InputStream inp = new FileInputStream(file);
Workbook wb = new HSSFWorkbook(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
def address = PostalAddress.forBucharest(row)
def template = new File(templateName).getText('UTF-8')
def engine = new groovy.text.SimpleTemplateEngine().createTemplate(template)
println("Adresa este: ${engine.make([address: address])}")