简体   繁体   English

如何将数据附加到Google电子表格?

[英]How to append data to a google spreadsheet?

I have checked various SO questions, and I see that there should be an append method available on service().spreadsheet().values() but that method does not exist on this object for me unlike other SO questions. 我检查了各种SO问题,发现在service()。spreadsheet()。values()上应该有一个append方法,但是与其他SO问题不同,该方法对我而言不存在。 How do I append data to a google spreadsheet instead of writing over the whole document? 如何将数据附加到Google电子表格,而不是覆盖整个文档?

This is the code that I am using which writes over the current document instead of appending to it. 这是我使用的代码,它覆盖当前文档,而不是附加到当前文档上。

    public static Sheets getSheetsService() throws IOException {
        Credential credential = authorize();
        return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

...

    public static boolean writeToPersistedGoogleDoc(List<FlightBookingRow> rows) throws IOException {

        Sheets service = getSheetsService();

        String valueInputOption = "USER_ENTERED";

        List<List<Object>> values = new ArrayList<>();

        for(FlightBookingRow row: rows){
            List<Object> list = new ArrayList<>();
            list.add(row.getTimeBooked());
            list.add(row.getPaidStatus());
            list.add(row.getCostOfTicket());
            list.add(row.getFlightPlan());
            list.add(row.getNumberOfPeople().toString());
            list.add(row.getDepartureDate().toString());
            list.add(row.getReturnDate().toString());
            list.add(row.getCompanyBookedWith());
            values.add(list);
        }




        ValueRange body = new ValueRange()
                .setValues(values);
        UpdateValuesResponse result =
                service.spreadsheets().values().update(PERSISTED_FLIGHT_SHEET, RANGE, body)
                        .setValueInputOption(valueInputOption)
                        .execute();
        System.out.printf("%d cells updated.", result.getUpdatedCells());


        return true;
    }

I had a slightly out dated dependency in my pom file. 我的pom文件中有一个过时的依赖项。

I added this, and then I got access to the append method. 我添加了此内容,然后可以访问append方法。

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-sheets</artifactId>
    <version>v4-rev494-1.23.0</version>
</dependency>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM