Google Sheets API 返回“无效值[3][1]:struct_value {\n}\n”错误

Glo*_*del 4 java google-sheets-api

我正在使用适用于 Java 的 Google Sheets API v4用一些值填充电子表格:

String spreadsheetID = ...;
Spreadsheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(),
    JacksonFactory.getDefaultInstance(), credential).setApplicationName("Test").build().spreadsheets();

List<List<Object>> content = new ArrayList<>();
content.add(Arrays.asList("Client", "John Doe"));
content.add(Arrays.asList("Date", new Date()));
service.values().update(spreadsheetID, "Sheet1!C3:D4",
    new ValueRange().setValues(content)).setValueInputOption("RAW").execute();
Run Code Online (Sandbox Code Playgroud)

但调用时出现以下错误execute()

String spreadsheetID = ...;
Spreadsheets service = new Sheets.Builder(GoogleNetHttpTransport.newTrustedTransport(),
    JacksonFactory.getDefaultInstance(), credential).setApplicationName("Test").build().spreadsheets();

List<List<Object>> content = new ArrayList<>();
content.add(Arrays.asList("Client", "John Doe"));
content.add(Arrays.asList("Date", new Date()));
service.values().update(spreadsheetID, "Sheet1!C3:D4",
    new ValueRange().setValues(content)).setValueInputOption("RAW").execute();
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Glo*_*del 6

方法采用嵌套的of作为参数,setValues但这ValueRange并不意味着您可以将所有类型的对象放入该列表中。根据文档,ListObject

对于输入,支持的值类型有:bool、string 和 double。

根据个人经验,整数和 BigDecimals 也可以工作,但是对于日期,您需要将它们转换为自 1899 年 12 月 30 日以来的天数,并应用此处描述的日期格式,或者(如果您不关心它不会存储)作为一天)作为字符串,例如通过 SimpleDateFormat。