英文:
Write Database data into Google sheet. Java, Googe Sheet API
问题
// 导入所需的库
import java.util.Arrays;
import java.util.List;
import com.google.api.services.sheets.v4.model.ValueRange;
// 定义要插入的列名数组
String[] column = {"_ID", "name", "mobile", "tel", "company", "email", "birthday", "products", "purchased_date", "reminders", "ps"};
// 创建存储数据的二维列表
List<List<Object>> values = Arrays.asList(
Arrays.asList(
id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps
)
// 可以继续添加其他行的数据
);
// 创建一个 ValueRange 对象,并设置数据
ValueRange body = new ValueRange().setValues(values);
// 使用 Google Sheet API 执行更新操作
UpdateValuesResponse result = sheetService.spreadsheets().values().update(fileId, "A1", body)
.setValueInputOption("RAW")
.execute();
System.out.printf("%d cells updated.", result.getUpdatedCells());
从数据库获取数据的部分:
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);
// 将每一行数据添加到二维列表的新行中
values.add(Arrays.asList(id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps));
cursor.moveToNext();
}
}
myDB.close();
英文:
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();
答案1
得分: 1
答案:
这需要是一个包含数组的数组,外部数组的每个元素对应表中的一行。
更多信息:
从文档中获得:
> public ValueRange setValues(java.util.List<java.util.List<java.lang.Object>> values)
>
> 已读取或将要写入的数据。这是一个包含数组的数组,外部数组表示所有数据,每个内部数组表示一个主要维度。内部数组中的每个项对应一个单元格。对于输出,不会包括空行和列。对于输入,支持的值类型包括:bool、string 和 double。将跳过空值。要将单元格设置为空值,请将字符串值设置为空字符串。
>
> 参数:
> values
- values
或 null
表示无
数组结构:
[
[ "_ID", "name", "mobile", "tel", "company", "email", "birthday", "products", "purchased_date", "reminders", "ps" ],
[ id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps]
]
参考:
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论