Writing to a Google Sheet from a Google Chrome (content) extension

google-chrome-extension, google-drive-api, google-sheets-api

Solution

Alright, Ill answer now that the question has been cleaned up. You should go for (1), but all 3 are doable. If you must find a reason to do 2 or 3, there are. In particular if you go for (1), the user must be signed-in to Chrome (not the same as signed into gmail). Many users dont get that or even know you can login to chrome or the benefits or separating accounts.

Ive done all 1,2 and 3 (and also the oauth2 flow manually) from a content script because chrome.identity wasnt yet available last year. Its been available for a while now, so yes you can do so from a content script, but the one doing the actual calls need to be the background page. You must put the right "scopes" ("https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds")

and "permissions" ("https://spreadsheets.google.com/", "https://docs.google.com/"), and create the correct client_id in the google console pointing it to the correct extension id. See this s.o. answer (by me :) for issues you might run with the client_id: keep getting kicked out my google login when using chrome.identity api

To write to the spreadsheet you can use row feeds or cell feeds. Make sure to know the pros and cons of each. Update: or use the much improved Sheets API: https://developers.google.com/sheets/

Its a good excercise to get it to work on your own so you know whats going on behing the scenes. If that fails, grab the source code of my "Plus for Trello" extension from github and see what it does.

Problem

I would like to write a Chrome extension that utilises a content script that injects some JavaScript into a specific webpage, scrapes some data using CSS selectors, and finally, stores the data in a Google Sheet. I've written a Chrome extension (content script) that scrapes the HTML page, the area I'm unclear on is how best to insert the data to a Google Sheet. I started reading around the subject and pretty quickly become overwhelmed by the options available and require some clarification and guidance. I have identified three options. Presented at the highest level, they are: - Do everything within the Chrome extension by scraping data with a content script and using an API in the background script. - Perform a HTTP POST to a custom server which writes to the Google Sheet. - Publish the sheet as a web-app with a Google Apps Script 'backend' and HTTP POST to that from the Chrome extension. I favour option 1, since it's self-contained. My questions are: - Have exhausted the solution domain? - I favour option 1, since it's self-contained, is there any good reason to favour options 2 or 3? - Can the Google Spreadsheet API be used directly from a Chrome background script?

Original source