2010年7月31日土曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートを更新する

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートを更新するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*
import com.google.api.client.http.*

// エントリ
public class Entry
{
@Key("@gd:etag")
String etag
@Key
String title
@Key
String id
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前のスプレッドシートを更新する
docname = "さんぷる1"
requrl = "https://docs.google.com/feeds/default/private/full/-/spreadsheet" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前のスプレッドシートが見つかったので更新する
println item.title
request = transport.buildPutRequest()
request.url = "https://docs.google.com/feeds/default/media/" +
"${item.id.substring(item.id.indexOf("spreadsheet%3A"))}"
request.headers.put("If-Match", item.etag)
fn = "さんぷる1rev2.xls"
gh = new GoogleHeaders()
gh.setSlug(request.headers, fn)
content = new InputStreamContent()
content.fileInput = new File(fn)
content.type = "application/vnd.ms-excel"
request.content = content
request.execute().parseAsString()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月30日金曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のプレゼンテーションをPDFファイルにエクスポートする

groovyとgdata-java-clientでGoogle Documents内の指定した名前のプレゼンテーションをPDFファイルにエクスポートするには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key
String title
@Key
String id
@Key("gd:resourceId")
String resourceId
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = "youraccount@gmail.com"
cl.password = 'yourpassword'
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前のプレゼンテーションをPDFファイルにエクスポートする
docname = "サンプルプレゼンテーション1"
requrl = "https://docs.google.com/feeds/default/private/full/-/document" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前の文書が見つかったのでエクスポート
println item.title

// pdfファイルをダウンロード
request = transport.buildGetRequest()
request.url= "https://docs.google.com" +
"/present/export" +
"?id=${item.resourceId.substring(item.resourceId.indexOf(":")+1)}" +
"&format=pdf"
is = request.execute().getContent()

bos = new BufferedOutputStream(
new FileOutputStream("ダウンロードした${item.title}.pdf"))
buf = new byte[1024]
while((sz = is.read(buf, 0, 1024)) != -1){
bos.write(buf, 0, sz)
}
bos.flush()
bos.close()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月29日木曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前の文書をWordファイルにエクスポートする

groovyとgdata-java-clientでGoogle Documents内の指定した名前の文書をWordファイルにエクスポートするには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key
String title
@Key
String id
@Key("gd:resourceId")
String resourceId
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = "youraccount@gmail.com"
cl.password = 'yourpassword'
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前の文書をwordファイル(doc)にエクスポートする
docname = "サンプル文書1"
requrl = "https://docs.google.com/feeds/default/private/full/-/document" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前の文書が見つかったのでエクスポート
println item.title

// docファイルをダウンロード
request = transport.buildGetRequest()
request.url= "https://docs.google.com" +
"/document/export" +
"?id=${item.resourceId.substring(item.resourceId.indexOf(":")+1)}" +
"&format=doc"
is = request.execute().getContent()

bos = new BufferedOutputStream(
new FileOutputStream("ダウンロードした${item.title}.doc"))
buf = new byte[1024]
while((sz = is.read(buf, 0, 1024)) != -1){
bos.write(buf, 0, sz)
}
bos.flush()
bos.close()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月28日水曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをExcelファイルにエクスポートする

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをExcelファイルにエクスポートするには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.data.spreadsheet.v3.*
import com.google.api.data.spreadsheet.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key
String title
@Key
String id
@Key("gd:resourceId")
String resourceId
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = "youraccount@gmail.com"
cl.password = 'yourpassword'
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前のスプレッドシートをexcelファイル(xls)にエクスポートする
docname = "さんぷる1"
requrl = "https://docs.google.com/feeds/default/private/full/-/spreadsheet" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前のスプレッドシートが見つかったのでエクスポート
println item.title

// Parser設定
transport2 = new GoogleTransport()
transport2.applicationName = "yourcorp-yourapp-1.0"
transport2.setVersionHeader(GoogleSpreadsheets.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleSpreadsheetsAtom.NAMESPACE_DICTIONARY
transport2.addParser(ap)

// 認証
cl2 = new ClientLogin()
cl2.authTokenType = GoogleSpreadsheets.AUTH_TOKEN_TYPE
cl2.username = "youraccount@gmail.com"
cl2.password = 'yourpassword'
cl2.authenticate().setAuthorizationHeader(transport2)

// xlsファイルをダウンロード
request = transport2.buildGetRequest()
request.url= "https://spreadsheets.google.com" +
"/feeds/download/spreadsheets/Export" +
"?key=${item.resourceId}&exportFormat=xls"
is = request.execute().getContent()

bos = new BufferedOutputStream(
new FileOutputStream("ダウンロードした${item.title}.xls"))
buf = new byte[1024]
while((sz = is.read(buf, 0, 1024)) != -1){
bos.write(buf, 0, sz)
}
bos.flush()
bos.close()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月27日火曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをフォルダに移動する

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをフォルダに移動するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key
String title
@Key
String id
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}


// フォルダ配置エントリ
public class EntryForMoving
{
@Key
String id
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = "youraccount@gmail.com"
cl.password = 'yourpassword'
cl.authenticate().setAuthorizationHeader(transport)

// 移動先のフォルダ
folderId = "https://docs.google.com/feeds/default/private/full/folder%3A0B6fa4uztHBXEMjFjYzBkZjgtZTBmNC00NDg3LWI5ZjAtMWEwYTNjZDA5OWJh/contents"
// 指定した名前のスプレッドシートをフォルダに配置する
docname = "さんぷる1"
requrl = "https://docs.google.com/feeds/default/private/full/-/spreadsheet" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前のスプレッドシートが見つかったのでフォルダに配置
println item.title
request = transport.buildPostRequest()
request.url = folderId
content = new AtomContent()
content.entry = new EntryForMoving()
content.entry.id = item.id
content.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
request.content = content;
request.execute().parseAsString()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月26日月曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートを削除する

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートを削除するには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key("@gd:etag")
String etag
@Key
String title
@Key
String id
@Key("gd:resourceId")
String resourceId
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = "youraccount@gmail.com"
cl.password = "yourpassword"
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前のスプレッドシートを削除する
docname = "さんぷる1"
requrl = "https://docs.google.com/feeds/default/private/full/-/spreadsheet" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前のスプレッドシートが見つかったので削除する
request = transport.buildDeleteRequest()
request.url = "https://docs.google.com/feeds/default/private/full/" +
item.resourceId + "?delete=true";
request.headers.put("If-Match", item.etag)
request.execute().parseAsString()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/

2010年7月25日日曜日

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをゴミ箱にいれる

groovyとgdata-java-clientでGoogle Documents内の指定した名前のスプレッドシートをゴミ箱にいれるには、以下のコードを実行します。

import com.google.api.client.googleapis.*
import com.google.api.client.googleapis.auth.clientlogin.*
import com.google.api.data.docs.v3.*
import com.google.api.data.docs.v3.atom.*
import com.google.api.client.xml.atom.*
import com.google.api.client.util.*

// エントリ
public class Entry
{
@Key("@gd:etag")
String etag
@Key
String title
@Key
String id
@Key("gd:resourceId")
String resourceId
}

// リンク
public class Link
{
@Key("@rel")
String rel
@Key("@type")
String type
@Key("@href")
String href
}

// フィード
public class Feed
{
@Key
String title
@Key
List<Link> link
@Key
List<Entry> entry
}

// Parser設定
GoogleTransport transport = new GoogleTransport()
transport.applicationName = "yourcorp-yourapp-1.0"
transport.setVersionHeader(GoogleDocumentsList.VERSION)
ap = new AtomParser()
ap.namespaceDictionary = GoogleDocumentsListAtom.NAMESPACE_DICTIONARY
transport.addParser(ap)

username = "youraccount@gmail.com"
password = 'yourpassword'

// 認証
cl = new ClientLogin()
cl.authTokenType = GoogleDocumentsList.AUTH_TOKEN_TYPE
cl.username = username
cl.password = password
cl.authenticate().setAuthorizationHeader(transport)

// 指定した名前のスプレッドシートをゴミ箱にいれる
docname = "さんぷる1"
requrl = "https://docs.google.com/feeds/default/private/full/-/spreadsheet" +
"?title=${URLEncoder.encode(docname, 'UTF-8')}&title-exact=true"
while(requrl != null){
request = transport.buildGetRequest()
request.url = requrl
feed = request.execute().parseAs(Feed.class)
println "feed title:" + feed.title
for(item in feed.entry){
// 指定した名前のスプレッドシートが見つかったのでゴミ箱に入れる
request = transport.buildDeleteRequest()
request.url = "https://docs.google.com/feeds/default/private/full/" +
item.resourceId;
request.headers.put("If-Match", item.etag)
request.execute().parseAsString()
}
// 100個以上の文書の場合
requrl = null
for(link in feed.link){
if( link.rel == "next" ){
requrl = link.href
}
}
}


動作環境
groovy 1.7.2, JDK6 Update20, gdata-java-2.2.1-alpha

関連情報
gdata-java-client
http://code.google.com/p/gdata-java-client/