public class SheetsQuickstart {
/** Application name. */
private static final String APPLICATION_NAME = "Google Sheets API 
Quickstart";

/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"),
".credentials/sheets.googleapis.com-java-quickstart.json");

/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;

/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory
.getDefaultInstance();

/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;

/**
* Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials at
* ~/.credentials/sheets.googleapis.com-java-quickstart.json
*/
private static final List<String> SCOPES = Arrays.asList(
SheetsScopes.SPREADSHEETS, SheetsScopes.DRIVE);

static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
System.exit(1);
}
}

/**
* Creates an authorized Credential object.
* 
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, SheetsQuickstart.getClientSecret(), SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
System.out.println("Credentials saved to "
+ DATA_STORE_DIR.getAbsolutePath());
return credential;
}
private static GoogleClientSecrets getClientSecret() throws IOException{
// Load client secrets.
InputStream in = SheetsQuickstart.class
.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(in));
return clientSecrets;
}

/**
* Build and return an authorized Sheets API client service.
* 
* @return an authorized Sheets API client service
* @throws IOException
*/
public static Sheets getSheetsService() throws IOException {
Credential credential = authorize();
return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}


public static void main(String[] args) throws IOException, ServiceException 
{
       // Build a new authorized API client service.
       Sheets service = getSheetsService();
       
       
     
       
       // Prints the names and majors of students in a sample spreadsheet:
       // 
https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
       String spreadsheetId = 
"1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms";
       String range = "Sheet1!A7:E13";


   List<List<Object>> arrData = getData();

       ValueRange oRange = new ValueRange();
       oRange.setRange(range); // I NEED THE NUMBER OF THE LAST ROW
       oRange.setValues(arrData);

       List<ValueRange> oList = new ArrayList<>();
       oList.add(oRange);

       BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
       oRequest.setValueInputOption("RAW");
       oRequest.setData(oList);

       BatchUpdateValuesResponse oResp1 = 
service.spreadsheets().values().batchUpdate(spreadsheetId, 
oRequest).execute();
  }

public static List<List<Object>> getData() {

List<Object> data1 = new ArrayList<Object>();
data1.add("jagadeesh");
data1.add("jagadeesh");
data1.add("jagadeesh");

List<Object> data2 = new ArrayList<Object>();
data2.add("jagadeesh");
data2.add("jagadeesh");
data2.add("jagadeesh");

List<List<Object>> data = new ArrayList<List<Object>>();
data.add(data1);
data.add(data2);

return data;
}

}


please refer  this link       
https://developers.google.com/sheets/quickstart/java 
 i use this page while doing this 

On Thursday, 30 June 2016 11:20:26 UTC+5:30, ASHWIN KARANGUTKAR wrote:
>
> Hello Everyone,
> Could anyone provide with sample example of writing data to Google Sheet 
> api v4 using java?
>
> Thanks, Ashwin
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Spreadsheets API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to