首页 > 解决方案 > 将数据库数据写入 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();

标签: javaandroidgoogle-sheets-api

解决方案


回答:

这需要是一个数组数组,外部数组的每个元素对应于 Sheet 中的一行。

更多信息:

文档

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

已读取或要写入的数据。这是一个数组数组,外部数组代表所有数据,每个内部数组代表一个主要维度。内部数组中的每一项都对应一个单元格。对于输出,将不包括空的尾随行和列。对于输入,支持的值类型有:bool、string 和 double。将跳过空值。要将单元格设置为空值,请将字符串值设置为空字符串。

参数: values -valuesnull

数组结构:

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

参考:


推荐阅读