logger.info("Creating new file.");
// Create "docs".SpreadsheetEntry
final String appId = ApiProxy.getCurrentEnvironment().getAppId();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
final String fileName = appId + "_" + sdf.format(new Date());
DocumentListEntry entry = new com.google.gdata.data.docs.SpreadsheetEntry();
entry.setTitle(new PlainTextConstruct(fileName));
DocumentListEntry newSpreadSheet =
cs.insert(new URL("https://docs.google.com/feeds/default/private/full/"), entry);
try {
// Re-get in "spreadsheet".SpreadsheetEntry
FeedURLFactory urlFactory = FeedURLFactory.getDefault();
SpreadsheetQuery spreadsheetQuery =
new SpreadsheetQuery(urlFactory.getSpreadsheetsFeedUrl());
spreadsheetQuery.setTitleQuery(fileName);
SpreadsheetFeed spreadsheetFeed = ss.query(spreadsheetQuery, SpreadsheetFeed.class);
SpreadsheetEntry spreadsheetEntry = spreadsheetFeed.getEntries().get(0);
// Modify a default worksheet
URL worksheetFeedUrl = spreadsheetEntry.getWorksheetFeedUrl();
WorksheetFeed worksheetFeed = ss.getFeed(worksheetFeedUrl, WorksheetFeed.class);
WorksheetEntry defaultWorksheet = worksheetFeed.getEntries().get(0);
defaultWorksheet.setTitle(new PlainTextConstruct(targetKinds.get(0)));
defaultWorksheet.setRowCount(2);
defaultWorksheet.setColCount(1);
defaultWorksheet.update();
logger.info("Worksheet:" + targetKinds.get(0) + "is created.");
// Add other Worksheets
for (int i = 1; i < targetKinds.size(); i++) {
WorksheetEntry newWorksheet = new WorksheetEntry();
newWorksheet.setTitle(new PlainTextConstruct(targetKinds.get(i)));
newWorksheet.setRowCount(2);
newWorksheet.setColCount(1);
ss.insert(worksheetFeedUrl, newWorksheet);
logger.info("Worksheet:" + targetKinds.get(i) + "is created.");
}
logger.info("Created new file:" + fileName);
return spreadsheetEntry;
} catch (Exception e) {
if (newSpreadSheet != null) {
try {
newSpreadSheet.delete();
} catch (Exception e2) {
logger.warning(e2.getMessage());
}
}
throw e;