2013年9月29日日曜日

groovyとApache POIを使用して簡単な図形を描画する

groovyとApache POIを使用して、Excelシートに四角、線、楕円を描画するには以下のコードを実行します。
@Grab(group='org.apache.poi', module='poi', version='3.9')
import org.apache.poi.hssf.usermodel.*;

workbook = new HSSFWorkbook()
sheet = workbook.createSheet("sheet1")

HSSFPatriarch patriarch = sheet.createDrawingPatriarch()

// 四角形を描画
ca1 = new HSSFClientAnchor( 0, 0, 0, 0,  
  /* top-left */ (short)1, 2,  /* right-bottom */ (short)3, 4 )
ca1.setAnchorType(ca1.MOVE_DONT_RESIZE)
shape1 = patriarch.createSimpleShape(ca1)
shape1.setShapeType(HSSFSimpleShape.OBJECT_TYPE_RECTANGLE)
// 線の色を設定
shape1.setLineStyleColor(0x77, 0x99, 0xdd)
// 塗りつぶし色を設定
shape1.setFillColor(0xcc, 0xdd, 0xff)

// 線を描画
ca2 = new HSSFClientAnchor( 0, 0, 0, 0, (short) 1, 5, (short) 3, 5 )
ca2.setAnchorType(ca2.MOVE_DONT_RESIZE)
shape2 = patriarch.createSimpleShape(ca2)
shape2.setShapeType(HSSFSimpleShape.OBJECT_TYPE_LINE)
// ラインスタイルを点線に設定
shape2.setLineStyle(HSSFShape.LINESTYLE_DOTSYS)

// 楕円を描画
ca3 = new HSSFClientAnchor( 0, 0, 0, 0, (short) 1, 6, (short) 3, 8 )
ca3.setAnchorType(ca3.MOVE_DONT_RESIZE)
shape3 = patriarch.createSimpleShape(ca3)
shape3.setShapeType(HSSFSimpleShape.OBJECT_TYPE_OVAL)
// 線の太さを設定 12700=1Pt
shape3.setLineWidth(12700*3)

workbook.write(new FileOutputStream("test.xls"))
出力excelファイル

動作環境
groovy 2.1.7, JDK7 update40

2013年9月20日金曜日

gitoriousのリポジトリ情報を一覧表示する

gitoriousにはリポジトリ情報を取得できるAPIがあるので、jsoupを使用して以下のようなコードで一覧表示できます。
@Grab(group='org.jsoup', module='jsoup', version='1.7.2')
import org.jsoup.*
import org.jsoup.select.*

gitorious = "<your-gitorious-server>"
doc = Jsoup.connect("http://${gitorious}/projects.xml").get()

for(repo in Selector.select("repository", doc)){
  println "-----------"
  println "name:" + repo.getElementsByTag("name").eq(0).text()
  println "owner:" + repo.getElementsByTag("owner").eq(0).text()
  println "clone url:" + repo.getElementsByTag("clone_url").eq(0).text()
}
関連項目
Gitorious - API - Open wiki - Gitorious
http://gitorious.org/gitorious/pages/Api

2013年9月15日日曜日

groovyでevernoteのENEXエクスポートファイルからタイトル・本文・作成日時をテキストに変換する

Evernoteでは、ENEX形式ファイルでノートをエクスポートできます。ENEX形式のファイルからノートのタイトル・本文・作成日時をテキストに変換するには、以下のコードを実行します。
@Grab(group='org.jsoup', module='jsoup', version='1.7.2')
import org.jsoup.*
import org.jsoup.select.*

doc = Jsoup.parse(new File(args[0]).getText("UTF-8"))

for(note in Selector.select("note", doc)){
  println "-----------"
  println "title:" + note.getElementsByTag("title").eq(0).text()
  content = Jsoup.parse(note.getElementsByTag("content").eq(0).text())
  println "content:" + content.text()
  dt = Date.parse("yyyyMMdd'T'HHmmss", note.getElementsByTag("created").eq(0).text())
  println "created:" + dt.format("yyyy/MM/dd HH:mm:ss")
}