簡體   English   中英

如何在Java中為ValueRange變量賦值?

[英]How to assign a value to a ValueRange variable in java?

我正在嘗試使用此網頁中的以下代碼在我的應用程序中實現Google Sheets API編寫功能,但無法弄清楚如何以及如何為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 Sheets 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<java.util.List<java.lang.Object>> 
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 Spreadsheet UI向單元格輸入值時,將解析您輸入的值。 它將進行字符串到數字,日期等的轉換。基本上,所有使用Google電子表格UI時都會應用的規則。 這取決於您要如何處理。

我認為您實際上要在此處執行的操作是使用一組值更新現有范圍?

在這種情況下,您可以這樣:

   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

謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM