问题
I'm sure this is really obvious and easy to do, but I can't seem to find any information on how to do it.
So I have a chrome extension that does some stuff and gathers some data using javascript. That all works fine. I want to write this data into a google spreadhseet. So I used the Google Spreadsheet API to write code in java that would insert rows with data into the appropriate spreadsheet. (The code is basically just the tutorial that is up here https://developers.google.com/google-apps/spreadsheets/#adding_a_list_row with the appropriate OAuth bits to be added in)
How can I get my extension to use this? Do I need to package it as a web app and call it from the extension? Can I write a javascript code that achieves the same thing and package it with the app? Again, sorry if this is really obvious, this is my first time working on web applications so trying to figure it out as I go along. Thanks.
Here is the (stripped down) java code for writing to the spreadheet:
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...");
}
}
The extension popup is just a basic HTML page with a form on it. I have a javascript that writes some details into this form and then the user will manually enter some more. I then want to use the Spreadsheet API to take these value and write them to the appropriate spreadsheet. Which is what the java code above does, but I don't know how to connect the two.
来源:https://stackoverflow.com/questions/15926841/connecting-google-apis-to-chrome-extension