将数据库数据写入 Google sheet。 Java、谷歌 Sheet API

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

我的 sqlite 数据库中有一些联系人列表数据,现在我想使用 Google Sheet API 写入 Google sheet v4,我已经成功填入了列名,但是数据部分,不知道怎么填Array列表:

        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中的一行。

更多信息:

来自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

数组结构:

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

参考文献: