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/validate_api_objects.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,
lib/sheets_v4/api_object_validation/load_schemas.rb,
lib/sheets_v4/api_object_validation/resolve_schema_ref.rb,
lib/sheets_v4/validate_api_objects/validate_api_object.rb,
lib/sheets_v4/api_object_validation/validate_api_object.rb,
lib/sheets_v4/api_object_validation/traverse_object_tree.rb
Overview
Copyright (c) 2022 Yahoo frozen_string_literal: true
Defined Under Namespace
Modules: ApiObjectValidation, GoogleExtensions Classes: Color, ConvertDatesAndTimes, CreateCredential
Constant Summary collapse
- VERSION =
The version of this gem
'0.9.0'
Date and DateTime Conversions collapse
-
.default_spreadsheet_tz
Set the default time zone for date and time conversions.
Validation collapse
-
.api_object_schema_names(logger: Logger.new(nil)) ⇒ Array<String>
List the names of the schemas available to use in the Google Sheets API.
-
.validate_api_object(schema_name:, object:, logger: Logger.new(nil))
Validate the object using the named JSON schema.
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.
146 147 148 |
# File 'lib/sheets_v4.rb', line 146 def default_spreadsheet_tz @default_spreadsheet_tz || raise('default_spreadsheet_tz not set') end |
Class Method Details
.api_object_schema_names(logger: Logger.new(nil)) ⇒ Array<String>
List the names of the schemas available to use in the Google Sheets API
91 92 93 |
# File 'lib/sheets_v4.rb', line 91 def api_object_schema_names(logger: Logger.new(nil)) SheetsV4::ApiObjectValidation::LoadSchemas.new(logger:).call.keys.sort end |
.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.
111 112 113 |
# File 'lib/sheets_v4.rb', line 111 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
122 |
# File 'lib/sheets_v4.rb', line 122 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
.
183 184 185 |
# File 'lib/sheets_v4.rb', line 183 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
222 223 224 |
# File 'lib/sheets_v4.rb', line 222 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
160 161 162 |
# File 'lib/sheets_v4.rb', line 160 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
206 207 208 |
# File 'lib/sheets_v4.rb', line 206 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
.
242 243 244 |
# File 'lib/sheets_v4.rb', line 242 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.
51 52 53 54 55 56 57 58 |
# File 'lib/sheets_v4.rb', line 51 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 |
.validate_api_object(schema_name:, object:, logger: Logger.new(nil))
This method returns an undefined value.
Validate the object using the named JSON schema
The JSON schemas are loaded from the Google Disocvery API. The schemas names are
returned by SheetsV4.api_object_schema_names
.
80 81 82 |
# File 'lib/sheets_v4.rb', line 80 def validate_api_object(schema_name:, object:, logger: Logger.new(nil)) SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger:).call(schema_name:, object:) end |