Search in sources :

Example 1 with SpreadsheetFeed

use of com.google.gdata.data.spreadsheet.SpreadsheetFeed in project OpenRefine by OpenRefine.

the class UploadCommand method uploadSpreadsheet.

private static String uploadSpreadsheet(final Project project, final Engine engine, final Properties params, String token, String name, List<Exception> exceptions) {
    Drive driveService = GDataExtension.getDriveService(token);
    final SpreadsheetService spreadsheetService = GDataExtension.getSpreadsheetService(token);
    try {
        File body = new File();
        body.setTitle(name);
        body.setDescription("Spreadsheet uploaded from OpenRefine project: " + name);
        body.setMimeType("application/vnd.google-apps.spreadsheet");
        File file = driveService.files().insert(body).execute();
        String fileID = file.getId();
        // Iterate through all spreadsheets to find one with our ID
        SpreadsheetEntry spreadsheetEntry2 = null;
        SpreadsheetFeed feed = spreadsheetService.getFeed(new URL(SPREADSHEET_FEED), SpreadsheetFeed.class);
        List<com.google.gdata.data.spreadsheet.SpreadsheetEntry> spreadsheets = feed.getEntries();
        for (com.google.gdata.data.spreadsheet.SpreadsheetEntry spreadsheet : spreadsheets) {
            if (spreadsheet.getId().endsWith(fileID)) {
                spreadsheetEntry2 = spreadsheet;
            }
        }
        // Bail if we didn't find our spreadsheet (shouldn't happen)
        if (spreadsheetEntry2 == null) {
            logger.error("Failed to find match for ID: " + fileID);
            return null;
        }
        int[] size = CustomizableTabularExporterUtilities.countColumnsRows(project, engine, params);
        URL worksheetFeedUrl = spreadsheetEntry2.getWorksheetFeedUrl();
        WorksheetEntry worksheetEntry = new WorksheetEntry(size[1], size[0]);
        worksheetEntry.setTitle(new PlainTextConstruct("Uploaded Data"));
        final WorksheetEntry worksheetEntry2 = spreadsheetService.insert(worksheetFeedUrl, worksheetEntry);
        spreadsheetEntry2.getDefaultWorksheet().delete();
        final SpreadsheetEntry spreadsheetEntry3 = spreadsheetEntry2;
        new Thread() {

            @Override
            public void run() {
                spreadsheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
                try {
                    uploadToCellFeed(project, engine, params, spreadsheetService, spreadsheetEntry3, worksheetEntry2);
                } catch (Exception e) {
                    logger.error("Error uploading data to Google Spreadsheets", e);
                }
            }
        }.start();
        return spreadsheetEntry2.getSpreadsheetLink().getHref();
    } catch (IOException | ServiceException e) {
        exceptions.add(e);
    }
    return null;
}
Also used : SpreadsheetService(com.google.gdata.client.spreadsheet.SpreadsheetService) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) IOException(java.io.IOException) URL(java.net.URL) WorksheetEntry(com.google.gdata.data.spreadsheet.WorksheetEntry) PlainTextConstruct(com.google.gdata.data.PlainTextConstruct) ServletException(javax.servlet.ServletException) ServiceException(com.google.gdata.util.ServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) ServiceException(com.google.gdata.util.ServiceException) SpreadsheetFeed(com.google.gdata.data.spreadsheet.SpreadsheetFeed) Drive(com.google.api.services.drive.Drive) File(com.google.api.services.drive.model.File)

Example 2 with SpreadsheetFeed

use of com.google.gdata.data.spreadsheet.SpreadsheetFeed in project OpenRefine by OpenRefine.

the class GDataImportingController method listSpreadsheets.

private void listSpreadsheets(DocsService service, JSONWriter writer) throws IOException, ServiceException, JSONException {
    URL metafeedUrl = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
    SpreadsheetFeed feed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
    for (SpreadsheetEntry entry : feed.getEntries()) {
        writer.object();
        writer.key("docId");
        writer.value(entry.getId());
        writer.key("docLink");
        writer.value(entry.getHtmlLink().getHref());
        writer.key("docSelfLink");
        writer.value(entry.getSelfLink().getHref());
        writer.key("title");
        writer.value(entry.getTitle().getPlainText());
        writer.key("type");
        writer.value("spreadsheet");
        DateTime updated = entry.getUpdated();
        if (updated != null) {
            writer.key("updated");
            writer.value(updated.toStringRfc822());
        }
        writer.key("authors");
        writer.array();
        for (Person person : entry.getAuthors()) {
            writer.value(person.getName());
        }
        writer.endArray();
        writer.endObject();
    }
}
Also used : SpreadsheetFeed(com.google.gdata.data.spreadsheet.SpreadsheetFeed) SpreadsheetEntry(com.google.gdata.data.spreadsheet.SpreadsheetEntry) Person(com.google.gdata.data.Person) URL(java.net.URL) DateTime(com.google.gdata.data.DateTime)

Aggregations

SpreadsheetEntry (com.google.gdata.data.spreadsheet.SpreadsheetEntry)2 SpreadsheetFeed (com.google.gdata.data.spreadsheet.SpreadsheetFeed)2 URL (java.net.URL)2 Drive (com.google.api.services.drive.Drive)1 File (com.google.api.services.drive.model.File)1 SpreadsheetService (com.google.gdata.client.spreadsheet.SpreadsheetService)1 DateTime (com.google.gdata.data.DateTime)1 Person (com.google.gdata.data.Person)1 PlainTextConstruct (com.google.gdata.data.PlainTextConstruct)1 WorksheetEntry (com.google.gdata.data.spreadsheet.WorksheetEntry)1 ServiceException (com.google.gdata.util.ServiceException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 ServletException (javax.servlet.ServletException)1