forked from googleworkspace/java-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadsheetSnippetsTest.java
More file actions
121 lines (107 loc) · 4.82 KB
/
Copy pathSpreadsheetSnippetsTest.java
File metadata and controls
121 lines (107 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import com.google.api.services.sheets.v4.model.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
public class SpreadsheetSnippetsTest extends BaseTest {
private SpreadsheetSnippets snippets;
@Before
public void createSnippets() {
this.snippets = new SpreadsheetSnippets(this.service);
}
@Test
public void testCreate() throws IOException {
String id = this.snippets.create("Title");
assertNotNull(id);
this.deleteFileOnCleanup(id);
}
@Test
public void testBatchUpdate() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
this.populateValuesWithStrings(spreadsheetId);
BatchUpdateSpreadsheetResponse response =
this.snippets.batchUpdate(spreadsheetId, "New Title", "Hello", "Goodbye");
List<Response> replies = response.getReplies();
assertEquals(2, replies.size());
FindReplaceResponse findReplaceResponse = replies.get(1).getFindReplace();
assertEquals(100, findReplaceResponse.getOccurrencesChanged().intValue());
}
@Test
public void testConditionalFormat() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
this.populateValuesWithNumbers(spreadsheetId);
BatchUpdateSpreadsheetResponse response =
this.snippets.conditionalFormat(spreadsheetId);
assertEquals(spreadsheetId, response.getSpreadsheetId());
assertEquals(2, response.getReplies().size());
}
@Test
public void testGetValues() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
this.populateValuesWithStrings(spreadsheetId);
ValueRange result = this.snippets.getValues(spreadsheetId, "A1:C2");
List<List<Object>> values = result.getValues();
assertEquals(2, values.size());
assertEquals(3, values.get(0).size());
}
@Test
public void testBatchGetValues() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
this.populateValuesWithStrings(spreadsheetId);
List<String> ranges = Arrays.asList("A1:A3", "B1:C1");
BatchGetValuesResponse result = this.snippets.batchGetValues(spreadsheetId, ranges);
List<ValueRange> valueRanges = result.getValueRanges();
assertEquals(2, valueRanges.size());
List<List<Object>> values = valueRanges.get(0).getValues();
assertEquals(3, values.size());
}
@Test
public void testUpdateValues() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
List<List<Object>> values = Arrays.asList(
Arrays.asList("A", "B"),
Arrays.asList("C", "D"));
UpdateValuesResponse result =
this.snippets.updateValues(spreadsheetId, "A1:B2", "USER_ENTERED", values);
assertEquals(2, result.getUpdatedRows().intValue());
assertEquals(2, result.getUpdatedColumns().intValue());
assertEquals(4, result.getUpdatedCells().intValue());
}
@Test
public void testBatchUpdateValues() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
List<List<Object>> values = Arrays.asList(
Arrays.asList("A", "B"),
Arrays.asList("C", "D"));
BatchUpdateValuesResponse result =
this.snippets.batchUpdateValues(spreadsheetId, "A1:B2", "USER_ENTERED", values);
assertEquals(1, result.getResponses().size());
assertEquals(2, result.getTotalUpdatedRows().intValue());
assertEquals(2, result.getTotalUpdatedColumns().intValue());
assertEquals(4, result.getTotalUpdatedCells().intValue());
}
@Test
public void testAppendValues() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
this.populateValuesWithStrings(spreadsheetId);
List<List<Object>> values = Arrays.asList(
Arrays.asList("A", "B"),
Arrays.asList("C", "D"));
AppendValuesResponse result =
this.snippets.appendValues(spreadsheetId, "A1:B2", "USER_ENTERED", values);
assertEquals("Sheet1!A1:J10", result.getTableRange());
UpdateValuesResponse updates = result.getUpdates();
assertEquals(2, updates.getUpdatedRows().intValue());
assertEquals(2, updates.getUpdatedColumns().intValue());
assertEquals(4, updates.getUpdatedCells().intValue());
}
@Test
public void testPivotTable() throws IOException {
String spreadsheetId = this.createTestSpreadsheet();
BatchUpdateSpreadsheetResponse result = this.snippets.pivotTables(spreadsheetId);
assertNotNull(result);
}
}