This API allows simplified access to Google Sheets.
In your Gemfile:
gem "google_sheets", "~> VERSION"
- This gem only provides functionality to replace and get spreadsheets.
The rake task uses the following environment variables
- SPREADSHEET_ID=<id_of_spreadsheet>
- KEY_PATH=<path_to_keyfile>
- CSV_PATH=<path_to_csv>
rake replace_spreadsheet
bundle exec rake
To gain access to a spreadsheet in Google Sheets:
- Create a new, empty Google spreadsheet and note the Spreadsheet ID, which can be found as part of the URL.
If the URL is: https://docs.google.com/spreadsheets/d/1UgxVGDt8X0YgB9JYRVQipCqa-nwcnZkKbm0ULOJgEYc/edit#gid=0,
then the Spreadsheet ID would be: 1UgxVGDt8X0YgB9JYRVQipCqa-nwcnZkKbm0ULOJgEYc. - Visit the Google Api Manager at: https://console.cloud.google.com/apis/library.
- Click Library->Sheets API and if necessary create a new project.
- Enable the Google Sheets API.
- Click Credentials -> Create Credentials -> Service Account Key.
- If necessary, create a new 'service account', with a role: Project->Owner.
- Create a new JSON key and save this file somewhere.
- Go back to the Google Spreadsheet and share it with the e-mail address of the service account. This can be found in the Key File under client_email.
To initialise:
sheets = GoogleSheets.new(key_file: , sheet_name: )
- keyfile: path the JSON key file saved in step 7.
- sheet_name(optional): The name of the sheet to use. Defaults to 'Sheet1'.
To get values in a sheet
sheets.get_document(sheet_id:, range: ).values
- sheet_id: The ID of the spreadsheet (step 1).
- range(optional): The range of values in A1 notation: https://developers.google.com/sheets/api/guides/concepts. Defaults to the whole spreadsheet.
To replace a spreadsheet:
sheets.replace_document(sheet_id:, data:, range: )
- sheet_id: The ID of the spreadsheet (step 1).
- data: A two dimensional array of data to upload to the spreadsheet.
- range(optional) Range to replace in A1 notation.
The gem is available as open source under the terms of the MIT License.