Created
January 18, 2015 23:52
-
-
Save bikisuke/fb6cce23116729f64581 to your computer and use it in GitHub Desktop.
gexcelapiを使ってExcelからJSONに変換するスクリプト。まだ使い物にならないレベルなので鵜呑みにしないように。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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