Android: add line to Google Spread Sheet

I use the following code to add a list line , this is successful. But he looks complicated.

SpreadsheetService service =
    new SpreadsheetService("MySpreadsheetIntegration-v1");

// Define the URL to request.  This should never change.
URL SPREADSHEET_FEED_URL = new URL(
    "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

// TODO Make a request to the API and get all spreadsheets.

/*
In Here, Instead of request all spread sheets,
I want to request only my spread sheet which I have stored on Google Drive.
*/

SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
    SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();

if (spreadsheets.size() == 0) {
  // TODO: There were no spreadsheets, act accordingly.
}

SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());

// Get the first worksheet of the first spreadsheet.
// TODO: Choose a worksheet more intelligently based on your
// app needs.
WorksheetFeed worksheetFeed = service.getFeed(
    spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);

// Fetch the list feed of the worksheet.
URL listFeedUrl = worksheet.getListFeedUrl();
ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

// Create a local representation of the new row.
ListEntry row = new ListEntry();
row.getCustomElements().setValueLocal("firstname", "Joe");
row.getCustomElements().setValueLocal("lastname", "Smith");

// Send the new row to the API for insertion.
row = service.insert(listFeedUrl, row);

And now I want to be simple.

Instead of requesting all distribution sheets, I want to request only my distribution sheet, which I saved on Google Drive.

I have a link to a file distribution sheet, docs.google.com/spreadsheet/ccc?key= KEYOFFILE # gid = 0.

Please say Thank you

0
source share
1 answer

Well, I fix it myself.

Use instead

URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");

We use:

URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/worksheets/KEYOFTHEFILE/private/full")

Thanks,

+1
source

All Articles