Class: Embulk::Output::GoogleSheetsRuby

Inherits:
OutputPlugin
  • Object
show all
Defined in:
lib/embulk/output/google_sheets_ruby.rb

Overview

Google Sheets Ruby output plugin for Embulk

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.transaction(config, _schema, _count) {|task| ... } ⇒ Object

Yields:

  • (task)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/embulk/output/google_sheets_ruby.rb', line 14

def self.transaction(config, _schema, _count)
  task = {
    'spreadsheet_id' => config.param('spreadsheet_id', :string),
    'range' => config.param('range', :string, default: 'A1'),
    'credentials_path' => config.param('credentials_path',
                                       :string,
                                       default: 'credentials.json')
  }

  yield(task)

  {}
end

Instance Method Details

#add(page) ⇒ Object



40
41
42
43
44
# File 'lib/embulk/output/google_sheets_ruby.rb', line 40

def add(page)
  page.each do |record|
    @rows << record
  end
end

#authorizeObject



57
58
59
60
61
62
63
64
65
# File 'lib/embulk/output/google_sheets_ruby.rb', line 57

def authorize
  authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
    json_key_io: File.open(@credentials_path),
    scope: SCOPE
  )
  authorizer.fetch_access_token!

  authorizer
end

#commitObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/embulk/output/google_sheets_ruby.rb', line 46

def commit
  value_range = Google::Apis::SheetsV4::ValueRange.new
  value_range.range = @range
  value_range.major_dimension = 'ROWS'
  value_range.values = @rows

  update_sheet(value_range)

  {}
end

#initObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/embulk/output/google_sheets_ruby.rb', line 28

def init
  @spreadsheet_id = task['spreadsheet_id']
  @credentials_path = task['credentials_path']
  @range = task['range']
  @rows = []
  @rows << schema.map(&:name)

  @service = Google::Apis::SheetsV4::SheetsService.new
  @service.client_options.application_name = APPLICATION_NAME
  @service.authorization = authorize
end

#update_sheet(value_range) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/embulk/output/google_sheets_ruby.rb', line 67

def update_sheet(value_range)
  @service.update_spreadsheet_value(
    @spreadsheet_id,
    value_range.range,
    value_range,
    value_input_option: 'USER_ENTERED'
  )
end