sheets_v4 0.6.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,32 @@
1
+ # Copyright (c) 2022 Yahoo
2
+ # frozen_string_literal: true
3
+
4
+ require 'google/apis/sheets_v4'
5
+ require 'googleauth'
6
+
7
+ module SheetsV4
8
+ module GoogleExtensions
9
+ # The SheetsService class implements handling credentials on top of the
10
+ # Google::Apis::SheetsV4::SheetsService class.
11
+ #
12
+ # @api public
13
+ #
14
+ module Sheet
15
+ # The sheets_service object used to create this sheet
16
+ #
17
+ # @example
18
+ # sheets_service = sheet.sheets_service
19
+ #
20
+ # @return [Google::Apis::SheetsV4::SheetsService]
21
+ attr_reader :sheets_service
22
+
23
+ # The spreadsheet object that contains this sheet
24
+ #
25
+ # @example
26
+ # spreadsheet = sheet.spreadsheet
27
+ #
28
+ # @return [Google::Apis::SheetsV4::Spreadsheet]
29
+ attr_reader :spreadsheet
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,99 @@
1
+ # Copyright (c) 2022 Yahoo
2
+ # frozen_string_literal: true
3
+
4
+ module SheetsV4
5
+ module GoogleExtensions
6
+ # This module extends the `Google::Apis::SheetsV4::SheetsService` class to add
7
+ # attributes to the `Google::Apis::SheetsV4::Spreadsheet` and `Google::Apis::SheetsV4::Sheet`
8
+ # classes that reference the `SheetsService` instance used to retrieve them.
9
+ #
10
+ # Similarly, an attribute is added to the `Google::Apis::SheetsV4::Sheet` class
11
+ # that references the `Google::Apis::SheetsV4::Spreadsheet` instance that contains it.
12
+ #
13
+ # This allows getting the `SheetsService` object from a spreadsheet or sheet object,
14
+ # making it unnecessary to pass the `SheetsService` object around with the spreadsheet
15
+ # and its sheets.
16
+ #
17
+ # @example
18
+ # require 'sheets_v4/google_extensions'
19
+ # sheets_service = SheetsV4::SheetsService.new
20
+ # spreadsheet = sheets_service.get_spreadsheet('1nT_q0TrQzC3dLZXuI3K9V5P3mArBVZpVd_vRsOpvcyk')
21
+ #
22
+ # spreadsheet.sheets_service == sheets_service # => true
23
+ # spreadsheet.sheets.each do |sheet|
24
+ # sheet.sheets_service == sheets_service # => true
25
+ # sheet.spreadsheet == spreadsheet # => true
26
+ # end
27
+ #
28
+ # @api public
29
+ #
30
+ module SheetsService
31
+ # Replace the prepending class's `get_spreadsheet` implementation
32
+ #
33
+ # When this module is prepended to a class, class's `get_spreadsheet` method
34
+ # is replaced wity `new_get_spreadsheet` method from this module. The class's
35
+ # original `get_spreadsheet` method is renamed to `original_get_spreadsheet`.
36
+ #
37
+ # @example
38
+ # Google::Apis::SheetsV4::SheetsService.prepend(
39
+ # SheetsV4::GoogleExtensions::SheetsService
40
+ # )
41
+ #
42
+ # @return [void]
43
+ #
44
+ # @private
45
+ #
46
+ def self.prepended(prepended_to_class)
47
+ prepended_to_class.send(:alias_method, :original_get_spreadsheet, :get_spreadsheet)
48
+ prepended_to_class.send(:remove_method, :get_spreadsheet)
49
+ prepended_to_class.send(:alias_method, :get_spreadsheet, :new_get_spreadsheet)
50
+ end
51
+
52
+ # @!method get_spreadsheet(spreadsheet_id, include_grid_data, ranges, fields, quota_user, options, &block)
53
+ #
54
+ # @api public
55
+ #
56
+ # Gets an existing spreadsheet
57
+ #
58
+ # Creates a spreadsheet object by calling the original
59
+ # Google::Apis::SheetsV4::SheetsService#get_spreadsheet method and then does
60
+ # the following:
61
+ #
62
+ # * Sets the `sheets_service` attribute for the returned spreadsheet.
63
+ # * Sets the `sheets_service` and `spreadsheet` attributes all the sheets contained in the spreadsheet.
64
+ #
65
+ # See the documentation for Google::Apis::SheetsV4::SheetsService#get_spreadsheet for
66
+ # details on the parameters and return value.
67
+ #
68
+ # @example Get a spreadsheet object and output new attributes:
69
+ # require 'sheets_v4'
70
+ # require 'sheets_v4/google_extensions'
71
+ #
72
+ # sheets_service = SheetsV4::SheetsService.new
73
+ # spreadsheet_id = '1nT_q0TrQzC3dLZXuI3K9V5P3mArBVZpVd_vRsOpvcyk'
74
+ #
75
+ # spreadsheet = sheets_service.get_spreadsheet(spreadsheet_id)
76
+ #
77
+ # @return [Google::Apis::SheetsV4::Spreadsheet] the spreadsheet whose ID is `spreadsheet_id`
78
+
79
+ # Replaces the `get_spreadsheet` method implementation in the prepended class
80
+ #
81
+ # @example
82
+ # spreadsheet = sheets_service.new_get_spreadsheet(spreadsheet_id)
83
+ #
84
+ # @private
85
+ #
86
+ # @return [Google::Apis::SheetsV4::Spreadsheet]
87
+ #
88
+ def new_get_spreadsheet(...)
89
+ original_get_spreadsheet(...)&.tap do |spreadsheet|
90
+ spreadsheet.instance_variable_set(:@sheets_service, self)
91
+ spreadsheet.sheets.each do |sheet|
92
+ sheet.instance_variable_set(:@sheets_service, self)
93
+ sheet.instance_variable_set(:@spreadsheet, spreadsheet)
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2022 Yahoo
2
+ # frozen_string_literal: true
3
+
4
+ require 'google/apis/sheets_v4'
5
+ require 'googleauth'
6
+
7
+ module SheetsV4
8
+ module GoogleExtensions
9
+ # The SheetsService class implements handling credentials on top of the
10
+ # Google::Apis::SheetsV4::SheetsService class.
11
+ #
12
+ # @api public
13
+ #
14
+ module Spreadsheet
15
+ # The sheets_service object used to create this spreadsheet
16
+ #
17
+ # @example
18
+ # sheets_service = spreadsheet.sheets_service
19
+ #
20
+ # @return [Google::Apis::SheetsV4::SheetsService]
21
+ attr_reader :sheets_service
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SheetsV4
4
+ # The Google extensions are additions directly to Google::Apis::SheetsV4 classes
5
+ #
6
+ # These additions are optional and provide convenience methods and attributes
7
+ # that simplify use of the Google Sheets API.
8
+ #
9
+ # To use these extensions, require the `sheets_v4/google_extensions` file.
10
+ #
11
+ # @example
12
+ # require 'sheets_v4/google_extensions'
13
+ #
14
+ module GoogleExtensions; end
15
+ end
16
+
17
+ require_relative 'google_extensions/sheets_service'
18
+ require_relative 'google_extensions/spreadsheet'
19
+ require_relative 'google_extensions/sheet'
20
+
21
+ # @private
22
+ module Google
23
+ module Apis
24
+ # Add SheetsV4 extensions to Google::Apis::SheetsV4 classes
25
+ module SheetsV4
26
+ # Add SheetsV4 extensions to Google::Apis::SheetsV4::SheetsService
27
+ class SheetsService
28
+ prepend ::SheetsV4::GoogleExtensions::SheetsService
29
+ end
30
+
31
+ # Add SheetsV4 extensions to Google::Apis::SheetsV4::Spreadsheet
32
+ class Spreadsheet
33
+ prepend ::SheetsV4::GoogleExtensions::Spreadsheet
34
+ end
35
+
36
+ # Add SheetsV4 extensions to Google::Apis::SheetsV4::Sheet
37
+ class Sheet
38
+ prepend ::SheetsV4::GoogleExtensions::Sheet
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,21 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'active_support'
3
4
  require 'active_support/inflector'
