Java 如何绕过GoogleSheetsV4API不返回空单元格

Java 如何绕过GoogleSheetsV4API不返回空单元格,java,google-api,google-spreadsheet-api,Java,Google Api,Google Spreadsheet Api,我有一张谷歌表格,上面有几列。我们使用它来保持测试帐户的登录/密码组合。我被要求每隔90天自动更改每个帐户的密码,以满足IT要求 我决定使用Java和GoogleSheets V4 API。我需要打开工作表,收集所有行,并遍历它们。对于每一行,获取登录ID和密码,生成新密码,更改密码,然后使用新密码和旧密码写回该行 现在,我的绊脚石是,对于一行中没有内容的单元格,它不会返回任何内容。因此,一行将有5列,一行将有4列。我无法知道哪一列是空的,因此没有返回 有没有办法让它返回空单元格 ran

我有一张谷歌表格,上面有几列。我们使用它来保持测试帐户的登录/密码组合。我被要求每隔90天自动更改每个帐户的密码,以满足IT要求

我决定使用Java和GoogleSheets V4 API。我需要打开工作表,收集所有行,并遍历它们。对于每一行,获取登录ID和密码,生成新密码,更改密码,然后使用新密码和旧密码写回该行

现在,我的绊脚石是,对于一行中没有内容的单元格,它不会返回任何内容。因此,一行将有5列,一行将有4列。我无法知道哪一列是空的,因此没有返回

有没有办法让它返回空单元格

    range = "'Functional Users'!A" + rowCount + ":E" + rowCount;
    response = service.spreadsheets().values().get(spreadsheetId, range).execute();
    values = response.getValues();
    cells = values.get(0);
    while (cells != null || cells.size() != 0)
    {
        //Iterate through the columns in the row (NOTE: If a column is blank, it will be missing here)
        //Cells(0) = Domain
        //Cells(1) = Ignored
        //Cells(2) = Account Name
        //Cells(3) = Email
        //Cells(4) = Password Status (Soon to be deprecated)
        if (cells.size() == 5)
        {

我有一个类似的用例,并使用“CellFeeds”来解决这个问题

我从谷歌电子表格中获取了所有的行和列,但出现了一个问题,即空/空值丢失,列数始终受到影响

您可以尝试将“returnempty=true”作为查询参数添加到CellFeed的URL中。它将空值返回为null,而不是绕过这些值

您可以参考有关使用基于单元格的提要的详细文档

以下代码可能有助于解决您的问题:

SpreadsheetService service =
    new SpreadsheetService("MyService");

URL SPREADSHEET_FEED_URL = new URL(
    "https://spreadsheets.google.com/feeds/cells/"+ "SpreadSheetID" +"/default/public/values");  //Replace SpreadSheetID with your spreadsheet ID, Note: Here the spreadsheet is shared publicly. If it is not published publicly, you need to change the URL and also workaround with the API keys.

// Fetch the first 10 rows of the spreadsheet
URL cellFeedUrl;
try {
    cellFeedUrl = new URI(SPREADSHEET_FEED_URL.toString()
        + "?return-empty=true&min-row=1&max-row=10").toURL(); //Here, return-empty=true is important
    CellFeed cellFeed = service.getFeed(cellFeedUrl, CellFeed.class);

    // Iterate through each cell, printing its value.
    for (CellEntry cell : cellFeed.getEntries()) {
        System.out.println(cell.getCell().getValue() + "\t");
    }
}
catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
结果将为您提供前10行(包括空单元格)为null,而不影响列的排列


希望这能有所帮助。

我也遇到同样的问题-您找到解决方案了吗?