将数据库数据写入Google表格。Java,Google表格API。

huangapple go评论112阅读模式
英文:

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 ={&quot;_ID&quot;,&quot;name&quot;,&quot;mobile&quot;,&quot;tel&quot;,&quot;company&quot;,&quot;email&quot;,&quot;birthday&quot;,&quot;products&quot;,&quot;purchased_date&quot;,&quot;reminders&quot;,&quot;ps&quot;};
        
        List&lt;List&lt;Object&gt;&gt; values = Arrays.asList(
                            Arrays.asList( field
                                    // Cell values ...
                            )
                            // Additional rows ...
        
                    );
       ValueRange body = new ValueRange().setValues(values);
       UpdateValuesResponse result =
                            sheetService.spreadsheets().values().update(fileId, &quot;A1&quot;, body)
                                    .setValueInputOption(&quot;RAW&quot;)
                                    .execute();
       System.out.printf(&quot;%d cells updated.&quot;, 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&lt;java.util.List&lt;java.lang.Object&gt;&gt; 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]
]

参考:

英文:

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&lt;java.util.List&lt;java.lang.Object&gt;&gt; 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:

[
  [ &quot;_ID&quot;, &quot;name&quot;, &quot;mobile&quot;, &quot;tel&quot;, &quot;company&quot;, &quot;email&quot;, &quot;birthday&quot;, &quot;products&quot;, &quot;purchased_date&quot;, &quot;reminders&quot;, &quot;ps&quot; ],
  [ id, name, mobile, tel, company, email, birthday, products, purchased_date, reminders, ps]
]

References:

huangapple
  • 本文由 发表于 2020年9月20日 00:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63971253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定