Java 以编程方式编辑Google电子表格
我编写了一个接收用户输入的程序,但现在我想通过每次用户提交表单时编辑谷歌电子表格来保存输入。所以基本上,谷歌电子表格是不断更新的 有人能提供一个关于我如何实现这一目标的教程吗?我正在使用Eclipse用Java编写,那么我需要哪些插件呢 我已经尝试使用(部分)中提供的一些示例代码,但似乎无法使其正常工作Java 以编程方式编辑Google电子表格,java,google-sheets,google-spreadsheet-api,Java,Google Sheets,Google Spreadsheet Api,我编写了一个接收用户输入的程序,但现在我想通过每次用户提交表单时编辑谷歌电子表格来保存输入。所以基本上,谷歌电子表格是不断更新的 有人能提供一个关于我如何实现这一目标的教程吗?我正在使用Eclipse用Java编写,那么我需要哪些插件呢 我已经尝试使用(部分)中提供的一些示例代码,但似乎无法使其正常工作 import com.google.gdata.client.spreadsheet.*; import com.google.gdata.data.spreadsheet.*; import
import com.google.gdata.client.spreadsheet.*;
import com.google.gdata.data.spreadsheet.*;
import com.google.gdata.util.*;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class MySpreadsheetIntegration {
public static void main(String[] args)
throws AuthenticationException, MalformedURLException, IOException, ServiceException {
SpreadsheetService service =
new SpreadsheetService("MySpreadsheetIntegration-v1");
// TODO: Authorize the service object for a specific user (see other sections)
// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://docs.google.com/spreadsheets/d/1OcDp1IZ4iuvyhndtrZ3OOMHZNSEt7XTaaTrhEkNPnN4/edit#gid=0");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if (spreadsheets.size() == 0) {
// TODO: There were no spreadsheets, act accordingly.
}
// TODO: Choose a spreadsheet more intelligently based on your
// app's needs.
SpreadsheetEntry spreadsheet = spreadsheets.get(0);
System.out.println(spreadsheet.getTitle().getPlainText());
// 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);
}
}
import com.google.gdata.client.spreadsheet.*;
导入com.google.gdata.data.spreadsheet.*;
导入com.google.gdata.util.*;
导入java.io.IOException;
导入java.net。*;
导入java.util.*;
公共类MySpreadsheetIntegration{
公共静态void main(字符串[]args)
引发AuthenticationException、MalformedURLException、IOException、ServiceException{
电子表格服务=
新的电子表格服务(“MySpreadsheetIntegration-v1”);
//TODO:为特定用户授权服务对象(请参阅其他部分)
//定义要请求的URL。这永远不会更改。
URL电子表格\u提要\u URL=新URL(
"https://docs.google.com/spreadsheets/d/1OcDp1IZ4iuvyhndtrZ3OOMHZNSEt7XTaaTrhEkNPnN4/edit#gid=0");
//向API发出请求并获取所有电子表格。
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,
电子表格feed.class);
列表电子表格=feed.getEntries();
如果(电子表格.size()==0){
//TODO:没有电子表格,请相应地采取行动。
}
//TODO:根据您的需求更智能地选择电子表格
//应用程序的需求。
电子表格输入电子表格=电子表格.get(0);
System.out.println(电子表格.getTitle().getPlainText());
//获取第一个电子表格的第一个工作表。
//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,行);
}
}
似乎很晚了,但这肯定会帮助其他人!问题在于您的电子表格\u提要\u URL和电子表格服务实例的身份验证,因为该官员尚未共享有关此问题的详细解释。您需要获取身份验证令牌,并将其设置在电子表格服务实例上,如下所示,以使其正常工作:
private void getAuthenticationToken(Activity activity, String accountName){
//Scopes used to get access to google docs and spreadsheets present in the drive
String SCOPE1 = "https://spreadsheets.google.com/feeds";
String SCOPE2 = "https://docs.google.com/feeds";
String scope = "oauth2:" + SCOPE1 + " " + SCOPE2;
String authenticationToken = null;
try {
accessToken= GoogleAuthUtil.getToken(activity, accountName, scope);
}
catch (UserRecoverableAuthException exception){
//For first time, user has to give this permission explicitly
Intent recoveryIntent = exception.getIntent();
startActivityForResult(recoveryIntent, RECOVERY_REQUEST_CODE);
}catch (IOException e) {
e.printStackTrace();
} catch (GoogleAuthException e) {
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_REQUEST_CODE){
if(resultCode == RESULT_OK){
if(data != null){
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null && !accountName.equals("")){
//To be called only for the first time after the permission is given
getAuthenticationToken(activity, accountName);
}
}else {
Utility.showSnackBar(linearLayout, Constants.INTENT_DATA_NULL);
}
}
}
}
最后,下面是获取电子邮件帐户中所有电子表格的代码:
public class MySpreadsheetIntegration {
public void getSpreadSheetEntries()
throws AuthenticationException, MalformedURLException, IOException, ServiceException {
SpreadsheetService service =
new SpreadsheetService("MySpreadsheetIntegration-v1");
service = new SpreadsheetService(applicationName);
service .setProtocolVersion(SpreadsheetService.Versions.V3);
service .setAuthSubToken(accessToken);
// Define the URL to request. This should never change.
URL SPREADSHEET_FEED_URL = new URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
// Make a request to the API and get all spreadsheets.
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
// Iterate through all of the spreadsheets returned
for (SpreadsheetEntry spreadsheet : spreadsheets) {
// Print the title of this spreadsheet to the screen
System.out.println(spreadsheet.getTitle().getPlainText());
}
}
}
公共类MySpreadsheetIntegration{
public void getSpreadSheetEntries()
引发AuthenticationException、MalformedURLException、IOException、ServiceException{
电子表格服务=
新的电子表格服务(“MySpreadsheetIntegration-v1”);
服务=新的电子表格服务(applicationName);
setProtocolVersion(SpreadsheetService.Versions.V3);
setAuthSubToken(accessToken);
//定义要请求的URL。这永远不会更改。
URL电子表格\u提要\u URL=新URL(
"https://spreadsheets.google.com/feeds/spreadsheets/private/full");
//向API发出请求并获取所有电子表格。
SpreadsheetFeed=service.getFeed(电子表格\u feed\u URL,SpreadsheetFeed.class);
列表电子表格=feed.getEntries();
//遍历返回的所有电子表格
用于(电子表格输入电子表格:电子表格){
//将此电子表格的标题打印到屏幕上
System.out.println(电子表格.getTitle().getPlainText());
}
}
}
想展示一下你不工作的代码吗?我基本上复制并粘贴了谷歌的示例代码,但它就在那里。我只是用我想要编辑的电子表格的URL替换了URL。任何人都可以访问和编辑该电子表格。链接示例中的代码是C#代码,而不是java@MarshallTigerus在代码区域的右上角,您可以单击Java选项。之前没有看到该选项卡。我看不出您的代码有任何明显的错误:(