Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 追加メソット
``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.
Expand Down Expand Up @@ -120,3 +124,5 @@ Ruby 2.0.0 or later. Checked with Ruby 2.3.0.
## Author

[Hiroshi Ichikawa](http://gimite.net/en/index.php?Contact)


33 changes: 33 additions & 0 deletions lib/api/spreadsheets.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/google_drive/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
25 changes: 25 additions & 0 deletions lib/google_drive/spreadsheet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ def worksheet_by_gid(gid)
worksheets.find { |ws| ws.gid == gid }
end

def add_worksheet_from_template(title, template_file_name = 'template')
# find template key
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
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)

# 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.
def add_worksheet(title, max_rows = 100, max_cols = 20)
xml = <<-"EOS"
Expand All @@ -79,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