Penny Chen
Penny Chen

Reputation: 79

Write Database data into Google sheet. Java, Googe Sheet API

I have some contact list data in my sqlite database, now I would like to write in Google sheet by using Google Sheet API v4, I have successfully put in the column name, but the data part, I don't know how to fill in the Array list:

        String[] column ={"_ID","name","mobile","tel","company","email","birthday","products","purchased_date","reminders","ps"};
        
        List<List<Object>> values = Arrays.asList(
                            Arrays.asList( field
                                    // Cell values ...
                            )
                            // Additional rows ...
        
                    );
       ValueRange body = new ValueRange().setValues(values);
       UpdateValuesResponse result =
                            sheetService.spreadsheets().values().update(fileId, "A1", body)
                                    .setValueInputOption("RAW")
                                    .execute();
       System.out.printf("%d cells updated.", result.getUpdatedCells());

below is how I get the data from database:

 myDB.open();
        Cursor cursor = myDB.getAll();
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                String id = cursor.getString(0);
                String name = cursor.getString(1);
                String mobile = cursor.getString(2);
                String tel = cursor.getString(3);
                String company = cursor.getString(4);
                String email = cursor.getString(5);
                String birthday = cursor.getString(6);
                String products = cursor.getString(7);
                String purchased_date = cursor.getString(8);
                String reminders = cursor.getString(9);
                String ps = cursor.getString(10);


                cursor.moveToNext();
            }
        }
        myDB.close();

Upvotes: 0

Views: 133

Answers (1)

Rafa Guillermo
Rafa Guillermo

Reputation: 15377

Answer:

This needs to be an array of arrays, each element of the outer array corresponding to a row in the Sheet.

More Information:

From the documentation:

public ValueRange setValues(java.util.List<java.util.List<java.lang.Object>> values)

The data that was read or to be written. This is an array of arrays, the outer array representing all the data and each inner array representing a major dimension. Each item in the inner array corresponds with one cell. For output, empty trailing rows and columns will not be included. For input, supported value types are: bool, string, and double. Null values will be skipped. To set a cell to an empty value, set the string value to an empty string.

Parameters: values - values or null for none

Array Structure:

[
  [ "_ID", "name", "mobile", "tel", "company", "email", "birthday", "products", "purchased_date", "reminders", "ps" ],
  [ id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps]
]

References:

Upvotes: 1

Related Questions