Search in sources :

Example 1 with Request

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

the class SheetsHelper method addSheet.

public void addSheet(String spreadsheetId, String sheetName) throws IOException {
    AddSheetRequest addSheetRequest = new AddSheetRequest();
    addSheetRequest.setProperties(new SheetProperties().setTitle(sheetName));
    Request request = new Request();
    request.setAddSheet(addSheetRequest);
    sheetsService.batchUpdate(spreadsheetId, Collections.singletonList(request));
}
Also used : AddSheetRequest(com.google.api.services.sheets.v4.model.AddSheetRequest) 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)

Example 2 with Request

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

the class GoogleSheetsIntegrationTest method whenUpdateSpreadSheetTitle_thenOk.

@Test
public void whenUpdateSpreadSheetTitle_thenOk() throws IOException {
    UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest().setFields("*").setProperties(new SpreadsheetProperties().setTitle("Expenses"));
    CopyPasteRequest copyRequest = new CopyPasteRequest().setSource(new GridRange().setSheetId(0).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)).setDestination(new GridRange().setSheetId(1).setStartColumnIndex(0).setEndColumnIndex(2).setStartRowIndex(0).setEndRowIndex(1)).setPasteType("PASTE_VALUES");
    List<Request> requests = new ArrayList<>();
    requests.add(new Request().setCopyPaste(copyRequest));
    requests.add(new Request().setUpdateSpreadsheetProperties(updateRequest));
    BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest().setRequests(requests);
    sheetsService.spreadsheets().batchUpdate(SPREADSHEET_ID, body).execute();
}
Also used : CopyPasteRequest(com.google.api.services.sheets.v4.model.CopyPasteRequest) UpdateSpreadsheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest) SpreadsheetProperties(com.google.api.services.sheets.v4.model.SpreadsheetProperties) BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) GridRange(com.google.api.services.sheets.v4.model.GridRange) BatchUpdateSpreadsheetRequest(com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest) UpdateSpreadsheetPropertiesRequest(com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest) BatchUpdateValuesRequest(com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest) Request(com.google.api.services.sheets.v4.model.Request) CopyPasteRequest(com.google.api.services.sheets.v4.model.CopyPasteRequest) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 3 with Request

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

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

Aggregations

BatchUpdateSpreadsheetRequest (com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest)4 Request (com.google.api.services.sheets.v4.model.Request)4 UpdateSpreadsheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest)4 AddSheetRequest (com.google.api.services.sheets.v4.model.AddSheetRequest)3 UpdateSheetPropertiesRequest (com.google.api.services.sheets.v4.model.UpdateSheetPropertiesRequest)3 ArrayList (java.util.ArrayList)3 SheetProperties (com.google.api.services.sheets.v4.model.SheetProperties)2 SpreadsheetProperties (com.google.api.services.sheets.v4.model.SpreadsheetProperties)2 BatchUpdateValuesRequest (com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest)1 CopyPasteRequest (com.google.api.services.sheets.v4.model.CopyPasteRequest)1 GridProperties (com.google.api.services.sheets.v4.model.GridProperties)1 GridRange (com.google.api.services.sheets.v4.model.GridRange)1 Spreadsheet (com.google.api.services.sheets.v4.model.Spreadsheet)1 Test (org.junit.Test)1