在使用谷歌表格 API 登录我的 Google 帐户后出现错误。

huangapple go评论73阅读模式
英文:

Error after I log in with my Google Account in Google Sheet API

问题

我正在尝试通过Java操作Google表格,但在使用我的Google账号登录后出现错误:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
    	at java.util.ArrayList.get(ArrayList.java:433)
    	at com.esdras.caixaipm.DeskIPM.main(DeskIPM.java:75)

这是我的PSVM代码:

    public static void main(String[] args) throws IOException, GeneralSecurityException {
        sheetsService = getSheetsService();
        String range = "A2:A4";
        
        ValueRange response = sheetsService.spreadsheets().values()
                .get(SPREADSHEET_ID, range)
                .execute();
        
        List<List<Object>> values = response.getValues();
        
        if (values == null || values.isEmpty()){
            System.out.println("未找到数据!");
        }else{
            for (List<Object> row : values) {
                System.out.printf("%s %s %s \n", row.get(0), row.get(1), row.get(2));
            }
        }
        
    }

我认为问题出现在for循环的结构,但我无法修复它。
英文:

I'm trying to manipulate a Google Sheet through Java, but there's a error after I log in with my Google Account:

Exception in thread &quot;main&quot; java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at com.esdras.caixaipm.DeskIPM.main(DeskIPM.java:75)

In this is my PSVM:

public static void main(String[] args) throws IOException, GeneralSecurityException {
    sheetsService = getSheetsService();
    String range = &quot;A2:A4&quot;;
    
    ValueRange response = sheetsService.spreadsheets().values()
            .get(SPREADSHEET_ID, range)
            .execute();
    
    List&lt;List&lt;Object&gt;&gt; values = response.getValues();
    
    if (values == null || values.isEmpty()){
        System.out.println(&quot;No data found!&quot;);
    }else{
        for (List row : values) {
            System.out.printf(&quot;%s %s %s \n&quot;, row.get(1), row.get(2), row.get(3));
        }
    }
    
}

I think the problem is in the for structure, but I can't fix it.

答案1

得分: 0

因为"row.get()"而出现错误。

我将代码编辑为以下内容:

for (List row : values) {
    System.out.println(row.get(0));
}

而不是:

for (List row : values) {
    System.out.printf("%s %s %s \n", row.get(1), row.get(2), row.get(3));
}

谢谢... 在使用谷歌表格 API 登录我的 Google 帐户后出现错误。

英文:

The error I was getting was because of the "row.get()"

I edited the code to this:

for (List row : values) {
            System.out.println(row.get(0));
        }

Instead of:

for (List row : values) {
            System.out.printf(&quot;%s %s %s \n&quot;, row.get(1), row.get(2), row.get(3));
        }

Thanks... 在使用谷歌表格 API 登录我的 Google 帐户后出现错误。

huangapple
  • 本文由 发表于 2020年5月31日 05:31:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/62108917.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定