class Embulk::Output::GoogleSheetsRuby

Google Sheets Ruby output plugin for Embulk

Public Class Methods

transaction(config, _schema, _count) { |task| ... } click to toggle source
# 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

Public Instance Methods

add(page) click to toggle source
# File lib/embulk/output/google_sheets_ruby.rb, line 46
def add(page)
  page.each do |record|
    @rows << record
  end
end
append_sheet(value_range) click to toggle source
# File lib/embulk/output/google_sheets_ruby.rb, line 96
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
authorize() click to toggle source
# File lib/embulk/output/google_sheets_ruby.rb, line 70
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
commit() click to toggle source
# File lib/embulk/output/google_sheets_ruby.rb, line 52
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
init() click to toggle source
# 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 = []
  if @index == 0
    @rows << schema.map(&:name)
  end

  @service = Google::Apis::SheetsV4::SheetsService.new
  @service.client_options.application_name = APPLICATION_NAME
  @service.authorization = authorize
end
update_sheet(value_range) click to toggle source
# File lib/embulk/output/google_sheets_ruby.rb, line 87
def update_sheet(value_range)
  @service.update_spreadsheet_value(
    @spreadsheet_id,
    value_range.range,
    value_range,
    value_input_option: 'USER_ENTERED'
  )
end