Java 通过谷歌电子表格API添加标题

Java 通过谷歌电子表格API添加标题,java,google-sheets,google-docs-api,Java,Google Sheets,Google Docs Api,我无法通过JavaAPI将标题(或任何常规数据)插入到新创建的工作表中。插入数据的标准方法是提供标题和值,例如 newEntry.getCustomElements().setValueLocal(header, value2); 此问题与stackoverflow上发布的问题类似: 建议的答案是尝试使用基于单元格的提要,而不是上面示例中基于列表的提要。我一直无法做到这一点,因为对单元格的任何查询或获取单元格提要的尝试似乎都返回0,因为任何单元格中都没有要返回的数据…至少我认为这就是正在发生

我无法通过JavaAPI将标题(或任何常规数据)插入到新创建的工作表中。插入数据的标准方法是提供标题和值,例如

newEntry.getCustomElements().setValueLocal(header, value2);
此问题与stackoverflow上发布的问题类似:

建议的答案是尝试使用基于单元格的提要,而不是上面示例中基于列表的提要。我一直无法做到这一点,因为对单元格的任何查询或获取单元格提要的尝试似乎都返回0,因为任何单元格中都没有要返回的数据…至少我认为这就是正在发生的情况

e、 g

public void getCells(){
cellFeedUrl=worksheetEntry.getCellFeedUrl();
细胞饲料;
试一试{
feed=spreadSheetService.getFeed(cellFeedUrl,CellFeed.class);
for(CellEntry单元格:feed.getEntries()){
System.out.println(worksheetEntry.getTitle().getPlainText());
字符串shortId=cell.getId().substring(cell.getId().lastIndexOf('/')+1);
System.out.println(“--Cell(“+shortId+”/”+Cell.getTitle().getPlainText())
+)公式(“+cell.getCell().getInputValue()+”)数值(“
+cell.getCell().getNumericValue()+“”)值(“”)
+cell.getCell().getValue()+”);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(服务异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
和
公共无效查询头(){
CellQuery查询=新的CellQuery(cellFeedUrl);
query.setMinimumRow(1);
query.setMaximumRow(1);
query.setMinimumCol(1);
query.setMaximumCol(1);
试一试{
CellFeed=spreadSheetService.query(query,CellFeed.class);
List entries=feed.getEntries();
用于(单元格输入单元格:输入){
单元格。changeInputValueLocal(“新”);
}
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(服务异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}

您对空单元格未返回的怀疑是正确的。通过添加以下行,可以覆盖此默认设置:

query.setReturnEmpty(true)

    public void getCells(){
         cellFeedUrl = worksheetEntry.getCellFeedUrl();
        CellFeed feed;
        try {
            feed = spreadSheetService.getFeed(cellFeedUrl, CellFeed.class);

        for (CellEntry cell : feed.getEntries()) {
          System.out.println(worksheetEntry.getTitle().getPlainText());
          String shortId = cell.getId().substring(cell.getId().lastIndexOf('/') + 1);
          System.out.println(" -- Cell(" + shortId + "/" + cell.getTitle().getPlainText()
              + ") formula(" + cell.getCell().getInputValue() + ") numeric("
              + cell.getCell().getNumericValue() + ") value("
              + cell.getCell().getValue() + ")");
        }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

and

public void queryHeader(){

    CellQuery query = new CellQuery(cellFeedUrl);
    query.setMinimumRow(1);
    query.setMaximumRow(1);
    query.setMinimumCol(1);
    query.setMaximumCol(1);
    try {
        CellFeed feed = spreadSheetService.query(query, CellFeed.class);

        List<CellEntry> entries = feed.getEntries();

        for(CellEntry cell : entries){

            cell.changeInputValueLocal("new");
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ServiceException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}