在将 Google 电子表格 API 与 JAVA 结合使用时,公式单元格值未在参考中更新
Formula Cell value is not updating in the reference while Using Google Spreadsheet API with JAVA
我有这个示例代码,我在其中尝试将 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 的值。它在 google 电子表格中显示正确的值。我无法在我的代码中读取它。
我们将不胜感激。
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());
}
}
我有这个示例代码,我在其中尝试将 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 的值。它在 google 电子表格中显示正确的值。我无法在我的代码中读取它。
我们将不胜感激。
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());
}
}