Napmi
Napmi

Reputation: 541

google-sheet api rails, multiple write to sheet issue

I am currently trying to work this in my website , https://github.com/gimite/google-drive-ruby

because i want to store some submitted information to my google sheets , everytime someone submits the form.

After implementing "on behalf of you" method to my rails, i am able to save these information to my google sheets. However, i have a serious issue where , if there are more than 1 people submitting the form the same time, three things might happen

1) both forms keyed into the sheet 2) one of the form is keyed twice 3) (if 3 form run at same time) one entry goes missing.

this is my ruby definition in my controller which will trigger when the user hits the submit button.

def save_googlesheet
  session = GoogleDrive::Session.from_config("config.json")
  ws = session.spreadsheet_by_key("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").worksheets[0]
  ws1 = session.spreadsheet_by_key("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").worksheets[1]
  # get row value and update row sheet val
  row = p ws1[1, 2].to_i  + 1
  ws1[1,2] = row;
  ws1.save

  column = 1;

  ws[row, column+1] = @some_form.name_first
  ws[row, column+2] = @some_form.name_last
  ws[row, column+3] = @some_form.job_title
  ws[row, column+4] = @some_form.zip
  ws[row, column+5] = @some_form.addr1
  ws[row, column+6] = @some_form.addr2
  ws[row, column+6] = @some_form.addr3
  ws[row, column+7] = @some_form.mail
  ws[row, column+8] = @some_form.tel
  ws.save
end
def get_row_googlesheet
  session = GoogleDrive::Session.from_config("config.json")
end

Just to note, i have 2 spreadsheet. the second sheet keeps the row number. I used this solution because i am not sure how to prevent 2 people from overriding the same row at the same time. And the first spreadsheet is of course the file i wish to be updating.

Upvotes: 0

Views: 394

Answers (1)

Keval
Keval

Reputation: 3336

I would recommend to use a background job using any job library, so when the user submits a form que the job to write in to google sheet

http://tutorials.jumpstartlab.com/topics/performance/background_jobs.html

https://github.com/resque/resque

http://redistogo.com/documentation/resque

Upvotes: 1

Related Questions