#StackBounty: #ruby-on-rails #ruby #google-sheets #google-drive-api Iterating Through Rows when importing Gsheet Worksheets Via Ruby Ge…

Bounty: 100

I’m using the "google_drive" gem to access a Google Sheet worksheet and I’ve been able to access and print the worksheet, so the API connection is fine.

However, I’m trying to build a method that iterates through every row and find or creates a user based on that. I’ve tried these two options:


def import
      session = GoogleDrive::Session.from_service_account_key("credspath")
      spreadsheet  = session.spreadsheet_by_title("My Worksheet Title")
      worksheet  = spreadsheet.worksheets.first
      worksheet.rows.each do |row|
        customer = Customer.find_by(email: row['email'])
        if customer.blank?
           customer = Customer.new(email: row['email'] )
           puts "New Customer #{customer.email} "
        end
      end
    end

and this


def import
      session = GoogleDrive::Session.from_service_account_key("credspath")
      spreadsheet  = session.spreadsheet_by_title("My Worksheet Title")
      worksheet  = spreadsheet.worksheets.first
      CSV.foreach(worksheet, headers: true, encoding:"iso-8859-1:utf-8") do |row|
        customer = Customer.find_by(email: row['email'])
        if customer.blank?
           customer = Customer.new(email: row['email'] )
           puts "New Customer #{customer.email} "
        end
      end
    end

I’m getting the error: "no implicit conversion of String into Integer" for the line that the iteration starts on…

How can I create from this API connection WITHOUT exporting to CSV?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.