Java 使用googlesheetsapi插入行
我正在努力理解Google Sheets API。这是我正在使用的电子表格 我正试图通过遵循谷歌教程向这张工作表中添加工作表 公共类谷歌表单测试{Java 使用googlesheetsapi插入行,java,google-spreadsheet-api,Java,Google Spreadsheet Api,我正在努力理解Google Sheets API。这是我正在使用的电子表格 我正试图通过遵循谷歌教程向这张工作表中添加工作表 公共类谷歌表单测试{ public static void main(String[] a) throws GeneralSecurityException, IOException, ServiceException, URISyntaxException { new GoogleSheetsApiTest().testConnectToSpr
public static void main(String[] a)
throws GeneralSecurityException, IOException, ServiceException, URISyntaxException {
new GoogleSheetsApiTest().testConnectToSpreadSheet();
}
private final String CLIENT_ID = "snip";
private final List<String> SCOPES = Arrays.asList("https://spreadsheets.google.com/feeds");
private final static String P12FILE = "snip.p12";
private final static String WORKSHEET_ID = "snip";
public void testConnectToSpreadSheet()
throws GeneralSecurityException, IOException, ServiceException, URISyntaxException {
SpreadsheetService service = new SpreadsheetService("sheetsbot");
service.setProtocolVersion(SpreadsheetService.Versions.V3);
GoogleCredential credential = getCredentials();
service.setOAuth2Credentials(credential);
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/worksheets/" + WORKSHEET_ID + "/private/full");
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());
WorksheetEntry worksheet = new WorksheetEntry();
worksheet.setTitle(new PlainTextConstruct("New Worksheet"));
worksheet.setColCount(10);
worksheet.setRowCount(20);
URL worksheetFeedUrl = spreadsheet.getWorksheetFeedUrl();
service.insert(worksheetFeedUrl, worksheet);
}
private GoogleCredential getCredentials() throws GeneralSecurityException, IOException, URISyntaxException {
JacksonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
URL fileUrl = this.getClass().getClassLoader().getResource(P12FILE);
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY).setServiceAccountId(CLIENT_ID)
.setServiceAccountPrivateKeyFromP12File(new File(fileUrl.toURI())).setServiceAccountScopes(SCOPES)
.build();
return credential;
}
}
我不明白我做错了什么,也不明白我如何尝试不同的方法。我有添加工作表的代码。我使用:
key = the spreadsheet key
service.insert(new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
+ "/private/full") , worksheet);
我有添加工作表的代码。我使用:
key = the spreadsheet key
service.insert(new URL("https://spreadsheets.google.com/feeds/worksheets/" + key
+ "/private/full") , worksheet);
我看不到您在工作表中添加任何行
// Get the first worksheet of the first spreadsheet.
// TODO: Choose a worksheet more intelligently based on your
// app's needs.
WorksheetFeed worksheetFeed = service.getFeed(
spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);
// Fetch the list feed of the worksheet.
URL listFeedUrl = worksheet.getListFeedUrl();
ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);
// Create a local representation of the new row.
ListEntry row = new ListEntry();
row.getCustomElements().setValueLocal("firstname", "Joe");
row.getCustomElements().setValueLocal("lastname", "Smith");
row.getCustomElements().setValueLocal("age", "26");
row.getCustomElements().setValueLocal("height", "176");
// Send the new row to the API for insertion.
row = service.insert(listFeedUrl, row);
//获取第一个电子表格的第一个工作表。
//TODO:根据您的需求更智能地选择工作表
//应用程序的需求。
工作表提要工作表提要=service.getFeed(
电子表格.getWorksheetFeedUrl(),WorksheetFeed.class);
List worksheets=worksheetFeed.getEntries();
WorksheetEntry工作表=worksheets.get(0);
//获取工作表的列表提要。
URL listFeedUrl=工作表。getListFeedUrl();
ListFeed ListFeed=service.getFeed(listFeedUrl,ListFeed.class);
//创建新行的本地表示形式。
ListEntry行=新建ListEntry();
row.getCustomElements().setValueLocal(“firstname”、“Joe”);
row.getCustomElements().setValueLocal(“lastname”、“Smith”);
row.getCustomElements().setValueLocal(“年龄”,“26”);
row.getCustomElements().setValueLocal(“高度”、“176”);
//将新行发送到API以进行插入。
行=服务.insert(listFeedUrl,行);
我没有看到您在工作表中添加任何行
// Get the first worksheet of the first spreadsheet.
// TODO: Choose a worksheet more intelligently based on your
// app's needs.
WorksheetFeed worksheetFeed = service.getFeed(
spreadsheet.getWorksheetFeedUrl(), WorksheetFeed.class);
List<WorksheetEntry> worksheets = worksheetFeed.getEntries();
WorksheetEntry worksheet = worksheets.get(0);
// Fetch the list feed of the worksheet.
URL listFeedUrl = worksheet.getListFeedUrl();
ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);
// Create a local representation of the new row.
ListEntry row = new ListEntry();
row.getCustomElements().setValueLocal("firstname", "Joe");
row.getCustomElements().setValueLocal("lastname", "Smith");
row.getCustomElements().setValueLocal("age", "26");
row.getCustomElements().setValueLocal("height", "176");
// Send the new row to the API for insertion.
row = service.insert(listFeedUrl, row);
//获取第一个电子表格的第一个工作表。
//TODO:根据您的需求更智能地选择工作表
//应用程序的需求。
工作表提要工作表提要=service.getFeed(
电子表格.getWorksheetFeedUrl(),WorksheetFeed.class);
List worksheets=worksheetFeed.getEntries();
WorksheetEntry工作表=worksheets.get(0);
//获取工作表的列表提要。
URL listFeedUrl=工作表。getListFeedUrl();
ListFeed ListFeed=service.getFeed(listFeedUrl,ListFeed.class);
//创建新行的本地表示形式。
ListEntry行=新建ListEntry();
row.getCustomElements().setValueLocal(“firstname”、“Joe”);
row.getCustomElements().setValueLocal(“lastname”、“Smith”);
row.getCustomElements().setValueLocal(“年龄”,“26”);
row.getCustomElements().setValueLocal(“高度”、“176”);
//将新行发送到API以进行插入。
行=服务.insert(listFeedUrl,行);
我认为您需要定义列名。请参阅以添加行。注意,它定义了标题行:行。getCustomElements().setValueLocal(名称,值);
我认为您需要定义列名。请参阅以添加行。请注意,它定义了标题行:行。getCustomElements().setValueLocal(名称、值);