Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
to_google_spreadsheet
Advanced tools
to_google_spreadsheet makes it trivial to populate a Google spreadsheet with an Array of Ruby objects.
# Adds the users Bob and John to the default spreadsheet in the worksheet Employees
[{:name => "Bob", :age => 26}, {:name => "John", :age => 28}].to_google_spreadsheet("Employees")
Results in:
You could also use this within Rails like so:
Account.all.to_google_spreadsheet("Accounts")
$ gem install to_google_spreadsheet
You must provide your Google Docs credentials, optionally you can supply a default spreadsheet to use:
GoogleSpreadsheet.config do |c|
c.email = "user@company.com"
c.password = "seekrit"
# optional, see note below on how to obtain a spreadsheet key
c.default_spreadsheet = "spreadsheet_key"
end
The spreadsheet key is found in the URL when visiting the spreadsheet:
#to_google_spreadsheet
is simply a method defined on Enumerable, thus any subclasses hereof should work with to_google_spreadsheet
as expected (e.g. Arrays). Enumerable#to_google_spreadsheet
takes two arguments:
module Enumerable
def to_google_spreadsheet(worksheet, spreadsheet = GoogleSpreadsheet::Config.default_spreadsheet)
session = GoogleSpreadsheet.login(GoogleSpreadsheet::Config.email,
GoogleSpreadsheet::Config.password)
spreadsheet = session.spreadsheet_by_key(spreadsheet)
ws = spreadsheet.find_or_create_worksheet_by_name(worksheet)
ws.set_header_columns(self.first)
ws.populate(self)
ws.save
end
end
The first argument being the name of the worksheet within the spreadsheet, e.g. "Accounts", if it doesn't already exist, it will be created. The second, optional, argument is the spreadsheet key (see Obtaining your spreadsheet key above), if it is not supplied, the default spreadsheet, defined by in the configuration will be used.
# Configure
GoogleSpreadsheet.config do |c|
c.email = "user@company.com"
c.password = "seekrit"
# optional, see note below on how to obtain a spreadsheet key
c.default_spreadsheet = "spreadsheet_key"
end
# Populates the worksheet "Employees" in the default spreadsheet
# with two employees
[
{:name => "Bob", :age => 26},
{:name => "John", :age => 28}
].to_google_spreadsheet("Employees")
# Populates the worksheet "Signups" in the nondefault spreadsheet
# with two signup dates
[
{:date => "18/7/2011", :signups => 28},
{:date => "19/7/2011", :signups => 33}
].to_google_spreadsheet("Signups", "other_spreadsheet_key")
# Populates the worksheet "Accounts" in the default spreadsheet
# with all # accounts in the database
Account.all.to_google_spreadsheet("Accounts")
assert array.all? {|e| [OpenStruct,Hash,ActiveRecord::Base].any? { |type| e.kind_of?(type) }}
I.e. anything that is #kind_of? Hash
, an OpenStruct
or an ActiveRecord model. It is easy to add support for additional types.
to_google_spreadsheet is released under the MIT license.
FAQs
Unknown package
We found that to_google_spreadsheet demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.