|
| 1 | +package com.baeldung.google.sheets; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.security.GeneralSecurityException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.junit.BeforeClass; |
| 10 | +import org.junit.Test; |
| 11 | + |
| 12 | +import com.google.api.services.sheets.v4.Sheets; |
| 13 | +import com.google.api.services.sheets.v4.model.AppendValuesResponse; |
| 14 | +import com.google.api.services.sheets.v4.model.BatchGetValuesResponse; |
| 15 | +import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest; |
| 16 | +import com.google.api.services.sheets.v4.model.BatchUpdateValuesRequest; |
| 17 | +import com.google.api.services.sheets.v4.model.BatchUpdateValuesResponse; |
| 18 | +import com.google.api.services.sheets.v4.model.CopyPasteRequest; |
| 19 | +import com.google.api.services.sheets.v4.model.GridRange; |
| 20 | +import com.google.api.services.sheets.v4.model.Request; |
| 21 | +import com.google.api.services.sheets.v4.model.Spreadsheet; |
| 22 | +import com.google.api.services.sheets.v4.model.SpreadsheetProperties; |
| 23 | +import com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest; |
| 24 | +import com.google.api.services.sheets.v4.model.UpdateValuesResponse; |
| 25 | +import com.google.api.services.sheets.v4.model.ValueRange; |
| 26 | + |
| 27 | +import static org.assertj.core.api.Assertions.*; |
| 28 | + |
| 29 | +public class GoogleSheetsIntegrationTest { |
| 30 | + |
| 31 | + private static Sheets sheetsService; |
| 32 | + |
| 33 | + // this id can be replaced with your spreadsheet id |
| 34 | + // otherwise be advised that multiple people may run this test and update the public spreadsheet |
| 35 | + private static final String SPREADSHEET_ID = "1sILuxZUnyl_7-MlNThjt765oWshN3Xs-PPLfqYe4DhI"; |
| 36 | + |
| 37 | + @BeforeClass |
| 38 | + public static void setup() throws GeneralSecurityException, IOException { |
| 39 | + sheetsService = SheetsServiceUtil.getSheetsService(); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void whenWriteSheet_thenReadSheetOk() throws IOException { |
| 44 | + ValueRange body = new ValueRange() |
| 45 | + .setValues(Arrays.asList( |
| 46 | + Arrays.asList("Expenses January"), |
| 47 | + Arrays.asList("books", "30"), |
| 48 | + Arrays.asList("pens", "10"), |
| 49 | + Arrays.asList("Expenses February"), |
| 50 | + Arrays.asList("clothes", "20"), |
| 51 | + Arrays.asList("shoes", "5"))); |
| 52 | + UpdateValuesResponse result = sheetsService.spreadsheets().values() |
| 53 | + .update(SPREADSHEET_ID, "A1", body) |
| 54 | + .setValueInputOption("RAW") |
| 55 | + .execute(); |
| 56 | + |
| 57 | + List<ValueRange> data = new ArrayList<>(); |
| 58 | + data.add(new ValueRange() |
| 59 | + .setRange("D1") |
| 60 | + .setValues(Arrays.asList( |
| 61 | + Arrays.asList("January Total", "=B2+B3")))); |
| 62 | + data.add(new ValueRange() |
| 63 | + .setRange("D4") |
| 64 | + .setValues(Arrays.asList( |
| 65 | + Arrays.asList("February Total", "=B5+B6")))); |
| 66 | + |
| 67 | + BatchUpdateValuesRequest batchBody = new BatchUpdateValuesRequest() |
| 68 | + .setValueInputOption("USER_ENTERED") |
| 69 | + .setData(data); |
| 70 | + BatchUpdateValuesResponse batchResult = |
| 71 | + sheetsService.spreadsheets().values() |
| 72 | + .batchUpdate(SPREADSHEET_ID, batchBody) |
| 73 | + .execute(); |
| 74 | + |
| 75 | + List<String> ranges = Arrays.asList("E1","E4"); |
| 76 | + BatchGetValuesResponse readResult = |
| 77 | + sheetsService.spreadsheets().values() |
| 78 | + .batchGet(SPREADSHEET_ID) |
| 79 | + .setRanges(ranges) |
| 80 | + .execute(); |
| 81 | + |
| 82 | + ValueRange januaryTotal = readResult.getValueRanges().get(0); |
| 83 | + assertThat(januaryTotal.getValues().get(0).get(0)).isEqualTo("40"); |
| 84 | + |
| 85 | + ValueRange febTotal = readResult.getValueRanges().get(1); |
| 86 | + assertThat(febTotal.getValues().get(0).get(0)).isEqualTo("25"); |
| 87 | + |
| 88 | + ValueRange appendBody = new ValueRange() |
| 89 | + .setValues(Arrays.asList( |
| 90 | + Arrays.asList("Total", "=E1+E4"))); |
| 91 | + AppendValuesResponse appendResult = |
| 92 | + sheetsService.spreadsheets().values() |
| 93 | + .append(SPREADSHEET_ID, "A1", appendBody) |
| 94 | + .setValueInputOption("USER_ENTERED") |
| 95 | + .setInsertDataOption("INSERT_ROWS") |
| 96 | + .setIncludeValuesInResponse(true) |
| 97 | + .execute(); |
| 98 | + |
| 99 | + ValueRange total = appendResult.getUpdates().getUpdatedData(); |
| 100 | + assertThat(total.getValues().get(0).get(1)).isEqualTo("65"); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + @Test |
| 105 | + public void whenUpdateSpreadSheetTitle_thenOk() throws IOException { |
| 106 | + |
| 107 | + UpdateSpreadsheetPropertiesRequest updateRequest = new UpdateSpreadsheetPropertiesRequest() |
| 108 | + .setFields("*") |
| 109 | + .setProperties(new SpreadsheetProperties().setTitle("Expenses")); |
| 110 | + |
| 111 | + CopyPasteRequest copyRequest = new CopyPasteRequest() |
| 112 | + .setSource(new GridRange().setSheetId(0) |
| 113 | + .setStartColumnIndex(0).setEndColumnIndex(2) |
| 114 | + .setStartRowIndex(0).setEndRowIndex(1)) |
| 115 | + .setDestination(new GridRange().setSheetId(1) |
| 116 | + .setStartColumnIndex(0).setEndColumnIndex(2) |
| 117 | + .setStartRowIndex(0).setEndRowIndex(1)) |
| 118 | + .setPasteType("PASTE_VALUES"); |
| 119 | + |
| 120 | + List<Request> requests = new ArrayList<>(); |
| 121 | + |
| 122 | + requests.add(new Request().setCopyPaste(copyRequest)); |
| 123 | + requests.add(new Request().setUpdateSpreadsheetProperties(updateRequest)); |
| 124 | + |
| 125 | + BatchUpdateSpreadsheetRequest body = |
| 126 | + new BatchUpdateSpreadsheetRequest().setRequests(requests); |
| 127 | + |
| 128 | + sheetsService.spreadsheets().batchUpdate(SPREADSHEET_ID, body).execute(); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void whenCreateSpreadSheet_thenIdOk() throws IOException { |
| 133 | + Spreadsheet spreadSheet = new Spreadsheet() |
| 134 | + .setProperties(new SpreadsheetProperties().setTitle("My Spreadsheet")); |
| 135 | + Spreadsheet result = sheetsService.spreadsheets().create(spreadSheet).execute(); |
| 136 | + |
| 137 | + assertThat(result.getSpreadsheetId()).isNotNull(); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments