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.
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
Related
I really need your help with this.
I have created an invoice template in Google Docs with databases flowed from Google sheets.
The problem is:
In the template (Google Docs), I only put a specific items line (eg 3 lines).
When the data is changed, such as the number of items lines are changing, how it's automatically gone through Google Docs if there are more than 3 items lines
Many thanks for your help.
Below is my script to get data from G-sheets to G-Docs template.
function Invoice() {
let copyFile = DriveApp.getFileById('id URL').makeCopy(),
copyID = copyFile.getId(),
copyDoc = DocumentApp.openById(copyID),
copyBody = copyDoc.getBody()
let activeSheet = SpreadsheetApp.getActiveSheet(),
numOfCol = activeSheet.getLastColumn(),
activeRowIndex = activeSheet.getActiveRange().getRowIndex(),
activeRow = activeSheet.getRange(activeRowIndex, 1, 1, numOfCol).getValues(),
headerRow = activeSheet.getRange(1, 1, 1, numOfCol).getValues(),
columnIndex = 0
for (; columnIndex < headerRow[0].length; columnIndex++){
copyBody.replaceText('%' + headerRow[0][columnIndex] + '%', activeRow[0][columnIndex])
}
copyDoc.saveAndClose()
Here is screenshot of the files.
Data in G-sheet with the additional item (Item 4)
G-Docs template with specific 3 rows for 3 items lines
When I have 4 items, I must manually amend the G-Docs template. Is there any way to get its automatically.
#Duc I don't think it's possible to pass the new header as placeholder in the GDoc, it sounds like an endless loop.
Unless you pass it as List_ITEM, but I am pretty sure you will lose formatting.
I have this block inside rails console.
#idea = Idea.find_by(rand(1...Idea.count))
#roundid = Faker::Number.between(1, 3)
#idea.round_id = #roundid
I am trying trying to update seven database rows. And I have used a code below.
7.times do
#idea = Idea.find_by(rand(1...Idea.count))
#roundid = Faker::Number.between(1, 3)
#idea.round_id = #roundid
end
it does not do what I expect. Thoughts?
If you're just trying to randomly grab an Idea each time, you can use the more expressive Idea.all.sample.
To grab a random number, you also don't necessarily need Faker, you can just do rand(3) + 1.
If round_id is a column in the ideas table, you'll need to actually save the model on each iteration for the changes you're making to persist.
One faster way you could do this is:
Idea.all.sample(7).each do |idea|
idea.update(round_id: rand(3) + 1)
end
This grabs 7 random Ideas, iterates over them, and assigns each a random round_id between 1 and 3.
After accepting data through a form for one table, I want to process that data and use it to generate entries for another table. Essentially I will be taking the first set of data, plugging it into a formula, and then entering the result as the entry of another table. Each original set of data generates multiple rows in the other table (as the formula runs several times with one variable being incremented throughout the runs).
In short, I want to do something like this:
def show
#diagram = Diagram.find(params[:id])
#horizon = #diagram.horiz
#horizon.times do |i|
#cashflow = Cashflow.new
#flow = compute_cashflow(#diagram.investment, i)
#cashflow.flow = #flow
#cashflow.year = i
#cashflow.save
end
end
Is this possible? Thanks in advance.
EDIT
Here is the function "compute_cashflow(investment, year)":
def compute_cashflow(investment, year)
return investment+year
end
(it's meaningless right now but I want to test the idea)
Feel like the title summed it up pretty well. I have two large arrays, all containing ids.
One is the old_list and one is the current_list. What I would like to do is this:
delete all the values in the old_list, that are not present in the current_list
if the value in the current_list is present in the old_list don't do anything
if the value in current_list is new then create it and add it to the old_list
This is set as a background job that updates every 4 hours. Thus I want to see if any new value have appeared, or been removed since I last checked.
Here is what I have currently, which is not complete:
twitter.follower_ids("#{uid}").each do |f_id|
# unless user already has follower id saved
unless followers.map(&:follower_id).include?(f_id.to_s)
followers.create do |follower|
follower.follower_id = f_id
end
end
end
You need to do the below Set operation :
(old_list & current_list) | current_list
Example :
old_v = [1,2,43]
new_v = [1,11,21]
(old_v & new_v) | new_v # => [1, 11, 21]
Array#& and Array#|.
I currently have an app that makes a request through an API call, and returns the data. I created two date methods (the request uses these dates as parameters), so that it always calls the data from last week:
def self.monday
d = Date.today
seven_days_ago = (d - 7)
seven_days_ago.beginning_of_week
end
def self.sunday
d = Date.today
seven_days_ago = (d - 7)
seven_days_ago.end_of_week
end
I would like to use these monday and sunday methods as the default values for the application, but if the user inputs dates in a form, these methods will be overwritten by the inputted values. These inputted values should stay as the user is navigating the site, until there is a new session, or they clear the values (thus sending the methods back to their default values).
How would I create a form that doesn't save dates to a database, but just overwrites the default dates in the two methods above? Then those dates should stay active until they clear that form, or they start a new session?
def self.monday(options = {})
if options[:date]
return Date.parse(options[:date])
else
d = Date.today
seven_days_ago = (d - 7)
return seven_days_ago.beginning_of_week
end
Will use default date
whatever.monday()
Will use specific date
whatever.monday(:date => '2013-07-01')