use of org.teiid.translator.google.api.SpreadsheetOperationException 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);
}
use of org.teiid.translator.google.api.SpreadsheetOperationException 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);
}
}
use of org.teiid.translator.google.api.SpreadsheetOperationException in project teiid by teiid.
the class AbstractSpreadsheetExecution method checkHeaders.
void checkHeaders(String worksheetTitle) throws TranslatorException {
SpreadsheetInfo info = connection.getSpreadsheetInfo();
Worksheet worksheet = info.getWorksheetByName(worksheetTitle);
if (worksheet == null) {
// $NON-NLS-1$
throw new SpreadsheetOperationException(SpreadsheetExecutionFactory.UTIL.gs("missing_worksheet", worksheetTitle));
}
if (!worksheet.isHeaderEnabled()) {
// $NON-NLS-1$
throw new TranslatorException(SpreadsheetExecutionFactory.UTIL.gs("headers_required"));
}
}
Aggregations