java - 将数据库数据写入 Google 表格。Java、谷歌表格 API
问题描述
我的sqlite数据库中有一些联系人列表数据,现在我想使用Google Sheet API v4在Google sheet中写入,我已经成功输入了列名,但是数据部分,我不知道如何填写数组列表:
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());
下面是我如何从数据库中获取数据:
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();
解决方案
回答:
这需要是一个数组数组,外部数组的每个元素对应于 Sheet 中的一行。
更多信息:
从文档:
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]
]
参考:
推荐阅读
- javascript - 如何使用计算属性和道具过滤对象 [VueJS]
- java - Java springboot websocket,浏览器和服务器之间没有连接
- python - 如何从python中的字符串中的每个单词中提取数字
- c++ - 在 Visual Studio 2005 上的 C++ 应用程序中设置代理
- elasticsearch - Elasticsearch 按 inner_hits 总值排序
- java - Java Spring 休眠和反射
- android - NavigationDrawer - 每个片段的不同工具栏
- python - 将各种文本字段的输入输入标签并将其保存为文件
- bash - 当我使用“cat”命令将文本文件的内容放入变量时,变量不会被解释
- protocol-buffers - 带有字节字段的 Protobuf 结构