Search in sources :

Example 1 with Spreadsheet

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

the class GoogleSheetsIntegrationTest method whenCreateSpreadSheet_thenIdOk.

@Test
public void whenCreateSpreadSheet_thenIdOk() throws IOException {
    Spreadsheet spreadSheet = new Spreadsheet().setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet"));
    Spreadsheet result = sheetsService.spreadsheets().create(spreadSheet).execute();
    assertThat(result.getSpreadsheetId()).isNotNull();
}
Also used : Spreadsheet(com.google.api.services.sheets.v4.model.Spreadsheet) SpreadsheetProperties(com.google.api.services.sheets.v4.model.SpreadsheetProperties) Test(org.junit.Test)

Example 2 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet in project jbpm-work-items by kiegroup.

the class ReadSheetValuesWorkitemHandler method executeWorkItem.

public void executeWorkItem(WorkItem workItem, WorkItemManager workItemManager) {
    Map<String, Object> results = new HashMap<String, Object>();
    String paramSheetId = (String) workItem.getParameter("SheetId");
    // to learn google spreadsheet ranges go to
    // https://developers.google.com/sheets/api/guides/concepts
    String paramRange = (String) workItem.getParameter("Range");
    try {
        Sheets service = auth.getSheetsService(appName, clientSecret);
        ValueRange sheetResponse = service.spreadsheets().values().get(paramSheetId, paramRange).execute();
        List<List<Object>> values = sheetResponse.getValues();
        results.put(RESULTS_VALUES, values);
    } catch (Exception e) {
        handleException(e);
    }
    workItemManager.completeWorkItem(workItem.getId(), results);
}
Also used : ValueRange(com.google.api.services.sheets.v4.model.ValueRange) HashMap(java.util.HashMap) List(java.util.List) Sheets(com.google.api.services.sheets.v4.Sheets)

Example 3 with Spreadsheet

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

the class SheetsHelper method getSpreadsheet.

/**
 * Checks whether the selected google account has sufficient permissions
 * to modify the given spreadsheetId. If yes, then returns complete spreadsheet
 * otherwise throws exception
 */
public Spreadsheet getSpreadsheet(String spreadsheetId) throws IOException {
    /*
         * Read permission check
         *
         * To check read permissions, we are trying to fetch the complete spreadsheet using the
         * given spreadsheet id
         */
    // fetching the google spreadsheet
    Spreadsheet spreadsheet = sheetsService.getSpreadsheet(spreadsheetId);
    String spreadsheetFileName = spreadsheet.getProperties().getTitle();
    /*
         * Write permission check
         *
         * To check write permissions, we are trying to overwrite the name of the spreadsheet with
         * the same name
         *
         * Todo 22/3/17 Find a better way to check the write permissions
         */
    // creating a request to update name of spreadsheet
    SpreadsheetProperties sheetProperties = new SpreadsheetProperties().setTitle(spreadsheetFileName);
    List<Request> requests = new ArrayList<>();
    requests.add(new Request().setUpdateSpreadsheetProperties(new UpdateSpreadsheetPropertiesRequest().setProperties(sheetProperties).setFields("title")));
    // updating the spreadsheet with the given id
    sheetsService.batchUpdate(spreadsheetId, requests);
    return spreadsheet;
}
Also used : Spreadsheet(com.google.api.services.sheets.v4.model.Spreadsheet) SpreadsheetProperties(com.google.api.services.sheets.v4.model.SpreadsheetProperties) UpdateSpreadsheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest) 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 4 with Spreadsheet

use of com.google.api.services.sheets.v4.model.Spreadsheet 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 5 with Spreadsheet

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

the class SheetsHelperTest method getSpreadsheetTest.

@Test
public void getSpreadsheetTest() throws IOException {
    Spreadsheet mockedSpreadsheet = mock(Spreadsheet.class);
    SpreadsheetProperties mockedProperties = mock(SpreadsheetProperties.class);
    doReturn("sheet_title").when(mockedProperties).getTitle();
    doReturn(mockedProperties).when(mockedSpreadsheet).getProperties();
    doReturn(mockedSpreadsheet).when(sheetsService).getSpreadsheet("spreadsheet_id");
    Spreadsheet spreadsheet = sheetsHelper.getSpreadsheet("spreadsheet_id");
    assertEquals(mockedSpreadsheet, spreadsheet);
    assertBatchUpdateCalled(1);
}
Also used : Spreadsheet(com.google.api.services.sheets.v4.model.Spreadsheet) SpreadsheetProperties(com.google.api.services.sheets.v4.model.SpreadsheetProperties) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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