user3492837
user3492837

Reputation: 1

Google Sheets API bug

Been following the guide on Googles website. Have my OAuth keys correctly setup but can't get this cell extraction code to work in Java.

Error message is:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from element type SpreadsheetEntry to CellEntry

at com.wow.trading.WOWTrading.MySpreadsheetIntegration.main(MySpreadsheetIntegration.java:51)

Code is exactly as Google website and JARs are not missing.

package com.wow.trading.WOWTrading;
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;

import java.io.IOException;
import java.net.*;
import java.util.*;

public class MySpreadsheetIntegration {
  public static void main(String[] args)
      throws AuthenticationException, MalformedURLException, IOException, ServiceException {

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

    // TODO: Authorize the service object for a specific user (see other     sections)

    // Define the URL to request.  This should never change.
    URL SPREADSHEET_FEED_URL = new URL(
        "https://spreadsheets.google.com/feeds/spreadsheets/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();

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

    // TODO: Choose a spreadsheet more intelligently based on your
    // app's needs.
    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's needs.
    WorksheetFeed worksheetFeed = service.getFeed(
        spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
    List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
    WorksheetEntry worksheet = worksheets.get(0);

    // Fetch the cell feed of the worksheet.
    URL cellFeedUrl = worksheet.getCellFeedUrl();
    CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

    // Iterate through each cell, updating its value if necessary.
    // TODO: Update cell values more intelligently.
    **for (CellEntry cell : feed.getEntries()) {**
      if (cell.getTitle().getPlainText().equals("A1")) {
        cell.changeInputValueLocal("200");
        cell.update();
      } else if (cell.getTitle().getPlainText().equals("B1")) {
        cell.changeInputValueLocal("=SUM(A1, 200)");
        cell.update();
      }
    }
  }
}

Upvotes: 0

Views: 95

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64959

I've never used the Google Spreadsheets API, but the line you've highlighted has an error which is fairly clear to me.

Your variable feed is a SpreadsheetFeed. As you have a number of 'feed' objects, for spreadsheets, worksheets and cells, it would perhaps be an idea to rename feed to spreadsheetFeed, so it is clear which type of feed it is. You're already doing this with worksheets (worksheetFeed) and cells (cellFeed), so why not spreadsheets as well?

If you do that, the line of code you've highlighted above then becomes

for (CellEntry cell : spreadsheetFeed.getEntries()) {

Hopefully by now the error is obvious. I'm guessing you wanted

for (CellEntry cell : cellFeed.getEntries()) {

instead.

Upvotes: 1

Related Questions