我从Write data to Google Sheet using Google Sheet API V4 - Java Sample Code @“Ashwin Karangutkar”中获取了以下代码。其中写入googlespread工作表中的单元格。
但我真的想做的是将这些数据附加到工作表上或写在第一行。我遇到了api的AppendCellsRequest函数,但我不太清楚如何集成它。任何人都可以帮我这个吗?
提前谢谢。
亲切的问候,
尼科斯。
Aswin的代码示例
public static void setValue(String SheetName,String RowStart, String RowEnd) throws IOException{
// Build a new authorized API client service.
Sheets service = getSheetsService();
// Prints the names and majors of students in a sample spreadsheet:
String spreadsheetId = "17PQyEPayrHSIgvPtrfZtItuUcArhvjDy-G68Xg02s3s";
String range = SheetName+"!"+RowStart+":"+RowEnd;
Integer sheetLocationNumber = 0;
List<List<Object>> arrData = getData();
ValueRange oRange = new ValueRange();
oRange.setRange(range); // I NEED THE NUMBER OF THE LAST ROW
oRange.setValues(arrData);
List<ValueRange> oList = new ArrayList<>();
oList.add(oRange);
BatchUpdateValuesRequest oRequest = new BatchUpdateValuesRequest();
oRequest.setValueInputOption("RAW");
oRequest.setData(oList);
BatchUpdateValuesResponse oResp1 = service.spreadsheets().values().batchUpdate(spreadsheetId, oRequest).execute();
// service.spreadsheets().values().update (spreadsheetId, range,) ;
//return request;
}
public static List<List<Object>> getData () {
//将数据添加到水平轴(同一行)
List<Object> data1 = new ArrayList<Object>();
data1.add ("Example");
data1.add ("Example2");
System.out.println("Values");
//将数据添加到垂直轴(新行)
List<List<Object>> data = new ArrayList<List<Object>>();
data.add (data1);
return data;
}
答案 0 :(得分:2)
如果要附加数据(并且不关心单元格的格式或其他属性),最简单的方法是使用spreadsheets.values.append。示例用法指南为here。
用法类似于values.update或values.get。
答案 1 :(得分:1)
我能够使用java将数据附加到我自己的Google电子表格中,我从这个stackflow问题Google Sheet API V4(Java) append Date in cells中得到了答案并对其进行了修改以添加我的数据,我是下面代码的示例。
我承认,它做得非常糟糕,它不是可扩展的atm,我将创建一个hashMap / list来匹配带有标题的值,这样我就可以正确更新字段,而不是将它们更新为盲目的我是atm。
public static void main(String[] args) throws Exception {
Sheets service = getSheetsService();
String spreadSheetID = "xx";
Integer sheetID = 0;
String defectReferenceValue = "Defect 9999";
Double defectIDValue = 9999.0;
String areaValue = "RandomArea";
String screenValue = "";
String fieldValue = "";
String reqValue = "";
String ruleValue = "";
String fieldIDValue = "";
String defectDescriptionValue = "";
String screenShotValue = "ScreenShotLink";
String dateValue = dateForReport();
List<RowData> rowData = new ArrayList<RowData>();
List<CellData> cellData = new ArrayList<CellData>();
CellData defectReferenceField = new CellData();
defectReferenceField.setUserEnteredValue(new ExtendedValue().setStringValue(defectReferenceValue));
defectReferenceField.setUserEnteredFormat(new CellFormat().setNumberFormat(new NumberFormat().setType("DATE")));
cellData.add(defectReferenceField);
CellData defectIDField = new CellData();
defectIDField.setUserEnteredValue(new ExtendedValue().setNumberValue(defectIDValue));
cellData.add(defectIDField);
CellData areaField = new CellData();
areaField.setUserEnteredValue(new ExtendedValue().setStringValue(areaValue));
cellData.add(areaField);
CellData screenField = new CellData();
screenField.setUserEnteredValue(new ExtendedValue().setStringValue(screenValue));
cellData.add(screenField);
CellData fieldField = new CellData();
fieldField.setUserEnteredValue(new ExtendedValue().setStringValue(fieldValue));
cellData.add(fieldField);
CellData reqField = new CellData();
reqField.setUserEnteredValue(new ExtendedValue().setStringValue(reqValue));
cellData.add(reqField);
CellData ruleField = new CellData();
ruleField.setUserEnteredValue(new ExtendedValue().setStringValue(ruleValue));
cellData.add(ruleField);
CellData fieldIDField = new CellData();
fieldIDField.setUserEnteredValue(new ExtendedValue().setStringValue(fieldIDValue));
cellData.add(fieldIDField);
CellData defectDescriptionField = new CellData();
defectDescriptionField.setUserEnteredValue(new ExtendedValue().setStringValue(defectDescriptionValue));
cellData.add(defectDescriptionField);
CellData screenShotField = new CellData();
screenShotField.setUserEnteredValue(new ExtendedValue().setStringValue(screenShotValue));
cellData.add(screenShotField);
CellData dateField = new CellData();
dateField.setUserEnteredValue(new ExtendedValue().setStringValue(dateValue));
cellData.add(dateField);
rowData.add(new RowData().setValues(cellData));
BatchUpdateSpreadsheetRequest batchRequests = new BatchUpdateSpreadsheetRequest();
BatchUpdateSpreadsheetResponse response;
List<Request> requests = new ArrayList<Request>();
AppendCellsRequest appendCellReq = new AppendCellsRequest();
appendCellReq.setSheetId(sheetID);
appendCellReq.setRows(rowData);
appendCellReq.setFields("userEnteredValue,userEnteredFormat.numberFormat");
requests = new ArrayList<Request>();
requests.add(new Request().setAppendCells(appendCellReq));
batchRequests = new BatchUpdateSpreadsheetRequest();
batchRequests.setRequests(requests);
response = service.spreadsheets().batchUpdate(spreadSheetID, batchRequests).execute();
System.out.println("Request \n\n");
System.out.println(batchRequests.toPrettyString());
System.out.println("\n\nResponse \n\n");
System.out.println(response.toPrettyString());
}
我希望这有助于寻找类似答案的其他人。
此致
尼科斯。
答案 2 :(得分:0)
String content ="just now";
List<Object> data1 = new ArrayList<>();
data1.add (content);
List<List<Object>> data = new ArrayList<>();
data.add (data1);
ValueRange valueRange=new ValueRange();
valueRange.setValues(data);
service.spreadsheets().values().
append(spreadsheetId, range, valueRange)
.setValueInputOption("RAW")
.execute();
上面的代码工作正常