Allows writing data to google sheets via a public URL. Useful for writing data or error logging on small projects without the hassle of setting up a persistent data store.
Based on and inspired by Scott Olmsted's guide for doing a similar thing in javascript. That in turn was based on this post by Martin Hawksey.
Add this line to your Gemfile:
gem 'google_scribe'
Complete the Google Scripts Setup (below) and save the url generated on the last step.
Initialize:
scribe_url = "https://script.google.com/macros/s/..." # The Google Script URL
scribe = GoogleScribe.new(scribe_url)
Log Data:
# Keys correspond to the columns on the google sheet (case-sensitive)
data = { "Name" => "Samwell", "Type" => "Raven", "Message" => "Happy Birthday" }
scribe.send_data(data)
Log Process:
scribe.log_process("Weekly Update Emails") do
user_count = 0
Users.find_each do |u|
# ... send email to user
user_count += 1
end
{ "Emails sent" => user_count }
end
If the block returns an error, GoogleScribe will log the exception message
and the first line of the backtrace in the "Error Message" column, if present.
The exception will still be raised normally, unless the exception is suppressed by calling
scribe.log_process("Weekly Update Emails")
.
- Create a sheet in Google Drive.
- Add the headers you're planning to pass to sheets (case-sensitive).
- Optionally, add column for 'Timestamp.' To support process logging, add columns 'Process,' 'Success,' and 'Error Message.'
- Go to Tools -> Script Editor.
- Copy and paste the contents of sheets_server.js into the Code Editor and save.
- On the navbar of Script Editor, click Run -> setup.
- Sheets will request permissions, which you should grant.
- Click to File -> Manage Versions and save a version of the project.
- Click to Edit -> Current Project's Triggers. Under 'Run', select 'doPost'. Then under 'Events', select 'From spreadsheet' and 'On form submit.' Save.
- Click to Publish -> Deploy as web app. Select '1,' 'Me,' and 'Anyone, even anonymous.' Click Deploy.
- Copy and save the URL in the dialog that appears.
Google responds to the logging attempt with a 302 temporary redirect message, but still correctly logs the data. This has made setting up tests difficult.
MIT License
Greg Sherrid