Module: SheetsV4
- Defined in:
- lib/sheets_v4.rb,
lib/sheets_v4/color.rb,
lib/sheets_v4/version.rb,
lib/sheets_v4/create_credential.rb,
lib/sheets_v4/google_extensions.rb,
lib/sheets_v4/api_object_validation.rb,
lib/sheets_v4/convert_dates_and_times.rb,
lib/sheets_v4/google_extensions/sheet.rb,
lib/sheets_v4/google_extensions/spreadsheet.rb,
lib/sheets_v4/google_extensions/sheets_service.rb
Overview
Unofficial helpers for the Google Sheets V4 API
Defined Under Namespace
Modules: ApiObjectValidation, GoogleExtensions Classes: Color, ConvertDatesAndTimes, CreateCredential
Constant Summary collapse
- VERSION =
The version of this gem
'0.10.3'
Date and DateTime Conversions collapse
-
.default_spreadsheet_tz
Set the default time zone for date and time conversions.
Colors collapse
-
.color(name) ⇒ Hash
Given the name of the color, return a Google Sheets API color object.
-
.color_names ⇒ Array<Symbol>
List the names of the colors available to use in the Google Sheets API.
Date and DateTime Conversions collapse
-
.date_to_gs(date) ⇒ Float, String
Convert a Ruby Date object to a Google Sheet date value.
-
.datetime_to_gs(datetime) ⇒ Float, String
Convert a Ruby DateTime object to a Google Sheets value.
-
.default_date_and_time_converter ⇒ SheetsV4::ConvertDatesAndTimes
private
The default converter for dates and times.
-
.gs_to_date(gs_value) ⇒ Date?
Convert a Google Sheets date value to a Ruby Date object.
-
.gs_to_datetime(gs_value) ⇒ DateTime?
Convert a Google Sheets date time value to a DateTime object.
Class Method Summary collapse
-
.sheets_service(credential_source: nil, scopes: nil, credential_creator: SheetsV4::CreateCredential) ⇒ Object
Create a new Google::Apis::SheetsV4::SheetsService object.
Class Attribute Details
.default_spreadsheet_tz
This method returns an undefined value.
Set the default time zone for date and time conversions
Valid time zone names are those listed in one of these two sources:
ActiveSupport::TimeZone.all.map { |tz| tz.tzinfo.name }
ActiveSupport::TimeZone.all.map(&:name)
If you want to set the timezone to the time zone of the system's local time, you could use the timezone_local gem.
110 111 112 |
# File 'lib/sheets_v4.rb', line 110 def default_spreadsheet_tz @default_spreadsheet_tz || raise('default_spreadsheet_tz not set') end |
Class Method Details
.color(name) ⇒ Hash
Given the name of the color, return a Google Sheets API color object
Available color names are listed using SheetsV4.color_names
.
The object returned is frozen. If you want a color you can change (for instance,
to adjust the alpha
attribute), you must clone the object returned.
75 76 77 |
# File 'lib/sheets_v4.rb', line 75 def color(name) SheetsV4::Color::COLORS[name.to_sym] || raise("Color #{name} not found") end |
.color_names ⇒ Array<Symbol>
List the names of the colors available to use in the Google Sheets API
86 |
# File 'lib/sheets_v4.rb', line 86 def color_names = SheetsV4::Color::COLORS.keys |
.date_to_gs(date) ⇒ Float, String
Convert a Ruby Date object to a Google Sheet date value
Uses the time zone given by SheetsV4.default_spreadsheet_tz
.
147 148 149 |
# File 'lib/sheets_v4.rb', line 147 def date_to_gs(date) default_date_and_time_converter.date_to_gs(date) end |
.datetime_to_gs(datetime) ⇒ Float, String
Convert a Ruby DateTime object to a Google Sheets value
186 187 188 |
# File 'lib/sheets_v4.rb', line 186 def datetime_to_gs(datetime) default_date_and_time_converter.datetime_to_gs(datetime) end |
.default_date_and_time_converter ⇒ SheetsV4::ConvertDatesAndTimes
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
The default converter for dates and times
124 125 126 |
# File 'lib/sheets_v4.rb', line 124 def default_date_and_time_converter @default_date_and_time_converter ||= SheetsV4::ConvertDatesAndTimes.new(default_spreadsheet_tz) end |
.gs_to_date(gs_value) ⇒ Date?
Convert a Google Sheets date value to a Ruby Date object
170 171 172 |
# File 'lib/sheets_v4.rb', line 170 def gs_to_date(gs_value) default_date_and_time_converter.gs_to_date(gs_value) end |
.gs_to_datetime(gs_value) ⇒ DateTime?
Convert a Google Sheets date time value to a DateTime object
Time is rounded to the nearest second. The DateTime object returned is in
the time zone given by SheetsV4.default_spreadsheet_tz
.
206 207 208 |
# File 'lib/sheets_v4.rb', line 206 def gs_to_datetime(gs_value) default_date_and_time_converter.gs_to_datetime(gs_value) end |
.sheets_service(credential_source: nil, scopes: nil, credential_creator: SheetsV4::CreateCredential) ⇒ Object
Create a new Google::Apis::SheetsV4::SheetsService object
Simplifies creating and configuring a the credential.
50 51 52 53 54 55 56 57 |
# File 'lib/sheets_v4.rb', line 50 def sheets_service(credential_source: nil, scopes: nil, credential_creator: SheetsV4::CreateCredential) credential_source ||= File.read(File.('~/.google-api-credential.json')) scopes ||= [Google::Apis::SheetsV4::AUTH_SPREADSHEETS] Google::Apis::SheetsV4::SheetsService.new.tap do |service| service. = credential_creator.call(credential_source, scopes) end end |