2016-07-15 2 views
2

여러 열이 포함 된 Google 시트가 있습니다. 우리는 이것을 사용하여 테스트 계정에 대한 로그인/비밀번호 조합을 유지합니다. IT 요구 사항을 충족하기 위해 90 일마다 각 계정의 암호를 변경하는 프로세스를 자동화하라는 요청을 받았습니다.비어있는 셀을 반환하지 않는 Google 스프레드 시트 V4 API를 얻는 방법

자바와 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 스프레드 시트에서 모든 행과 열을 가져 왔으며 빈/null 값이 누락되어 열의 수가 항상 영향을 받았다는 문제가있었습니다.

"return-empty = true"를 쿼리 매개 변수로 CellFeed의 URL에 추가 할 수 있습니다. 빈 값을 우회하는 대신 null 값으로 반환합니다.

셀 기반 피드 사용에 대한 자세한 설명은 this page을 참조하십시오.

다음 코드는 우려와 함께 당신을 도울 수 있습니다

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(); 
} 

결과는 당신에게 열 배열에 영향을주지 않고 null로 빈 세포를 포함하는 제 10 개 행을 제공 할 것입니다.

희망이 도움이됩니다.