以编程方式编辑Google电子表格 [英] Programmatically edit a Google Spreadsheet

查看:87
本文介绍了以编程方式编辑Google电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序来接收用户输入,但是现在我希望能够通过每次用户提交表单时编辑Google电子表格来保存输入.因此,基本上,Google电子表格正在不断更新.

I have a written a program that takes in user input, but now I want to be able to save that input by editing a Google spreadsheet every time a user submits the form. So basically, the Google spreadsheet is constantly being updated.

任何人都可以提供有关如何实现此目标的教程吗?我正在使用Eclipse用Java编写程序,那么我需要哪些插件?

Can anyone provide a tutorial on how I might be able to achieve this? I'm writing in Java using Eclipse, so which plug-ins would I need?

我已经尝试使用 Google Spreadsheets API (添加列表行部分),但我似乎无法使它正常工作.

I have already tried using some of the sample code provided in the Google Spreadsheets API (adding a list row section), but I can't seem to get it to work.

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);

  }
}

推荐答案

似乎已经很晚了,但是可以肯定的是这将对其他人有所帮助!问题出在您的SPREADSHEET_FEED_URL和SpreadSheetService实例的身份验证中,因为官方 SpreadSheets Api 尚未共享有关此内容的详细说明.您需要获取身份验证令牌并将其设置在SpreadSheetService实例上,如下所示:

seem to be very late but surely this is going to help others! The problem is in your SPREADSHEET_FEED_URL and authentication of SpreadSheetService instance because the official SpreadSheets Api has not shared detailed explaination regarding that.You need to get an authentication token and set it on SpreadSheetService Instance like below to get it work:

 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);
                        }
                    }
                }
            }

最后是下面的代码,用于通过电子邮件帐户获取所有电子表格:

And finally below code to get all spreadsheets in an email account:

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());
    }
  }
}

这篇关于以编程方式编辑Google电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