Skip to content

Instantly share code, notes, and snippets.

@bikisuke
Created January 18, 2015 23:52
Show Gist options
  • Save bikisuke/fb6cce23116729f64581 to your computer and use it in GitHub Desktop.
Save bikisuke/fb6cce23116729f64581 to your computer and use it in GitHub Desktop.
gexcelapiを使ってExcelからJSONに変換するスクリプト。まだ使い物にならないレベルなので鵜呑みにしないように。
package org.jggug.bikisuke.excel2json
import groovy.json.*
import org.jggug.kobo.gexcelapi.GExcel
class Main {
static types
static fields
static data
static void main(String[] args) {
def sheet = GExcel.open('C:/sandbox/tool/input.xlsx')[1]
def target = sheet.B2_J7
types = target[0]
fields = target[1]
data = sheet.B4_J7
def json = new JsonBuilder(parse([:]))
println StringEscapeUtils.unescapeJava(json.toPrettyString())
new File('json-generated.txt').setText(StringEscapeUtils.unescapeJava(json.toPrettyString()), 'UTF-8')
}
static def parse(root) {
while(types.size()) {
println types
println fields
def type = types.first()
def field = fields.first()
def value = data[0].first()
types -= type
fields -= field
data[0] -= value
if(type.toString()) {
root."$type" = [:]
root."$type"."$field" = "$value"
parse(root."$type")
} else {
root."$field" = "$value"
}
}
root
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment