Reputation: 79
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
Reputation: 15377
This needs to be an array of arrays, each element of the outer array corresponding to a row in the Sheet.
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
ornull
for none
[
[ "_ID", "name", "mobile", "tel", "company", "email", "birthday", "products", "purchased_date", "reminders", "ps" ],
[ id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps]
]
Upvotes: 1