From 30729f851008f56dfbd573b187ef6dda38241eca Mon Sep 17 00:00:00 2001 From: yuya Date: Thu, 13 Oct 2016 23:31:10 +0900 Subject: [PATCH 1/9] Add sheets function "add_worksheet_from_template" --- lib/google_drive/spreadsheet.rb | 38 +++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index b9cc7ea3..cb47e79d 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -66,6 +66,44 @@ def worksheet_by_gid(gid) worksheets.find { |ws| ws.gid == gid } end + def add_worksheet_from_template(title) + # get access token and client_key(id) from @session + byebug + auth_data = @session.drive.request_options.authorization + access_token = auth_data.access_token # client_id + client_id = auth_data.client_id + + # find template key + template_sheet = worksheet_by_title('template') + template_sheet_id = template_sheet.gid + # set sheet key + spreadsheetId = template_sheet.spreadsheet.api_file.id + + # create new work sheet from (template)key + # set uri + url = "https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheetId}/sheets/#{template_sheet_id}:copyTo?key=#{client_id}" + uri = URI.parse(url) + + # create sheme + header = { + 'Authorization' => "Bearer #{access_token}", + 'Content-Type' => 'application/json' + } + + body = { + 'destinationSpreadsheetId' => spreadsheetId + }.to_json + + https = Net::HTTP.new(uri.host, uri.port) + https.use_ssl = true + req = Net::HTTP::Post.new(uri.path, header) + req.body = body + p res = https.request(req) + # return ss + new_sheet_id = JSON::parse(res.body)['sheetId'] + worksheet_by_gid(new_sheet_id) + end + # Adds a new worksheet to the spreadsheet. Returns added GoogleDrive::Worksheet. def add_worksheet(title, max_rows = 100, max_cols = 20) xml = <<-"EOS" From b5eeb5b8266fdac29952a250962e22ebc7237dd8 Mon Sep 17 00:00:00 2001 From: yuya Date: Thu, 13 Oct 2016 23:40:08 +0900 Subject: [PATCH 2/9] Edit README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 1eecdcbf..27aa6c52 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +# 追加メソット +GoogleDrive::Spreadsheet#add_worksheet_from_template(name) +シートを追加するときにtemplateという名前があればそこからコピーして新たに作る。 + + This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs. NOTE: This is NOT a library to create Google Drive App. @@ -120,3 +125,5 @@ Ruby 2.0.0 or later. Checked with Ruby 2.3.0. ## Author [Hiroshi Ichikawa](http://gimite.net/en/index.php?Contact) + + From 8c8613a8f556271838b4f6eacc8d6c2fbc1cd11e Mon Sep 17 00:00:00 2001 From: yuya Date: Fri, 14 Oct 2016 12:56:39 +0900 Subject: [PATCH 3/9] set title in add_worksheet_from_template --- README.md | 1 - lib/google_drive/spreadsheet.rb | 11 ++++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 27aa6c52..3cc90767 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ GoogleDrive::Spreadsheet#add_worksheet_from_template(name) シートを追加するときにtemplateという名前があればそこからコピーして新たに作る。 - This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs. NOTE: This is NOT a library to create Google Drive App. diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index cb47e79d..49c09bd9 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -68,7 +68,6 @@ def worksheet_by_gid(gid) def add_worksheet_from_template(title) # get access token and client_key(id) from @session - byebug auth_data = @session.drive.request_options.authorization access_token = auth_data.access_token # client_id client_id = auth_data.client_id @@ -99,9 +98,15 @@ def add_worksheet_from_template(title) req = Net::HTTP::Post.new(uri.path, header) req.body = body p res = https.request(req) - # return ss new_sheet_id = JSON::parse(res.body)['sheetId'] - worksheet_by_gid(new_sheet_id) + # select new worksheet + new_work_sheet = worksheet_by_gid(new_sheet_id) + + # set title + new_work_sheet.title = title + new_work_sheet.save + + new_work_sheet end # Adds a new worksheet to the spreadsheet. Returns added GoogleDrive::Worksheet. From 2dc7ce324d6621dc47fb28cd5168648af1a7a9e3 Mon Sep 17 00:00:00 2001 From: yuya Date: Fri, 14 Oct 2016 13:17:57 +0900 Subject: [PATCH 4/9] edit readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3cc90767..ed76af7b 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # 追加メソット -GoogleDrive::Spreadsheet#add_worksheet_from_template(name) -シートを追加するときにtemplateという名前があればそこからコピーして新たに作る。 +``GoogleDrive::Spreadsheet#add_worksheet_from_template(name)`` +シートを追加するときにtemplateという名前があればそこからコピーして新たに作る。 This is a Ruby library to read/write files/spreadsheets in Google Drive/Docs. From f12bd25b7e7705e93b39ea8e9299cabac0942f67 Mon Sep 17 00:00:00 2001 From: yuya Date: Tue, 1 Nov 2016 16:25:15 +0900 Subject: [PATCH 5/9] Create SpreadsheetsApi class --- lib/api/spreadsheets.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lib/api/spreadsheets.rb diff --git a/lib/api/spreadsheets.rb b/lib/api/spreadsheets.rb new file mode 100644 index 00000000..abc3782e --- /dev/null +++ b/lib/api/spreadsheets.rb @@ -0,0 +1,33 @@ +class SpreadsheetsApi + def initialize(access_token, client_id) + @access_token = access_token + @client_id = client_id + end + + # https://developers.google.com/sheets/reference/rest/v4/spreadsheets.sheets/copyTo + def copy_to(spreadsheet_id, template_sheet_id, destination_spreadsheet_id) + url = "https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheet_id}/sheets/#{template_sheet_id}:copyTo?key=#{@client_id}" + uri = URI.parse(url) + body = { + 'destinationSpreadsheetId' => destination_spreadsheet_id + }.to_json + + post(uri, body) + end + + private + def post(uri, body) + https = Net::HTTP.new(uri.host, uri.port) + https.use_ssl = true + req = Net::HTTP::Post.new(uri.path, header) + req.body = body + https.request(req) # return response + end + + def header + { + 'Authorization' => "Bearer #{@access_token}", + 'Content-Type' => 'application/json' + } + end +end From ba5d1458ffe898153eb304e7810a37f62157c9f0 Mon Sep 17 00:00:00 2001 From: yuya Date: Tue, 1 Nov 2016 16:26:49 +0900 Subject: [PATCH 6/9] Create API instance --- lib/google_drive/file.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/google_drive/file.rb b/lib/google_drive/file.rb index 90a284fc..2af634bf 100644 --- a/lib/google_drive/file.rb +++ b/lib/google_drive/file.rb @@ -7,6 +7,7 @@ require 'google_drive/util' require 'google_drive/acl' +require 'api/spreadsheets' module GoogleDrive # A file in Google Drive, including Google Docs document/spreadsheet/presentation. @@ -28,6 +29,10 @@ def initialize(session, api_file) #:nodoc: @api_file = api_file @acl = nil delegate_api_methods(self, @api_file, [:title]) + auth_data = @session.drive.request_options.authorization + access_token = auth_data.access_token # client_id + client_id = auth_data.client_id + @sheet_api = SpreadsheetsApi.new(access_token, client_id) end # Wrapped Google::APIClient::Schema::Drive::V3::File object. From dae95e5c304f211ed0a15c9094cb26c3baf3dab0 Mon Sep 17 00:00:00 2001 From: yuya Date: Tue, 1 Nov 2016 16:27:41 +0900 Subject: [PATCH 7/9] Refactoring --- lib/google_drive/spreadsheet.rb | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index 49c09bd9..b96a3788 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -67,37 +67,12 @@ def worksheet_by_gid(gid) end def add_worksheet_from_template(title) - # get access token and client_key(id) from @session - auth_data = @session.drive.request_options.authorization - access_token = auth_data.access_token # client_id - client_id = auth_data.client_id - # find template key template_sheet = worksheet_by_title('template') template_sheet_id = template_sheet.gid # set sheet key - spreadsheetId = template_sheet.spreadsheet.api_file.id - - # create new work sheet from (template)key - # set uri - url = "https://sheets.googleapis.com/v4/spreadsheets/#{spreadsheetId}/sheets/#{template_sheet_id}:copyTo?key=#{client_id}" - uri = URI.parse(url) - - # create sheme - header = { - 'Authorization' => "Bearer #{access_token}", - 'Content-Type' => 'application/json' - } - - body = { - 'destinationSpreadsheetId' => spreadsheetId - }.to_json - - https = Net::HTTP.new(uri.host, uri.port) - https.use_ssl = true - req = Net::HTTP::Post.new(uri.path, header) - req.body = body - p res = https.request(req) + spreadsheet_id = template_sheet.spreadsheet.api_file.id + res = @sheet_api.copy_to(spreadsheet_id, template_sheet_id, spreadsheet_id) new_sheet_id = JSON::parse(res.body)['sheetId'] # select new worksheet new_work_sheet = worksheet_by_gid(new_sheet_id) From f455594268836879d00ca75be1b335d7e348a022 Mon Sep 17 00:00:00 2001 From: yuya Date: Tue, 1 Nov 2016 16:29:42 +0900 Subject: [PATCH 8/9] Add copy_to method --- lib/google_drive/spreadsheet.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index b96a3788..f45c4656 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -97,5 +97,12 @@ def add_worksheet(title, max_rows = 100, max_cols = 20) doc = @session.request(:post, worksheets_feed_url, data: xml) Worksheet.new(@session, self, doc.root) end + + # https://developers.google.com/sheets/reference/rest/v4/spreadsheets.sheets/copyTo + def copy_to(spreadsheet_id, sheet_id, destination_spreadsheet_id) + res = @sheet_api.copy_to(spreadsheet_id, sheet_id, destination_spreadsheet_id) + new_sheet_id = JSON::parse(res.body)['sheetId'] + worksheet_by_gid(new_sheet_id) # return new worksheet + end end end From 68b6ed889c3138e6cb741a0a24659f719064d2ca Mon Sep 17 00:00:00 2001 From: yuya Date: Tue, 1 Nov 2016 16:40:57 +0900 Subject: [PATCH 9/9] Refactoring --- lib/google_drive/spreadsheet.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google_drive/spreadsheet.rb b/lib/google_drive/spreadsheet.rb index f45c4656..921f7d74 100644 --- a/lib/google_drive/spreadsheet.rb +++ b/lib/google_drive/spreadsheet.rb @@ -66,9 +66,9 @@ def worksheet_by_gid(gid) worksheets.find { |ws| ws.gid == gid } end - def add_worksheet_from_template(title) + def add_worksheet_from_template(title, template_file_name = 'template') # find template key - template_sheet = worksheet_by_title('template') + template_sheet = worksheet_by_title(template_file_name) template_sheet_id = template_sheet.gid # set sheet key spreadsheet_id = template_sheet.spreadsheet.api_file.id