有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将Google电子表格API与JAVA一起使用时,引用中的excel公式单元格值未更新

我有一个示例代码,在其中我尝试将google电子表格API与java集成

import java.net.URL;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.WorksheetEntry;

public class ExpressionExample {

    public static final String GOOGLE_ACCOUNT_USERNAME = "xxxx@gmail.com";

    public static final String GOOGLE_ACCOUNT_PASSWORD = "xxx";

    private static URL cellFeedUrl;

    public static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/1QWX-zOkBe36M7oC9sn6z8ZuccGt7Wg-IFwtynn379kM";

    public static void main(String[] args) throws Exception {

        SpreadsheetService service = new SpreadsheetService(
                "Print Google Spreadsheet Demo");

        service.setUserCredentials(GOOGLE_ACCOUNT_USERNAME,
                GOOGLE_ACCOUNT_PASSWORD);

        URL metafeedUrl = new URL(SPREADSHEET_URL);

        SpreadsheetEntry spreadsheet = service.getEntry(metafeedUrl,
                SpreadsheetEntry.class);

        WorksheetEntry sheet = ((WorksheetEntry) spreadsheet.getWorksheets()
                .get(0));

        cellFeedUrl = spreadsheet.getWorksheets().get(0).getCellFeedUrl();

        CellEntry cellA1 = new CellEntry(1, 1, "3");
        CellEntry cellB1 = new CellEntry(1, 2, "high mech");

        String line = "=IF(A1<5,IF(B1={\"high mech\",\"low mech\",\"mid mech\"},10,IF(B1=\"electronic\",20,0)),IF(A1>=5,IF(B1=\"electronic\",40,0),0)) ";
        CellEntry cellc1 = new CellEntry(1, 3, line);

        service.insert(cellFeedUrl, cellA1);
        service.insert(cellFeedUrl, cellB1);
        service.insert(cellFeedUrl, cellc1);

        System.out.println(cellc1.getCell().getValue());
        System.out.println(cellc1.getCell().getNumericValue());
        System.out.println(cellc1.getCell().getDoubleValue());
    }

}

当我运行代码时 我得到这个值

空的 无效的 楠

我想要单元格C1的值。它在谷歌电子表格中显示了正确的值。我无法在代码中读取它

任何帮助都将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    getCell()只读取有关单元格的本地信息,即插入的内容。 您必须再次从CellFeed中获取单元格,然后遍历条目以再次找到您的单元格以读取联机信息

    service.getFeed(cellFeedUrl, CellFeed.class);
    for(CellEntry ce : cellFeed.getEntries()) {
      Cell cell = ce.getCell();
      if(cell.getRow() == 1 && cell.getCol() == 3) {
        System.out.println(cell.getDoubleValue());
      }
    }