2013年10月8日火曜日

groovyとApache MetaModelでAccessテーブルに対してクエリーを実行する

groovyとApache MetaModelでAccessテーブルに対してクエリーを実行するには、以下のコードを実行します。
@Grab(group='org.eobjects.metamodel', module='MetaModel-full', version='3.4.5')
import org.eobjects.metamodel.*

dc = DataContextFactory.createAccessDataContext(new File("sales.mdb"))
// テーブルを指定
table = dc.getDefaultSchema().getTableByName("sales")
// 売上年月が2013年02月のデータを抽出
query = dc.query().from(table)
  .select("store_id", "sales")
  .where(table.getColumnByName("sales_month")).equals("201302").toQuery()
// Queryの表示
println query.toString()
// Queryの実行
ds = dc.executeQuery(query)
for(row in ds){
  // 店舗IDと売上を表示
  println row.getValue(0) + ":" + row.getValue(1)
}

クエリー対象のAccessテーブル

動作環境
groovy 2.1.7, JDK7 update40

2013年10月7日月曜日

groovyとApache MetaModelでCSVファイルに対してクエリーを実行する

groovyとApache MetaModelでCSVファイルに対してクエリーを実行するには、以下のコードを実行します。
@Grab(group='org.eobjects.metamodel', module='MetaModel-full', version='3.4.5')
import org.eobjects.metamodel.*

dc = DataContextFactory.createCsvDataContext(new File("customers.csv"))
// CSVだとテーブルが1つのみ存在する
table = dc.getDefaultSchema().getTable(0)
// 顧客ランクがgoldの顧客を抽出
query = dc.query().from(table)
  .select("customer_id", "customer_name")
  .where(table.getColumnByName("customer_rank")).equals("gold").toQuery()
// Queryの表示
println query.toString()
// Queryの実行
ds = dc.executeQuery(query)
for(row in ds){
  // 顧客IDと顧客名を表示
  println row.getValue(0) + ":" + row.getValue(1)
}

テスト用CSVファイル
customer_id,customer_name,customer_rank
12045,Brian Snow,none
23012,Andrew Clein,gold
11078,Bob Miller,none
67892,Matthew Brown,gold
1237,Elisabeth Roberts,platinum
動作環境
groovy 2.1.7, JDK7 update40

2013年10月6日日曜日

groovyとApache POIでPowerpointに角丸四角を描画する

groovyとApache POIでPowerpointに角丸四角を描画するには、以下のコードを実行します。
@Grab(group='org.apache.poi', module='poi-scratchpad', version='3.9')
import java.awt.*
import org.apache.poi.hslf.model.*
import org.apache.poi.hslf.usermodel.*
//import org.apache.poi.sl.usermodel.*

// プレゼンテーションを作成
presentation = new SlideShow()
// スライドを作成
slide = presentation.createSlide()

// 角丸四角を作成
as1 = new AutoShape(ShapeTypes.RoundRectangle)
as1.setAnchor(new Rectangle(100, 100, 300, 200))
// 塗りつぶし色
as1.setFillColor(new Color(0xcc, 0xdd, 0xff))
// 線の色
as1.setLineColor(new Color(0x77, 0x99, 0xdd))
// テキスト
as1.setText("サンプル")
slide.addShape(as1)

// 保存
presentation.write(new FileOutputStream("test1.ppt"))
出力ファイル
動作環境
groovy 2.1.7, JDK7 update40

2013年10月5日土曜日

groovyとApache POIでMicrosoft Officeのファイルのプロパティを表示する

xls等のMicrosoft Officeのファイルのプロパティを表示するには、以下のコードを実行します。
@Grab(group='org.apache.poi', module='poi', version='3.9')
import org.apache.poi.hpsf.*
import org.apache.poi.poifs.eventfilesystem.*

reader = new POIFSReader()
reader.registerListener(new POIFSReaderListener(){
  public void processPOIFSReaderEvent(POIFSReaderEvent event)
  {
    SummaryInformation si = null
    try
    {
      si = PropertySetFactory.create(event.getStream());
    }
    catch(Exception ex)
    {
      throw new RuntimeException(ex)
    }
    println "タイトル:${si.getTitle()}"
    println "作成者:${si.getAuthor()}"
    println "表題:${si.getSubject()}"
    println "キーワード:${si.getKeywords()}"
    println "コメント:${si.getComments()}"
  }
}, "\005SummaryInformation");
reader.read(new FileInputStream("test.xls"))

動作環境
groovy 2.1.7, JDK7 update40

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")
}