1+ // Copyright 2022 Google LLC
2+ //
3+ // Licensed under the Apache License, Version 2.0 (the "License");
4+ // you may not use this file except in compliance with the License.
5+ // You may obtain a copy of the License at
6+ //
7+ // https://www.apache.org/licenses/LICENSE-2.0
8+ //
9+ // Unless required by applicable law or agreed to in writing, software
10+ // distributed under the License is distributed on an "AS IS" BASIS,
11+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ // See the License for the specific language governing permissions and
13+ // limitations under the License.
14+
15+
16+ // [START sheets_batch_update_values]
17+ import com .google .api .client .googleapis .json .GoogleJsonError ;
18+ import com .google .api .client .googleapis .json .GoogleJsonResponseException ;
19+ import com .google .api .client .http .HttpRequestInitializer ;
20+ import com .google .api .client .http .javanet .NetHttpTransport ;
21+ import com .google .api .client .json .gson .GsonFactory ;
22+ import com .google .api .services .sheets .v4 .Sheets ;
23+ import com .google .api .services .sheets .v4 .SheetsScopes ;
24+ import com .google .api .services .sheets .v4 .model .BatchUpdateValuesRequest ;
25+ import com .google .api .services .sheets .v4 .model .BatchUpdateValuesResponse ;
26+ import com .google .api .services .sheets .v4 .model .ValueRange ;
27+ import com .google .auth .http .HttpCredentialsAdapter ;
28+ import com .google .auth .oauth2 .GoogleCredentials ;
29+
30+ import java .io .IOException ;
31+ import java .util .ArrayList ;
32+ import java .util .Collections ;
33+ import java .util .List ;
34+
35+ /* Class to demonstrate the use of Spreadsheet Batch Update Values API */
36+ public class BatchUpdateValues {
37+ /**
38+ * Set values in one or more ranges of spreadsheet.
39+ *
40+ * @param spreadsheetId - Id of the spreadsheet.
41+ * @param range - Range of cells of the spreadsheet.
42+ * @param valueInputOption - Determines how input data should be interpreted.
43+ * @param values - list of rows of values to input.
44+ * @return spreadsheet with updated values
45+ * @throws IOException - if credentials file not found.
46+ */
47+ public static BatchUpdateValuesResponse batchUpdateValues (String spreadsheetId ,
48+ String range ,
49+ String valueInputOption ,
50+ List <List <Object >> values )
51+ throws IOException {
52+ /* Load pre-authorized user credentials from the environment.
53+ TODO(developer) - See https://developers.google.com/identity for
54+ guides on implementing OAuth2 for your application. */
55+ GoogleCredentials credentials = GoogleCredentials .getApplicationDefault ()
56+ .createScoped (Collections .singleton (SheetsScopes .SPREADSHEETS ));
57+ HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter (
58+ credentials );
59+
60+ // Create the sheets API client
61+ Sheets service = new Sheets .Builder (new NetHttpTransport (),
62+ GsonFactory .getDefaultInstance (),
63+ requestInitializer )
64+ .setApplicationName ("Sheets samples" )
65+ .build ();
66+
67+ List <ValueRange > data = new ArrayList <>();
68+ data .add (new ValueRange ()
69+ .setRange (range )
70+ .setValues (values ));
71+
72+ BatchUpdateValuesResponse result = null ;
73+ try {
74+ // Updates the values in the specified range.
75+ BatchUpdateValuesRequest body = new BatchUpdateValuesRequest ()
76+ .setValueInputOption (valueInputOption )
77+ .setData (data );
78+ result = service .spreadsheets ().values ().batchUpdate (spreadsheetId , body ).execute ();
79+ System .out .printf ("%d cells updated." , result .getTotalUpdatedCells ());
80+ } catch (GoogleJsonResponseException e ) {
81+ // TODO(developer) - handle error appropriately
82+ GoogleJsonError error = e .getDetails ();
83+ if (error .getCode () == 404 ) {
84+ System .out .printf ("Spreadsheet not found with id '%s'.\n " ,spreadsheetId );
85+ } else {
86+ throw e ;
87+ }
88+ }
89+ return result ;
90+ }
91+ }
92+ // [END sheets_batch_update_values]
0 commit comments