Search in sources :

Example 6 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet in project repairnator by Spirals-Team.

the class GoogleSpreadSheetFactory method insertData.

public static void insertData(List<List<Object>> dataRows, Sheets sheets, String range, Logger logger) {
    ValueRange valueRange = new ValueRange();
    valueRange.setValues(dataRows);
    try {
        AppendValuesResponse response = sheets.spreadsheets().values().append(GoogleSpreadSheetFactory.getSpreadsheetID(), range, valueRange).setInsertDataOption("INSERT_ROWS").setValueInputOption("USER_ENTERED").execute();
        if (response != null && response.getUpdates().getUpdatedCells() > 0) {
            logger.debug("Data have been inserted in Google Spreadsheet.");
        }
    } catch (IOException e) {
        logger.error("An error occurred while inserting data in Google Spreadsheet.", e);
    }
}
Also used : ValueRange(com.google.api.services.sheets.v4.model.ValueRange) AppendValuesResponse(com.google.api.services.sheets.v4.model.AppendValuesResponse) IOException(java.io.IOException)

Example 7 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet in project teiid by teiid.

the class SheetsAPI method insert.

/**
 * Insert row into spreadsheet
 * @param spreadsheetKey  key that identifies spreadsheet
 * @param pairs  key that identifies worksheet
 * @param worksheet name - value pair that should be inserted into spreadsheet
 * @return 1 if the row is successfully inserted
 */
public UpdateResult insert(String spreadsheetId, Map<String, Object> pairs, Worksheet worksheet) {
    ValueRange content = new ValueRange();
    List<Object> row = new ArrayList<>();
    for (String label : worksheet.getColumns().keySet()) {
        Object value = pairs.get(label);
        if (value != null) {
            if (value instanceof String) {
                // $NON-NLS-1$
                value = "'" + value;
            } else if (!(value instanceof Boolean || value instanceof Double)) {
                value = value.toString();
            }
        // else directly supported
        }
        row.add(value);
    }
    content.setValues(Arrays.asList(row));
    try {
        service.spreadsheets().values().append(spreadsheetId, worksheet.getName(), content).setValueInputOption(// $NON-NLS-1$ -- TODO: this could be configurable
        "USER_ENTERED").execute();
    } catch (IOException e) {
        throw new SpreadsheetOperationException("Error inserting spreadsheet row", e);
    }
    return new UpdateResult(1, 1);
}
Also used : ValueRange(com.google.api.services.sheets.v4.model.ValueRange) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UpdateResult(org.teiid.translator.google.api.result.UpdateResult)

Example 8 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet in project teiid by teiid.

the class SpreadsheetMetadataExtractor method extractMetadata.

public SpreadsheetInfo extractMetadata(String spreadsheetId) {
    try {
        Spreadsheet spreadsheet = sheetsAPI.getSpreadsheet(spreadsheetId);
        SpreadsheetInfo metadata = new SpreadsheetInfo(spreadsheet.getProperties().getTitle());
        metadata.setSpreadsheetKey(spreadsheet.getSpreadsheetId());
        for (Sheet sheet : spreadsheet.getSheets()) {
            String title = sheet.getProperties().getTitle();
            Worksheet worksheet = metadata.createWorksheet(title);
            worksheet.setId(sheet.getProperties().getSheetId().toString());
            List<Column> cols = dataProtocolAPI.getMetadata(spreadsheet.getSpreadsheetId(), title);
            if (!cols.isEmpty()) {
                if (cols.get(0).getLabel() != null) {
                    worksheet.setHeaderEnabled(true);
                }
            }
            for (Column c : cols) {
                worksheet.addColumn(c.getLabel() != null ? c.getLabel() : c.getAlphaName(), c);
            }
        }
        return metadata;
    } catch (IOException ex) {
        throw new SpreadsheetOperationException(SpreadsheetManagedConnectionFactory.UTIL.gs("metadata_error"), // $NON-NLS-1$
        ex);
    }
}
Also used : Spreadsheet(com.google.api.services.sheets.v4.model.Spreadsheet) SpreadsheetInfo(org.teiid.translator.google.api.metadata.SpreadsheetInfo) Column(org.teiid.translator.google.api.metadata.Column) SpreadsheetOperationException(org.teiid.translator.google.api.SpreadsheetOperationException) Worksheet(org.teiid.translator.google.api.metadata.Worksheet) IOException(java.io.IOException) Sheet(com.google.api.services.sheets.v4.model.Sheet)

Example 9 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet in project selenium_java by sergueik.

the class Quickstart method main.

public static void main(String[] args) throws IOException {
    // Build a new authorized API client service.
    Sheets service = getSheetsService();
    // Prints the names and majors of students in a sample spreadsheet:
    // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
    String spreadsheetId = "1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
    String range = "Class Data!A2:E";
    ValueRange response = service.spreadsheets().values().get(spreadsheetId, range).execute();
    List<List<Object>> values = response.getValues();
    if (values == null || values.size() == 0) {
        System.out.println("No data found.");
    } else {
        System.out.println("Name, Major");
        for (List row : values) {
            // Print columns A and E, which correspond to indices 0 and 4.
            System.out.printf("%s, %s\n", row.get(0), row.get(4));
        }
    }
}
Also used : List(java.util.List) Sheets(com.google.api.services.sheets.v4.Sheets)

Aggregations

Spreadsheet (com.google.api.services.sheets.v4.model.Spreadsheet)4 SpreadsheetProperties (com.google.api.services.sheets.v4.model.SpreadsheetProperties)3 ValueRange (com.google.api.services.sheets.v4.model.ValueRange)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Sheets (com.google.api.services.sheets.v4.Sheets)2 AddSheetRequest (com.google.api.services.sheets.v4.model.AddSheetRequest)2 BatchUpdateSpreadsheetRequest (com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest)2 Request (com.google.api.services.sheets.v4.model.Request)2 UpdateSheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSheetPropertiesRequest)2 UpdateSpreadsheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest)2 List (java.util.List)2 Test (org.junit.Test)2 SpreadsheetOperationException (org.teiid.translator.google.api.SpreadsheetOperationException)2 AppendValuesResponse (com.google.api.services.sheets.v4.model.AppendValuesResponse)1 GridProperties (com.google.api.services.sheets.v4.model.GridProperties)1 Sheet (com.google.api.services.sheets.v4.model.Sheet)1 SheetProperties (com.google.api.services.sheets.v4.model.SheetProperties)1 HashMap (java.util.HashMap)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1