4
5
  require 'json_schemer'
5
6
 
6
7
  module SheetsV4
7
- module ValidateApiObjects
8
+ module ApiObjectValidation
8
9
  # Validate objects against a Google Sheets API request object schema
9
10
  #
10
11
  # @api public
11
12
  #
12
- class Validate
13
+ class ValidateApiObject
13
14
  # Create a new validator
14
15
  #
15
16
  # By default, a nil logger is used. This means that no messages are logged.
16
17
  #
17
18
  # @example
18
- # validator = SheetsV4::ValidateApiObjects::Validator.new
19
+ # validator = SheetsV4::ApiObjectValidation::ValidateApiObject.new
19
20
  #
20
21
  # @param logger [Logger] the logger to use
21
22
  #
@@ -30,7 +31,7 @@ module SheetsV4
30
31
  #
31
32
  # @example
32
33
  # logger = Logger.new(STDOUT, :level => Logger::INFO)
33
- # validator = SheetsV4::ValidateApiObjects::Validator.new(logger)
34
+ # validator = SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger)
34
35
  # validator.logger == logger # => true
35
36
  # validator.logger.debug { "Debug message" }
36
37
  #
@@ -43,7 +44,7 @@ module SheetsV4
43
44
  # @example
44
45
  # schema_name = 'batch_update_spreadsheet_request'
45
46
  # object = { 'requests' => [] }
46
- # validator = SheetsV4::ValidateApiObjects::Validator.new
47
+ # validator = SheetsV4::ApiObjectValidation::ValidateApiObject.new
47
48
  # validator.call(schema_name:, object:)
48
49
  #
49
50
  # @param schema_name [String] the name of the schema to validate against
@@ -69,7 +70,7 @@ module SheetsV4
69
70
  # The resolver to use to resolve JSON schema references
70
71
  # @return [ResolveSchemaRef]
71
72
  # @api private
72
- def ref_resolver = @ref_resolver ||= SheetsV4::ValidateApiObjects::ResolveSchemaRef.new(logger:)
73
+ def ref_resolver = @ref_resolver ||= SheetsV4::ApiObjectValidation::ResolveSchemaRef.new(logger:)
73
74
 
74
75
  # Raise an error when the object does not conform to the schema
75
76
  # @return [void]
@@ -7,14 +7,14 @@ module SheetsV4
7
7
  # logger = Logger.new(STDOUT, :level => Logger::ERROR)
8
8
  # schema_name = 'batch_update_spreadsheet_request'
9
9
  # object = { 'requests' => [] }
10
- # SheetsV4::ValidateApiObjects::Validator.new(logger:).call(schema_name:, object:)
10
+ # SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger:).call(schema_name:, object:)
11
11
  #
12
12
  # @api public
13
13
  #
14
- module ValidateApiObjects; end
14
+ module ApiObjectValidation; end
15
15
  end
16
16
 
17
- require_relative 'validate_api_objects/load_schemas'
18
- require_relative 'validate_api_objects/resolve_schema_ref'
19
- require_relative 'validate_api_objects/traverse_object_tree'
20
- require_relative 'validate_api_objects/validate'
17
+ require_relative 'api_object_validation/load_schemas'
18
+ require_relative 'api_object_validation/resolve_schema_ref'
19
+ require_relative 'api_object_validation/traverse_object_tree'
20
+ require_relative 'api_object_validation/validate_api_object'
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SheetsV4
4
4
  # The version of this gem
5
- VERSION = '0.6.0'
5
+ VERSION = '0.8.0'
6
6
  end
data/lib/sheets_v4.rb CHANGED
@@ -2,9 +2,14 @@
2
2
 
3
3
  require_relative 'sheets_v4/version'
4
4
  require_relative 'sheets_v4/color'
5
- require_relative 'sheets_v4/credential_creator'
6
- require_relative 'sheets_v4/validate_api_objects'
5
+ require_relative 'sheets_v4/convert_dates_and_times'
6
+ require_relative 'sheets_v4/create_credential'
7
+ require_relative 'sheets_v4/api_object_validation'
7
8
 
9
+ require 'active_support'
10
+ require 'active_support/values/time_zone'
11
+ require 'active_support/core_ext/numeric/time'
12
+ require 'active_support/core_ext/string/zones'
8
13
  require 'google/apis/sheets_v4'
9
14
  require 'json'
10
15
  require 'logger'
@@ -15,102 +20,227 @@ require 'net/http'
15
20
  # @api public
16
21
  #
17
22
  module SheetsV4
18
- # Create a new Google::Apis::SheetsV4::SheetsService object
19
- #
20
- # Simplifies creating and configuring a the credential.
21
- #
22
- # @example using the credential in `~/.google-api-credential`
23
- # SheetsV4.sheets_service
24
- #
25
- # @example using a credential passed in as a string
26
- # credential_source = File.read(File.expand_path('~/.google-api-credential.json'))
27
- # SheetsV4.sheets_service(credential_source:)
28
- #
29
- # @example using a credential passed in as an IO
30
- # credential_source = File.open(File.expand_path('~/.google-api-credential.json'))
31
- # SheetsV4.sheets_service(credential_source:)
32
- #
33
- # @param credential_source [nil, String, IO, Google::Auth::*] may
34
- # be either an already constructed credential, the credential read into a String or
35
- # an open file with the credential ready to be read. Passing `nil` will result
36
- # in the credential being read from `~/.google-api-credential.json`.
37
- #
38
- # @param scopes [Object, Array] one or more scopes to access.
39
- #
40
- # @param credential_creator [#credential] Used to inject the credential creator for
41
- # testing.
42
- #
43
- # @return a new SheetsService instance
44
- #
45
- def self.sheets_service(credential_source: nil, scopes: nil, credential_creator: SheetsV4::CredentialCreator)
46
- credential_source ||= File.read(File.expand_path('~/.google-api-credential.json'))
47
- scopes ||= [Google::Apis::SheetsV4::AUTH_SPREADSHEETS]
48
-
49
- Google::Apis::SheetsV4::SheetsService.new.tap do |service|
50
- service.authorization = credential_creator.call(credential_source, scopes)
23
+ class << self
24
+ # Create a new Google::Apis::SheetsV4::SheetsService object
25
+ #
26
+ # Simplifies creating and configuring a the credential.
27
+ #
28
+ # @example using the credential in `~/.google-api-credential`
29
+ # SheetsV4.sheets_service
30
+ #
31
+ # @example using a credential passed in as a string
32
+ # credential_source = File.read(File.expand_path('~/.google-api-credential.json'))
33
+ # SheetsV4.sheets_service(credential_source:)
34
+ #
35
+ # @example using a credential passed in as an IO
36
+ # credential_source = File.open(File.expand_path('~/.google-api-credential.json'))
37
+ # SheetsV4.sheets_service(credential_source:)
38
+ #
39
+ # @param credential_source [nil, String, IO, Google::Auth::*] may
40
+ # be either an already constructed credential, the credential read into a String or
41
+ # an open file with the credential ready to be read. Passing `nil` will result
42
+ # in the credential being read from `~/.google-api-credential.json`.
43
+ #
44
+ # @param scopes [Object, Array] one or more scopes to access.
45
+ #
46
+ # @param credential_creator [#credential] Used to inject the credential creator for
47
+ # testing.
48
+ #
49
+ # @return a new SheetsService instance
50
+ #
51
+ def sheets_service(credential_source: nil, scopes: nil, credential_creator: SheetsV4::CreateCredential)
52
+ credential_source ||= File.read(File.expand_path('~/.google-api-credential.json'))
53
+ scopes ||= [Google::Apis::SheetsV4::AUTH_SPREADSHEETS]
54
+
55
+ Google::Apis::SheetsV4::SheetsService.new.tap do |service|
56
+ service.authorization = credential_creator.call(credential_source, scopes)
57
+ end
51
58
  end
52
- end
53
59
 
