【问题标题】:Google Sheets API bug谷歌表格 API 错误
【发布时间】:2016-11-04 07:08:46
【问题描述】:

一直按照 Google 网站上的指南进行操作。正确设置了我的 OAuth 密钥,但无法让此单元格提取代码在 Java 中工作。

错误信息是:

线程“main”java.lang.Error 中的异常:未解决的编译问题: 类型不匹配:无法从元素类型 SpreadsheetEntry 转换为 CellEntry

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

代码与 Google 网站完全相同,并且没有丢失 JAR。

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();
      }
    }
  }
}

【问题讨论】:

  • 你的问题到底是什么?

标签: java api oauth google-sheets


【解决方案1】:

我从未使用过 Google 电子表格 API,但您突出显示的行有一个错误,我很清楚。

您的变量feedSpreadsheetFeed。由于您有许多“提要”对象,对于电子表格、工作表和单元格,将feed 重命名为spreadsheetFeed 可能是一个想法,因此很清楚它是哪种类型的提要。您已经在使用工作表 (worksheetFeed) 和单元格 (cellFeed) 进行此操作,那么为什么不使用电子表格呢?

如果你这样做,你上面突出显示的代码行就会变成

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

希望现在错误很明显。我猜你想要

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

改为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 2017-06-11
    • 2017-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多