Hello Everyone, Previously I have posted about reading data from Google spreadsheets and got lots of comments regarding writing data to google sheet. So in this post, I am describing how to write data to a google spreadsheet.
Here I am using a Google service account and auth key for authentication purposes. Here I'll show you how to obtain a key from google console.
- Go to Google Cloud Platform and log in using your google account
- Create a new project on the console
- Enable google sheets API for this project
- Click on the project -- APIs & Services -- Credentials and click on Create credentials button
- Select key type p12 and put account name
- After this step, p12 key is downloaded to your system
Now see the java code that inserts a new record in the existing google sheet, Here I am using the same Google sheet that I have used in the previous post.
https://docs.google.com/spreadsheets/d/1G3VABou70MUbRzY4vHgiDME5JW5rfo7lrAr_hZyEawU is the URL of the spreadsheet and it looks like this
- package spreadsheetdemo;
- import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
- import com.google.api.client.http.HttpTransport;
- import com.google.api.client.http.javanet.NetHttpTransport;
- import com.google.api.client.json.jackson.JacksonFactory;
- import com.google.gdata.client.spreadsheet.SpreadsheetService;
- import com.google.gdata.data.spreadsheet.ListEntry;
- import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
- import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
- import com.google.gdata.data.spreadsheet.WorksheetEntry;
- import com.google.gdata.data.spreadsheet.WorksheetFeed;
- import com.google.gdata.util.AuthenticationException;
- import com.google.gdata.util.ServiceException;
- import java.io.File;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.security.GeneralSecurityException;
- import java.util.Arrays;
- import java.util.List;
- public class MySpreadsheetIntegration {
- public static void main(String[] args) throws AuthenticationException, MalformedURLException, IOException,
- ServiceException, GeneralSecurityException {
- SpreadsheetService service = new SpreadsheetService("MySpreadsheetIntegration-v1");
- // Put the path of p12 file that is downloaded from Google Console
- File p12 = new File("D:/API Project-f35e9ad5ad07.p12");
- HttpTransport httpTransport = new NetHttpTransport();
- JacksonFactory jsonFactory = new JacksonFactory();
- String[] SCOPESArray = {
- "https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full",
- "https://docs.google.com/feeds"
- };
- final List SCOPES = Arrays.asList(SCOPESArray);
- //Put your google service account id
- GoogleCredential credential =
- new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory).setServiceAccountId("xxxxxxx.iam.gserviceaccount.com").setServiceAccountScopes(SCOPES).setServiceAccountPrivateKeyFromP12File(p12).build();
- service.setOAuth2Credentials(credential);
- // Define the URL to request. This should never change.
- URL SPREADSHEET_FEED_URL =
- new URL("https://spreadsheets.google.com/feeds/worksheets/1G3VABou70MUbRzY4vHgiDME5JW5rfo7lrAr_hZyEawU/private/full");
- // Make a request to the API and get all spreadsheets.
- SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
- List<SpreadsheetEntry> spreadsheets = feed.getEntries();
- System.out.println("Total Sheets- " + spreadsheets.size());
- if (spreadsheets.size() == 0) {
- }
- SpreadsheetEntry spreadsheet = spreadsheets.get(0);
- System.out.println(spreadsheet.getTitle().getPlainText());
- // Get the first worksheet of the first spreadsheet.
- WorksheetFeed worksheetFeed = service.getFeed(spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
- List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
- WorksheetEntry worksheet = worksheets.get(0);
- System.out.println(worksheet.getTitle().getPlainText());
- // Create a local representation of the new row.
- ListEntry row = new ListEntry();
- row.getCustomElements().setValueLocal("Cell1", "Testing");
- row.getCustomElements().setValueLocal("Cell2", "New Data");
- // Send the new row to the API for insertion.
- row = service.insert(spreadsheet.getWorksheetFeedUrl(), row);
- }
- }
After running this code see the new row is inserted in Google Sheet
You can download the required jars from this link
Cheers Happy Learning
No comments :
Post a Comment