Created
July 9, 2016 01:58
-
-
Save raviwu/7bba40e9bd52f00ef1a1640d2b43ff86 to your computer and use it in GitHub Desktop.
Use Google Sheets API and Service Account to import Data to Rails APP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Gemfile | |
gem 'google-api-client' | |
gem 'googleauth' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'google/apis/sheets_v4' | |
require 'googleauth' | |
class GoogleSheetCrawler | |
def self.get_sheet_array_from_google_sheet(options = {}) | |
service = Google::Apis::SheetsV4::SheetsService.new | |
service.authorization = get_google_auth | |
service.get_spreadsheet_values( | |
"a_sheet_id_you_can_fine_on_google_sheet_url", | |
"SHEETNAME!A2:M2000" | |
).values | |
end | |
private | |
def self.get_google_auth | |
scope = [Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY] | |
file = File.open("YOUR_FILE_PATH_OF_SECRET_JSON_FILE", 'r') | |
authorization = Google::Auth::ServiceAccountCredentials.make_creds({:json_key_io=> file, :scope => scope}) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment