From 3bc05bf76138961f063b3002f5c6c458fc030ac3 Mon Sep 17 00:00:00 2001 From: Kang-Kyu Lee Date: Wed, 31 Dec 2025 13:10:30 -0800 Subject: [PATCH] Use google sheets API append --- lib/gslide/concerns/requests.rb | 2 ++ lib/gslide/models/spreadsheet.rb | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/gslide/concerns/requests.rb b/lib/gslide/concerns/requests.rb index b9055e5..e4f86ff 100644 --- a/lib/gslide/concerns/requests.rb +++ b/lib/gslide/concerns/requests.rb @@ -1,6 +1,8 @@ require "net/http" module Gslide + + # @private module Concerns module Requests def get_request(uri, auth_token: "") diff --git a/lib/gslide/models/spreadsheet.rb b/lib/gslide/models/spreadsheet.rb index 7f6c44d..9ce2553 100644 --- a/lib/gslide/models/spreadsheet.rb +++ b/lib/gslide/models/spreadsheet.rb @@ -40,6 +40,23 @@ def get_sheets parsed_body = get parsed_body[:sheets].collect { |h| Sheet.new(h) } end + + # @param [Hash] options the request body. + # @see https://developers.google.com/workspace/sheets/api/reference/rest/v4/spreadsheets.values/append + def values_append(notation, options = {}) + uri = url_escape_uri(GOOGLE_SHEETS + "/#{@id}/values/'#{notation}':append?valueInputOption=USER_ENTERED") + request_body = options.convert_keys { |k| k.to_s.lower_camel_case }.to_json + + response_body = post_request(uri, auth_token: @auth.token, body: request_body) + response_body + end + + private + + def url_escape_uri(uri_string) + parser = URI::Parser.new + URI parser.escape(uri_string) + end end class SpreadsheetDraft @@ -61,10 +78,11 @@ def create(options = {}) end class Sheet - attr_reader :id, :charts + attr_reader :id, :title, :charts def initialize(options = {}) @id = options[:properties][:sheet_id] + @title = options[:properties][:title] @charts = options[:charts].collect { |h| Chart.new(h) } if options[:charts] end end