将Google API连接到Chrome扩展程序

时间:2013-04-10 13:11:24

标签: google-chrome-extension google-spreadsheet-api

我确信这是非常明显且容易做到的,但我似乎无法找到有关如何操作的任何信息。

所以我有一个chrome扩展程序可以执行一些操作并使用javascript收集一些数据。一切正常。我想把这些数据写入google spreadhseet。因此,我使用Google Spreadsheet API在java中编写代码,该代码会将包含数据的行插入到相应的电子表格中。 (代码基本上就是这里的教程https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row,其中添加了相应的OAuth位)

如何让我的扩展程序使用它?我是否需要将其打包为Web应用程序并从扩展程序中调用它?我可以编写一个实现相同功能的javascript代码并将其与应用程序打包在一起吗?再次,对不起,如果这是非常明显的,这是我第一次在Web应用程序上工作,所以试图在我进行时弄清楚它。感谢。

这是用于写入电子表格的(简化的)java代码:

public class MySpreadsheetIntegration {
      public static void main(String[] args)
          throws AuthenticationException, MalformedURLException, IOException, ServiceException {
           **OAuth stuff all happens first, not shown here as I'm not too concerned about it at the moment***

        SpreadsheetService service =
            new SpreadsheetService("MySpreadsheetIntegration-v1");

        // Define the URL to request.  This should never change.
        URL SPREADSHEET_FEED_URL = new URL(
            "https://docs.google.com/some_url");

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL,
            SpreadsheetFeed.class);
        List<SpreadsheetEntry> spreadsheets = feed.getEntries();

         //this pulls out the first spreadsheet.
        SpreadsheetEntry spreadsheet = spreadsheets.get(0); //spreadsheet is of type List
        //System.out.println(spreadsheet.getTitle().getPlainText());

        // Get the first worksheet of the first spreadsheet.
         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.
        //I think this will give me the headings from the spreadsheet, but insert them manually for now
        URL listFeedUrl = worksheet.getListFeedUrl();
        ListFeed listFeed = service.getFeed(listFeedUrl, ListFeed.class);

        // Create a local representation of the new row.
        ListEntry row = new ListEntry();
        String sDt, sUs, sTy, sAcc, sAccTy, sCon, sNt, sFw, sFwDt;

        sDt = "1/1/2013";
        sUs = "SK";

        row.getCustomElements().setValueLocal("Date", sDt);
        row.getCustomElements().setValueLocal("User", sUs);

        // Send the new row to the API for insertion.
        //this will be inserted at the bottom of the row. 
        //TODO make this get inserted at the top of the worksheet
        row = service.insert(listFeedUrl, row);

        System.out.println("Revoking OAuth Token...");
        oauthHelper.revokeToken(oauthParameters);
        System.out.println("OAuth Token revoked...");

      }
    }

扩展程序弹出窗口只是一个基本的HTML页面,上面有一个表单。我有一个javascript,将一些细节写入此表单,然后用户将手动输入更多。然后,我想使用Spreadsheet API获取这些值并将它们写入相应的电子表格。这是上面的java代码,但我不知道如何连接这两个。

0 个答案:

没有答案