gpt4 book ai didi

java - 类型错误使用 Google 表格 api 将列分配给 Java 中的 ValueRange 变量进行 append ?

转载 作者:搜寻专家 更新时间:2023-11-01 08:19:54 28 4
gpt4 key购买 nike

“java.lang.String 无法转换为 java.lang.Object”

 public class SheetsQuickstart {
private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";

/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
public ValueRange getValues(String spreadsheetId, String range, Sheets service) throws IOException {

// [START sheets_get_values]
ValueRange result = service.spreadsheets().values().get(spreadsheetId, range).execute();
int numRows = result.getValues() != null ? result.getValues().size() : 0;
System.out.printf("%d rows retrieved.", numRows);
// [END sheets_get_values]
return result;
}
private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
// Load client secrets.
InputStream in = SheetsQuickstart.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}

/**
* Prints the names and majors of students in a sample spreadsheet:
* https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
*/
public static void main(String... args) throws IOException, GeneralSecurityException {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final String spreadsheetId = "1h6NmjAqRBV-Mg67VYJ6K7nuNKHDzhMB0dP-iTgXuaJM";
final String range = "A1:E";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();


ValueRange body = new ValueRange();
body.setValues(
Arrays.asList(
Arrays.asList("Row 1 Cell 1", "Row 1 Cell 2", "Row 1 Cell 3"),
Arrays.asList("Row 2 Cell 1", "Row 2 Cell 2", "Row 2 Cell 3")));

AppendValuesResponse result =
service.spreadsheets().values().append(spreadsheetId,"A1", body)
.setValueInputOption("RAW")
.setInsertDataOption("INSERT_ROWS")
.execute();
System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells());


}

我正在尝试这个 google 表格 api,我是 java 的新手。我不明白如何创建 valuerange 主体。在列的 append 主体中,我在

处收到类型错误
 ValueRange body = new ValueRange().setValues(
Arrays.asList(
Arrays.asList("Row 1 Cell 1", "Row 1 Cell 2", "Row 1 Cell 3"),
Arrays.asList("Row 2 Cell 1", "Row 2 Cell 2", "Row 2 Cell 3")));

它抛出 Error:(104, 30) java: incompatible types: java.util.List< java.util.List< java.lang.String >> cannot be converted to java.util.List< java.util.列表 >我错过了创建类(class)吗?为什么会抛出这个错误。

最佳答案

String 更改为 Object

ValueRange body = new ValueRange().setValues(
Arrays.asList(
Arrays.asList((Object)"Row 1 Cell 1", (Object)"Row 1 Cell 2",(Object) "Row 1 Cell 3"),
Arrays.asList((Object)"Row 2 Cell 1",(Object) "Row 2 Cell 2", (Object)"Row 2 Cell 3")));

关于java - 类型错误使用 Google 表格 api 将列分配给 Java 中的 ValueRange 变量进行 append ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52681987/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com