android - How to assign a value to a ValueRange variable in java? -


i trying implement google sheets api writing feature app using following code web page unable figure out , how assign value valuerange variable.

public class sheetsexample { public static void main(string args[]) throws ioexception, generalsecurityexception { // id of spreadsheet update. string spreadsheetid = "my-spreadsheet-id"; // todo: update placeholder value.  //the a1 notation of values update. string range = "my-range"; // todo: update placeholder value.  // todo: assign values desired fields of `requestbody`. existing // fields replaced: valuerange requestbody = new valuerange();  sheets sheetsservice = createsheetsservice(); sheets.spreadsheets.values.update request =     sheetsservice.spreadsheets().values().update(spreadsheetid, range, requestbody);  updatevaluesresponse response = request.execute();  // todo: change code below process `response` object: system.out.println(response);   }     public static sheets createsheetsservice() throws ioexception, generalsecurityexception {     httptransport httptransport = googlenethttptransport.newtrustedtransport();     jsonfactory jsonfactory = jacksonfactory.getdefaultinstance();  // todo: change placeholder below generate authentication credentials. see // https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample // // authorize using 1 of following scopes: //   "https://www.googleapis.com/auth/drive" //   "https://www.googleapis.com/auth/drive.file" //   "https://www.googleapis.com/auth/spreadsheets" googlecredential credential = null;  return new sheets.builder(httptransport, jsonfactory, credential)     .setapplicationname("google-sheetssample/0.1")     .build(); }   } 

refer api guide google sheets api v4

first, need create valuerange instance. can valuerange.setvalues() method. @ben has shown in answer, can create valuerange instance in following way. https://stackoverflow.com/a/46171564/2999358

valuerange requestbody = new valuerange();  requestbody.setvalues(     arrays.aslist(       arrays.aslist("row 1 cell 1", "row 1 cell 2", "row 1 cell 3"),       arrays.aslist("row 2 cell 1", "row 2 cell 2", "row 2 cell 3"))); 

as can see in api guide, setvalues() requires array of arrays

public valuerange setvalues(java.util.list<java.util.list<java.lang.object>>  values) 

the outer list represents data (basically whole spreadsheet), while inner lists represents rows. each item in these lists represents cell.

after set values want write spreadsheet, can use valuerange object (in case requestbody variable) update spreadsheet.

if have followed https://developers.google.com/sheets/api/quickstart/android quick start guide, following changes code. since code shows how fetch data spreadsheet.

change following line,

private static final string[] scopes = { sheetsscopes.spreadsheets_readonly };  

to this,

private static final string[] scopes = { sheetsscopes.spreadsheets }; 

this enable view , manage spreadsheets in google drive.

and inside getdatafromapi() method in guide, contains following code,

valuerange response = this.mservice.spreadsheets().values()                 .get(spreadsheetid, range)                 .execute(); 

which used fetch data spreadsheet. change following,

valuerange response = this.mservice.spreadsheets().values()                     .update(spreadsheetid, range, valuerange)                     .setvalueinputoption("raw")                     .execute(); 

setting valueinputoption "raw" make values enter stored as-is. if use "user_entered", values enter parsed entering value cell google spreadsheet ui. conversions strings numbers, dates etc. rules apply when use google spreadsheet ui. up-to how want handle.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -