forked from googleworkspace/java-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpreadsheetSnippets.java
More file actions
297 lines (278 loc) · 13.2 KB
/
Copy pathSpreadsheetSnippets.java
File metadata and controls
297 lines (278 loc) · 13.2 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.model.*;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class SpreadsheetSnippets {
private Sheets service;
public SpreadsheetSnippets(Sheets service) {
this.service = service;
}
public String create(String title) throws IOException {
Sheets service = this.service;
// [START sheets_create]
Spreadsheet spreadsheet = new Spreadsheet()
.setProperties(new SpreadsheetProperties()
.setTitle(title));
spreadsheet = service.spreadsheets().create(spreadsheet)
.setFields("spreadsheetId")
.execute();
System.out.println("Spreadsheet ID: " + spreadsheet.getSpreadsheetId());
// [END sheets_create]
return spreadsheet.getSpreadsheetId();
}
public BatchUpdateSpreadsheetResponse batchUpdate(String spreadsheetId, String title,
String find, String replacement)
throws IOException {
Sheets service = this.service;
// [START sheets_batch_update]
List<Request> requests = new ArrayList<>();
// Change the spreadsheet's title.
requests.add(new Request()
.setUpdateSpreadsheetProperties(new UpdateSpreadsheetPropertiesRequest()
.setProperties(new SpreadsheetProperties()
.setTitle(title))
.setFields("title")));
// Find and replace text.
requests.add(new Request()
.setFindReplace(new FindReplaceRequest()
.setFind(find)
.setReplacement(replacement)
.setAllSheets(true)));
// Add additional requests (operations) ...
BatchUpdateSpreadsheetRequest body =
new BatchUpdateSpreadsheetRequest().setRequests(requests);
BatchUpdateSpreadsheetResponse response =
service.spreadsheets().batchUpdate(spreadsheetId, body).execute();
FindReplaceResponse findReplaceResponse = response.getReplies().get(1).getFindReplace();
System.out.printf("%d replacements made.", findReplaceResponse.getOccurrencesChanged());
// [END sheets_batch_update]
return response;
}
public ValueRange getValues(String spreadsheetId, String range) throws IOException {
Sheets service = this.service;
// [START sheets_get_values]
ValueRange result = service.spreadsheets().values().get(spreadsheetId, range).execute();
int numRows = result.getValues() != null ? result.getValues().size() : 0;
System.out.printf("%d rows retrieved.", numRows);
// [END sheets_get_values]
return result;
}
public BatchGetValuesResponse batchGetValues(String spreadsheetId, List<String> _ranges)
throws IOException {
Sheets service = this.service;
// [START sheets_batch_get_values]
List<String> ranges = Arrays.asList(
//Range names ...
);
// [START_EXCLUDE silent]
ranges = _ranges;
// [END_EXCLUDE]
BatchGetValuesResponse result = service.spreadsheets().values().batchGet(spreadsheetId)
.setRanges(ranges).execute();
System.out.printf("%d ranges retrieved.", result.getValueRanges().size());
// [END sheets_batch_get_values]
return result;
}
public UpdateValuesResponse updateValues(String spreadsheetId, String range,
String valueInputOption, List<List<Object>> _values)
throws IOException {
Sheets service = this.service;
// [START sheets_update_values]
List<List<Object>> values = Arrays.asList(
Arrays.asList(
// Cell values ...
)
// Additional rows ...
);
// [START_EXCLUDE silent]
values = _values;
// [END_EXCLUDE]
ValueRange body = new ValueRange()
.setValues(values);
UpdateValuesResponse result =
service.spreadsheets().values().update(spreadsheetId, range, body)
.setValueInputOption(valueInputOption)
.execute();
System.out.printf("%d cells updated.", result.getUpdatedCells());
// [END sheets_update_values]
return result;
}
public BatchUpdateValuesResponse batchUpdateValues(String spreadsheetId, String range,
String valueInputOption,
List<List<Object>> _values)
throws IOException {
Sheets service = this.service;
// [START sheets_batch_update_values]
List<List<Object>> values = Arrays.asList(
Arrays.asList(
// Cell values ...
)
// Additional rows ...
);
// [START_EXCLUDE silent]
values = _values;
// [END_EXCLUDE]
List<ValueRange> data = new ArrayList<ValueRange>();
data.add(new ValueRange()
.setRange(range)
.setValues(values));
// Additional ranges to update ...
BatchUpdateValuesRequest body = new BatchUpdateValuesRequest()
.setValueInputOption(valueInputOption)
.setData(data);
BatchUpdateValuesResponse result =
service.spreadsheets().values().batchUpdate(spreadsheetId, body).execute();
System.out.printf("%d cells updated.", result.getTotalUpdatedCells());
// [END sheets_batch_update_values]
return result;
}
public AppendValuesResponse appendValues(String spreadsheetId, String range,
String valueInputOption, List<List<Object>> _values)
throws IOException {
Sheets service = this.service;
// [START sheets_append_values]
List<List<Object>> values = Arrays.asList(
Arrays.asList(
// Cell values ...
)
// Additional rows ...
);
// [START_EXCLUDE silent]
values = _values;
// [END_EXCLUDE]
ValueRange body = new ValueRange()
.setValues(values);
AppendValuesResponse result =
service.spreadsheets().values().append(spreadsheetId, range, body)
.setValueInputOption(valueInputOption)
.execute();
System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells());
// [END sheets_append_values]
return result;
}
public BatchUpdateSpreadsheetResponse pivotTables(String spreadsheetId) throws IOException {
Sheets service = this.service;
// Create two sheets for our pivot table.
List<Request> sheetsRequests = new ArrayList<>();
sheetsRequests.add(new Request().setAddSheet(new AddSheetRequest()));
sheetsRequests.add(new Request().setAddSheet(new AddSheetRequest()));
BatchUpdateSpreadsheetRequest createSheetsBody = new BatchUpdateSpreadsheetRequest()
.setRequests(sheetsRequests);
BatchUpdateSpreadsheetResponse createSheetsResponse = service.spreadsheets()
.batchUpdate(spreadsheetId, createSheetsBody).execute();
int sourceSheetId = createSheetsResponse.getReplies().get(0).getAddSheet().getProperties()
.getSheetId();
int targetSheetId = createSheetsResponse.getReplies().get(1).getAddSheet().getProperties()
.getSheetId();
// [START sheets_pivot_tables]
PivotTable pivotTable = new PivotTable()
.setSource(
new GridRange()
.setSheetId(sourceSheetId)
.setStartRowIndex(0)
.setStartColumnIndex(0)
.setEndRowIndex(20)
.setEndColumnIndex(7)
)
.setRows(Collections.singletonList(
new PivotGroup()
.setSourceColumnOffset(1)
.setShowTotals(true)
.setSortOrder("ASCENDING")
))
.setColumns(Collections.singletonList(
new PivotGroup()
.setSourceColumnOffset(4)
.setShowTotals(true)
.setSortOrder("ASCENDING")
))
.setValues(Collections.singletonList(
new PivotValue()
.setSummarizeFunction("COUNTA")
.setSourceColumnOffset(4)
));
List<Request> requests = Lists.newArrayList();
Request updateCellsRequest = new Request().setUpdateCells(new UpdateCellsRequest()
.setFields("*")
.setRows(Collections.singletonList(
new RowData().setValues(
Collections.singletonList(
new CellData().setPivotTable(pivotTable))
)
))
.setStart(new GridCoordinate()
.setSheetId(targetSheetId)
.setRowIndex(0)
.setColumnIndex(0)
));
requests.add(updateCellsRequest);
BatchUpdateSpreadsheetRequest updateCellsBody = new BatchUpdateSpreadsheetRequest()
.setRequests(requests);
BatchUpdateSpreadsheetResponse result = service.spreadsheets()
.batchUpdate(spreadsheetId, updateCellsBody).execute();
// [END sheets_pivot_tables]
return result;
}
public BatchUpdateSpreadsheetResponse conditionalFormat(String spreadsheetId)
throws IOException {
// [START sheets_conditional_formatting]
List<GridRange> ranges = Collections.singletonList(new GridRange()
.setSheetId(0)
.setStartRowIndex(1)
.setEndRowIndex(11)
.setStartColumnIndex(0)
.setEndColumnIndex(4)
);
List<Request> requests = Arrays.asList(
new Request().setAddConditionalFormatRule(new AddConditionalFormatRuleRequest()
.setRule(new ConditionalFormatRule()
.setRanges(ranges)
.setBooleanRule(new BooleanRule()
.setCondition(new BooleanCondition()
.setType("CUSTOM_FORMULA")
.setValues(Collections.singletonList(
new ConditionValue().setUserEnteredValue(
"=GT($D2,median($D$2:$D$11))")
))
)
.setFormat(new CellFormat().setTextFormat(
new TextFormat().setForegroundColor(
new Color().setRed(0.8f))
))
)
)
.setIndex(0)
),
new Request().setAddConditionalFormatRule(new AddConditionalFormatRuleRequest()
.setRule(new ConditionalFormatRule()
.setRanges(ranges)
.setBooleanRule(new BooleanRule()
.setCondition(new BooleanCondition()
.setType("CUSTOM_FORMULA")
.setValues(Collections.singletonList(
new ConditionValue().setUserEnteredValue(
"=LT($D2,median($D$2:$D$11))")
))
)
.setFormat(new CellFormat().setBackgroundColor(
new Color().setRed(1f).setGreen(0.4f).setBlue(0.4f)
))
)
)
.setIndex(0)
)
);
BatchUpdateSpreadsheetRequest body = new BatchUpdateSpreadsheetRequest()
.setRequests(requests);
BatchUpdateSpreadsheetResponse result = service.spreadsheets()
.batchUpdate(spreadsheetId, body)
.execute();
System.out.printf("%d cells updated.", result.getReplies().size());
// [END sheets_conditional_formatting]
return result;
}
}