54
- # Validate the object using the named JSON schema
55
- #
56
- # The JSON schemas are loaded from the Google Disocvery API. The schemas names are
57
- # returned by `SheetsV4.api_object_schema_names`.
58
- #
59
- # @example
60
- # schema_name = 'batch_update_spreadsheet_request'
61
- # object = { 'requests' => [] }
62
- # SheetsV4.validate_api_object(schema_name:, object:)
63
- #
64
- # @param schema_name [String] the name of the schema to validate against
65
- # @param object [Object] the object to validate
66
- # @param logger [Logger] the logger to use for logging error, info, and debug message
67
- #
68
- # @raise [RuntimeError] if the object does not conform to the schema
69
- #
70
- # @return [void]
71
- #
72
- def self.validate_api_object(schema_name:, object:, logger: Logger.new(nil))
73
- SheetsV4::ValidateApiObjects::Validate.new(logger:).call(schema_name:, object:)
74
- end
60
+ # @!group Validation
75
61
 
76
- # List the names of the schemas available to use in the Google Sheets API
77
- #
78
- # @example List the name of the schemas available
79
- # SheetsV4.api_object_schema_names #=> ["add_banding_request", "add_banding_response", ...]
80
- #
81
- # @return [Array<String>] the names of the schemas available
82
- #
83
- def self.api_object_schema_names(logger: Logger.new(nil))
84
- SheetsV4::ValidateApiObjects::LoadSchemas.new(logger:).call.keys.sort
85
- end
62
+ # Validate the object using the named JSON schema
63
+ #
64
+ # The JSON schemas are loaded from the Google Disocvery API. The schemas names are
65
+ # returned by `SheetsV4.api_object_schema_names`.
66
+ #
67
+ # @example
68
+ # schema_name = 'batch_update_spreadsheet_request'
69
+ # object = { 'requests' => [] }
70
+ # SheetsV4.validate_api_object(schema_name:, object:)
71
+ #
72
+ # @param schema_name [String] the name of the schema to validate against
73
+ # @param object [Object] the object to validate
74
+ # @param logger [Logger] the logger to use for logging error, info, and debug message
75
+ #
76
+ # @raise [RuntimeError] if the object does not conform to the schema
77
+ #
78
+ # @return [void]
79
+ #
80
+ def validate_api_object(schema_name:, object:, logger: Logger.new(nil))
81
+ SheetsV4::ApiObjectValidation::ValidateApiObject.new(logger:).call(schema_name:, object:)
82
+ end
86
83
 
87
- # Given the name of the color, return a Google Sheets API color object
88
- #
89
- # Available color names are listed using `SheetsV4.color_names`.
90
- #
91
- # The object returned is frozen. If you want a color you can change (for instance,
92
- # to adjust the `alpha` attribute), you must clone the object returned.
93
- #
94
- # @example
95
- # SheetsV4::Color.color(:red) #=> { "red": 1.0, "green": 0.0, "blue": 0.0 }
96
- #
97
- # @param name [#to_sym] the name of the color requested
98
- #
99
- # @return [Hash] The color requested e.g. `{ "red": 1.0, "green": 0.0, "blue": 0.0 }`
100
- #
101
- # @api public
102
- #
103
- def self.color(name)
104
- SheetsV4::Color::COLORS[name.to_sym] || raise("Color #{name} not found")
105
- end
84
+ # List the names of the schemas available to use in the Google Sheets API
85
+ #
86
+ # @example List the name of the schemas available
87
+ # SheetsV4.api_object_schema_names #=> ["add_banding_request", "add_banding_response", ...]
88
+ #
89
+ # @return [Array<String>] the names of the schemas available
90
+ #
91
+ def api_object_schema_names(logger: Logger.new(nil))
92
+ SheetsV4::ApiObjectValidation::LoadSchemas.new(logger:).call.keys.sort
93
+ end
94
+
95
+ # @!group Colors
96
+
97
+ # Given the name of the color, return a Google Sheets API color object
98
+ #
99
+ # Available color names are listed using `SheetsV4.color_names`.
100
+ #
101
+ # The object returned is frozen. If you want a color you can change (for instance,
102
+ # to adjust the `alpha` attribute), you must clone the object returned.
103
+ #
104
+ # @example
105
+ # SheetsV4::Color.color(:red) #=> { "red": 1.0, "green": 0.0, "blue": 0.0 }
106
+ #
107
+ # @param name [#to_sym] the name of the color requested
108
+ #
109
+ # @return [Hash] The color requested e.g. `{ "red": 1.0, "green": 0.0, "blue": 0.0 }`
110
+ #
111
+ def color(name)
112
+ SheetsV4::Color::COLORS[name.to_sym] || raise("Color #{name} not found")
113
+ end
114
+
115
+ # List the names of the colors available to use in the Google Sheets API
116
+ #
117
+ # @example
118
+ # SheetsV4::Color.color_names #=> [:black, :white, :red, :green, :blue, :yellow, :magenta, :cyan, ...]
119
+ #
120
+ # @return [Array<Symbol>] the names of the colors available
121
+ #
122
+ def color_names = SheetsV4::Color::COLORS.keys
123
+
124
+ # @!group Date and DateTime Conversions
125
+
126
+ # @!attribute [rw] default_spreadsheet_tz
127
+ #
128
+ # Set the default time zone for date and time conversions
129
+ #
130
+ # Valid time zone names are those listed in one of these two sources:
131
+ # * `ActiveSupport::TimeZone.all.map { |tz| tz.tzinfo.name }`
132
+ # * `ActiveSupport::TimeZone.all.map(&:name)`
133
+ #
134
+ # If you want to set the timezone to the time zone of the system's local time,
135
+ # you could use the [timezone_local gem](https://rubygems.org/gems/timezone_local).
136
+ #
137
+ # @example
138
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
139
+ #
140
+ # @example Set the time zone to the system's local time
141
+ # require 'timezone_local'
142
+ # SheetsV4.default_spreadsheet_tz = TimeZone::Local.get.name
143
+ #
144
+ # @return [void]
145
+ #
146
+ def default_spreadsheet_tz
147
+ @default_spreadsheet_tz || raise('default_spreadsheet_tz not set')
148
+ end
106
149
 
