2016-07-15 83 views
2

我有一個包含多列的Google表格。我們使用這個來保持測試賬戶的登錄/密碼組合。我被要求每90天自動更改一次每個帳戶的密碼以符合IT要求。如何解決Google Sheets V4 API不返回空單元格

我決定使用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) 
     { 
+0

我遇到同樣的問題 - 您是否找到解決方案? –

回答

1

我有一個類似的用例,並用「CellFeeds」來解決這個問題。

我從Google電子表格中提取了所有的行和列,並且有一個問題,即空/空值錯過了,列數總是受到影響。

您可以嘗試將「return-empty = true」作爲查詢參數添加到CellFeed的URL中。它將空值作爲空值返回,而不是繞過這些值。

您可以參考this page瞭解有關使用基於單元格的Feed的詳細文檔。

下面的代碼可以幫助您與您的關注:

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行,包括爲空,而不會影響列排列的空單元格。

希望這會有所幫助。

相關問題