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
27
28
# 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'),
    'auth_method' => config.param('auth_method', :string, default: 'service_account'),
    'mode' => config.param('mode', :string, default: 'update')
  }

  yield(task)

  {}
end

Instance Method Details

#add(page) ⇒ Object



44
45
46
47
48
# File 'lib/embulk/output/google_sheets_ruby.rb', line 44

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

#append_sheet(value_range) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/embulk/output/google_sheets_ruby.rb', line 94

def append_sheet(value_range)
  @service.append_spreadsheet_value(
    @spreadsheet_id,
    value_range.range,
    value_range,
    value_input_option: 'USER_ENTERED',
    insert_data_option: 'INSERT_ROWS'
  )
end

#authorizeObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/embulk/output/google_sheets_ruby.rb', line 68

def authorize
  case @auth_method
  when 'service_account'
    return Google::Auth::ServiceAccountCredentials.make_creds(
      json_key_io: File.open(@credentials_path),
      scope: SCOPE
      )
  when 'authorized_user'
    return Google::Auth::UserRefreshCredentials.make_creds(
      json_key_io: File.open(@credentials_path),
      scope: SCOPE
      )
  else
    raise ConfigError.new("Unknown auth method: #{auth_method}")
  end
end

#commitObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/embulk/output/google_sheets_ruby.rb', line 50

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

  case @mode
  when 'update'
    update_sheet(value_range)
  when 'append'
    append_sheet(value_range)
  else
    raise ConfigError.new("Unknown mode: #{@mode}")
  end

  {}
end

#initObject



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/embulk/output/google_sheets_ruby.rb', line 30

def init
  @spreadsheet_id = task['spreadsheet_id']
  @credentials_path = task['credentials_path']
  @range = task['range']
  @auth_method = task['auth_method']
  @mode = task['mode']
  @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



85
86
87
88
89
90
91
92
# File 'lib/embulk/output/google_sheets_ruby.rb', line 85

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