107
- # List the names of the colors available to use in the Google Sheets API
108
- #
109
- # @example
110
- # SheetsV4::Color.color_names #=> [:black, :white, :red, :green, :blue, :yellow, :magenta, :cyan, ...]
111
- #
112
- # @return [Array<Symbol>] the names of the colors available
113
- # @api public
114
- #
115
- def self.color_names = SheetsV4::Color::COLORS.keys
150
+ def default_spreadsheet_tz=(time_zone)
151
+ raise "Invalid time zone '#{time_zone}'" unless ActiveSupport::TimeZone.new(time_zone)
152
+
153
+ @default_date_and_time_converter = nil unless @default_spreadsheet_tz == time_zone
154
+ @default_spreadsheet_tz = time_zone
155
+ end
156
+
157
+ # The default converter for dates and times
158
+ # @return [SheetsV4::ConvertDatesAndTimes]
159
+ # @api private
160
+ def default_date_and_time_converter
161
+ @default_date_and_time_converter ||= SheetsV4::ConvertDatesAndTimes.new(default_spreadsheet_tz)
162
+ end
163
+
164
+ # Convert a Ruby Date object to a Google Sheet date value
165
+ #
166
+ # Uses the time zone given by `SheetsV4.default_spreadsheet_tz`.
167
+ #
168
+ # @example with a Date object
169
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
170
+ # date = Date.parse('2021-05-17')
171
+ # SheetsV4.date_to_gs(date) #=> 44333
172
+ #
173
+ # @example with a DateTime object
174
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
175
+ # date_time = DateTime.parse('2021-05-17 11:36:00 UTC')
176
+ # SheetsV4.date_to_gs(date_time) #=> 44333
177
+ #
178
+ # @param date [DateTime, Date, nil] the date to convert
179
+ #
180
+ # @return [Float, String] the value to sstore in a Google Sheet cell
181
+ # Returns a Float if date is not nil; otherwise, returns an empty string
182
+ #
183
+ def date_to_gs(date)
184
+ default_date_and_time_converter.date_to_gs(date)
185
+ end
186
+
187
+ # Convert a Google Sheets date value to a Ruby Date object
188
+ #
189
+ # @example with a date value
190
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
191
+ # gs_value = 44333
192
+ # SheetsV4.gs_to_date(gs_value) #=> #<Date: 2021-05-17 ...>
193
+ #
194
+ # @example with a date and time value
195
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
196
+ # gs_value = 44333.191666666666
197
+ # SheetsV4.gs_to_date(gs_value) #=> #<Date: 2021-05-17 ...>
198
+ #
199
+ # @param gs_value [Float, "", nil] the value from the Google Sheets cell
200
+ #
201
+ # @return [Date, nil] the value represented by gs_date
202
+ #
203
+ # Returns a Date object if a Float was given; otherwise, returns nil if an
204
+ # empty string or nil was given.
205
+ #
206
+ def gs_to_date(gs_value)
207
+ default_date_and_time_converter.gs_to_date(gs_value)
208
+ end
209
+
210
+ # Convert a Ruby DateTime object to a Google Sheets value
211
+ #
212
+ # @example
213
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
214
+ # date_time = DateTime.parse('2021-05-17 11:36:00 UTC')
215
+ # SheetsV4.datetime_to_gs(date_time) #=> 44333.191666666666
216
+ #
217
+ # @param datetime [DateTime, nil] the date and time to convert
218
+ #
219
+ # @return [Float, String] the value to store in a Google Sheet cell
220
+ # Returns a Float if datetime is not nil; otherwise, returns an empty string.
221
+ #
222
+ def datetime_to_gs(datetime)
223
+ default_date_and_time_converter.datetime_to_gs(datetime)
224
+ end
225
+
226
+ # Convert a Google Sheets date time value to a DateTime object
227
+ #
228
+ # Time is rounded to the nearest second. The DateTime object returned is in
229
+ # the time zone given by `SheetsV4.default_spreadsheet_tz`.
230
+ #
231
+ # @example
232
+ # SheetsV4.default_spreadsheet_tz = 'America/Los_Angeles'
233
+ # gs_value = 44333.191666666666
234
+ # SheetsV4.gs_to_datetime(gs_value) #=> #<DateTime: 2021-05-17T04:35:59-07:00 ...>
235
+ #
236
+ # @param gs_value [Float, "", nil] the value from the Google Sheets cell
237
+ #
238
+ # @return [DateTime, nil] the value represented by gs_datetime
239
+ # Returns a DateTime object if a Float was given; otherwise, returns nil if an
240
+ # empty string or nil was given.
241
+ #
242
+ def gs_to_datetime(gs_value)
243
+ default_date_and_time_converter.gs_to_datetime(gs_value)
244
+ end
245
+ end
116
246
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sheets_v4
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Couball
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-10-04 00:00:00.000000000 Z
11
+ date: 2023-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler-audit
@@ -260,13 +260,20 @@ files:
260
260
  - examples/set_background_color
