如何为java中的ValueRange变量赋值?

我正在尝试使用此网页中的以下代码将Google工作表API编写function实施到我的应用中,但无法弄清楚如何为ValueRange变量赋值。

public class SheetsExample { public static void main(String args[]) throws IOException, GeneralSecurityException { // The ID of the spreadsheet to update. String spreadsheetId = "my-spreadsheet-id"; // TODO: Update placeholder value. //The A1 notation of the values to update. String range = "my-range"; // TODO: Update placeholder value. // TODO: Assign values to desired fields of `requestBody`. All existing // fields will be replaced: ValueRange requestBody = new ValueRange(); Sheets sheetsService = createSheetsService(); Sheets.Spreadsheets.Values.Update request = sheetsService.spreadsheets().values().update(spreadsheetId, range, requestBody); UpdateValuesResponse response = request.execute(); // TODO: Change code below to process the `response` object: System.out.println(response); } public static Sheets createSheetsService() throws IOException, GeneralSecurityException { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFactory jsonFactory = JacksonFactory.getDefaultInstance(); // TODO: Change placeholder below to generate authentication credentials. See // https://developers.google.com/sheets/quickstart/java#step_3_set_up_the_sample // // Authorize using one of the following scopes: // "https://www.googleapis.com/auth/drive" // "https://www.googleapis.com/auth/drive.file" // "https://www.googleapis.com/auth/spreadsheets" GoogleCredential credential = null; return new Sheets.Builder(httpTransport, jsonFactory, credential) .setApplicationName("Google-SheetsSample/0.1") .build(); } } 

请参阅API指南Google表格API v4

首先,您需要创建ValueRange实例。 您可以通过ValueRange.setValues()方法执行此ValueRange.setValues() 。 正如@Ben在他的回答中所示,您可以通过以下方式创建ValueRange实例。 https://stackoverflow.com/a/46171564/2999358

 ValueRange requestBody = new ValueRange(); requestBody.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"))); 

正如您在API指南中看到的那样, setValues()需要一个数组数组

 public ValueRange setValues(java.util.List> values) 

外部列表代表所有数据(基本上是整个电子表格),而内部列表代表ROWS。 这些列表中的每个项目代表一个CELL。

设置要写入电子表格的值后,可以使用ValueRange对象(在您的情况下为requestBody变量)来更新电子表格。

如果您已按照https://developers.google.com/sheets/api/quickstart/android快速入门指南操作,请对该代码执行以下更改。 由于该代码仅显示如何从电子表格中获取数据。

改变以下行,

 private static final String[] SCOPES = { SheetsScopes.SPREADSHEETS_READONLY }; 

对此,

 private static final String[] SCOPES = { SheetsScopes.SPREADSHEETS }; 

这样,您就可以在Google云端硬盘中查看和管理电子表格了。

在本指南的getDataFromAPI()方法内,包含以下代码,

 ValueRange response = this.mService.spreadsheets().values() .get(spreadsheetId, range) .execute(); 

用于从电子表格中获取数据。 将此更改为以下,

 ValueRange response = this.mService.spreadsheets().values() .update(spreadsheetId, range, valueRange) .setValueInputOption("RAW") .execute(); 

将ValueInputOption设置为“RAW”将使您输入的值按原样存储。 如果您使用“USER_ENTERED”,则在您从Google电子表格用户界面输入单元格的值时,将解析您输入的值。 这将对字符串进行转换,如数字,日期等。基本上,当您使用Google电子表格用户界面时,将适用所有规则。 这取决于您想要处理的方式。

我认为你在这里尝试做的是用一组值更新现有范围?

在这种情况下,你可以这样做:

  ValueRange requestBody = new ValueRange(); requestBody.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"))); 

希望有所帮助,在实践中,您将以编程方式构建列表。

我将我的贡献留给了一个不会在答案中抛出编译错误的具体示例:

https://stackoverflow.com/a/48832281/1621848

谢谢。