Search in sources :

Example 1 with Sheet

use of com.google.api.services.sheets.v4.model.Sheet in project collect by opendatakit.

the class SheetsHelper method resizeSpreadSheet.

/**
 * Resize the column size of the provided sheetId in the google spreadsheet
 *
 * @param spreadsheetId unique id of the spreadsheet
 * @param sheetId       the index of the sheet which is to be resized
 * @param columnSize    the new column size of the sheet
 * @throws IllegalArgumentException Providing sheetId or column size less than zero throws this exception
 *                                  Sheet Id is basically index of the sheet starting from zero
 *                                  Similarly, the value for the column size can't be negative
 * @throws IOException              Throws this exception if the google spreadsheet cannot be fetched
 *                                  due to insufficient permissions or invalid sheet id
 */
public void resizeSpreadSheet(String spreadsheetId, int sheetId, int columnSize) throws IllegalArgumentException, IOException {
    if (sheetId < 0) {
        throw new IllegalArgumentException("Sheet Id should be greater than or equal to 0");
    }
    if (columnSize < 1) {
        throw new IllegalArgumentException("Column size should be greater than 0");
    }
    // create grid properties with the new column size
    GridProperties gridProperties = new GridProperties().setColumnCount(columnSize);
    // create sheet properties for the first sheet in the spreadsheet
    SheetProperties sheetProperties = new SheetProperties().setSheetId(sheetId).setGridProperties(gridProperties);
    // Updates properties of the sheet with the specified sheetId
    UpdateSheetPropertiesRequest updateSheetPropertyRequest = new UpdateSheetPropertiesRequest().setProperties(sheetProperties).setFields("gridProperties.columnCount");
    // generate request
    List<Request> requests = new ArrayList<>();
    requests.add(new Request().setUpdateSheetProperties(updateSheetPropertyRequest));
    // send the API request
    sheetsService.batchUpdate(spreadsheetId, requests);
}
Also used : UpdateSheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSheetPropertiesRequest) GridProperties(com.google.api.services.sheets.v4.model.GridProperties) SheetProperties(com.google.api.services.sheets.v4.model.SheetProperties) AddSheetRequest(com.google.api.services.sheets.v4.model.AddSheetRequest) BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) UpdateSpreadsheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest) Request(com.google.api.services.sheets.v4.model.Request) UpdateSheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSheetPropertiesRequest) ArrayList(java.util.ArrayList)

Example 2 with Sheet

use of com.google.api.services.sheets.v4.model.Sheet 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)

Aggregations

AddSheetRequest (com.google.api.services.sheets.v4.model.AddSheetRequest)1 BatchUpdateSpreadsheetRequest (com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest)1 GridProperties (com.google.api.services.sheets.v4.model.GridProperties)1 Request (com.google.api.services.sheets.v4.model.Request)1 Sheet (com.google.api.services.sheets.v4.model.Sheet)1 SheetProperties (com.google.api.services.sheets.v4.model.SheetProperties)1 Spreadsheet (com.google.api.services.sheets.v4.model.Spreadsheet)1 UpdateSheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSheetPropertiesRequest)1 UpdateSpreadsheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 SpreadsheetOperationException (org.teiid.translator.google.api.SpreadsheetOperationException)1 Column (org.teiid.translator.google.api.metadata.Column)1 SpreadsheetInfo (org.teiid.translator.google.api.metadata.SpreadsheetInfo)1 Worksheet (org.teiid.translator.google.api.metadata.Worksheet)1