261
261
  - examples/set_background_color2
262
262
  - lib/sheets_v4.rb
263
+ - lib/sheets_v4/api_object_validation.rb
264
+ - lib/sheets_v4/api_object_validation/load_schemas.rb
265
+ - lib/sheets_v4/api_object_validation/resolve_schema_ref.rb
266
+ - lib/sheets_v4/api_object_validation/traverse_object_tree.rb
267
+ - lib/sheets_v4/api_object_validation/validate_api_object.rb
263
268
  - lib/sheets_v4/color.rb
264
- - lib/sheets_v4/credential_creator.rb
269
+ - lib/sheets_v4/convert_dates_and_times.rb
270
+ - lib/sheets_v4/create_credential.rb
271
+ - lib/sheets_v4/google_extensions.rb
272
+ - lib/sheets_v4/google_extensions/sheet.rb
273
+ - lib/sheets_v4/google_extensions/sheets_service.rb
274
+ - lib/sheets_v4/google_extensions/spreadsheet.rb
265
275
  - lib/sheets_v4/validate_api_objects.rb
266
- - lib/sheets_v4/validate_api_objects/load_schemas.rb
267
- - lib/sheets_v4/validate_api_objects/resolve_schema_ref.rb
268
- - lib/sheets_v4/validate_api_objects/traverse_object_tree.rb
269
- - lib/sheets_v4/validate_api_objects/validate.rb
276
+ - lib/sheets_v4/validate_api_objects/validate_api_object.rb
270
277
  - lib/sheets_v4/version.rb
271
278
  homepage: https://github.com/main-branch/sheets_v4
272
279
  licenses: