Add support to choose the sheet within google spreadsheets

This commit is contained in:
bumi 2020-06-10 15:50:15 +02:00
parent 7eb0ca6753
commit e64fa197aa
6 changed files with 91 additions and 71 deletions

View File

@ -55,6 +55,6 @@ class FormsController < ApplicationController
private
def form_params
params.require(:form).permit(:title, :thank_you_url, :backend_name, :airtable_table, :airtable_api_key, :airtable_app_key)
params.require(:form).permit(:title, :thank_you_url, :backend_name, :google_spreadsheet_sheet, :airtable_table, :airtable_api_key, :airtable_app_key)
end
end

View File

@ -38,9 +38,10 @@ class Form < ApplicationRecord
backend_name == 'airtable'
end
def google
backend_name == 'google'
def google?
backend_name == 'google_sheets'
end
def google; google?; end # TODO: remove this alias
def backend
@backend ||= SpreadsheetBackends.const_get(backend_name.camelize).new(self)

View File

@ -52,6 +52,19 @@
<p class="help">The URL where the user will be redirected after submission</p>
</div>
<% if @form.google? %>
<div class="field">
<label class="label">Sheet</label>
<div class="control">
<%= f.text_field :google_spreadsheet_sheet, class: 'input', autocomplete: 'off' %>
</div>
<p class="help">Which sheet within the spreadsheet should be used? Defaults to the first sheet. - make sure you enter the name exactly like in your spreadsheet.</p>
</div>
<% end %>
<% if @form.airtable? %>
<div class="field">

View File

@ -0,0 +1,5 @@
class AddGoogleSpreadsheetSheetToForms < ActiveRecord::Migration[6.0]
def change
add_column :forms, :google_spreadsheet_sheet, :string
end
end

View File

@ -1,5 +1,3 @@
# frozen_string_literal: true
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@ -12,83 +10,85 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20_200_413_152_532) do
ActiveRecord::Schema.define(version: 2020_06_10_131725) do
# These are extensions that must be enabled in order to support this database
enable_extension 'plpgsql'
enable_extension "plpgsql"
create_table 'active_storage_attachments', force: :cascade do |t|
t.string 'name', null: false
t.string 'record_type', null: false
t.bigint 'record_id', null: false
t.bigint 'blob_id', null: false
t.datetime 'created_at', null: false
t.string 'token'
t.index ['blob_id'], name: 'index_active_storage_attachments_on_blob_id'
t.index %w[record_type record_id name blob_id], name: 'index_active_storage_attachments_uniqueness', unique: true
t.index ['token'], name: 'index_active_storage_attachments_on_token', unique: true
create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
t.bigint "record_id", null: false
t.bigint "blob_id", null: false
t.datetime "created_at", null: false
t.string "token"
t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
t.index ["token"], name: "index_active_storage_attachments_on_token", unique: true
end
create_table 'active_storage_blobs', force: :cascade do |t|
t.string 'key', null: false
t.string 'filename', null: false
t.string 'content_type'
t.text 'metadata'
t.bigint 'byte_size', null: false
t.string 'checksum', null: false
t.datetime 'created_at', null: false
t.index ['key'], name: 'index_active_storage_blobs_on_key', unique: true
create_table "active_storage_blobs", force: :cascade do |t|
t.string "key", null: false
t.string "filename", null: false
t.string "content_type"
t.text "metadata"
t.bigint "byte_size", null: false
t.string "checksum", null: false
t.datetime "created_at", null: false
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end
create_table 'authentications', force: :cascade do |t|
t.integer 'user_id'
t.datetime 'expires_at'
t.datetime 'created_at', precision: 6, null: false
t.datetime 'updated_at', precision: 6, null: false
t.text 'access_token_ciphertext'
t.text 'refresh_token_ciphertext'
t.string 'provider'
t.string 'uid'
create_table "authentications", force: :cascade do |t|
t.integer "user_id"
t.datetime "expires_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.text "access_token_ciphertext"
t.text "refresh_token_ciphertext"
t.string "provider"
t.string "uid"
end
create_table 'forms', force: :cascade do |t|
t.integer 'user_id'
t.string 'google_spreadsheet_id'
t.string 'title'
t.string 'token'
t.string 'thank_you_url'
t.datetime 'created_at', precision: 6, null: false
t.datetime 'updated_at', precision: 6, null: false
t.string 'airtable_app_key_ciphertext'
t.string 'airtable_api_key_ciphertext'
t.string 'airtable_table'
t.string 'backend_name'
create_table "forms", force: :cascade do |t|
t.integer "user_id"
t.string "google_spreadsheet_id"
t.string "title"
t.string "token"
t.string "thank_you_url"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "airtable_app_key_ciphertext"
t.string "airtable_api_key_ciphertext"
t.string "airtable_table"
t.string "backend_name"
t.string "google_spreadsheet_sheet"
end
create_table 'submissions', force: :cascade do |t|
t.integer 'form_id'
t.json 'data', default: {}
t.datetime 'appended_at'
t.datetime 'created_at', precision: 6, null: false
t.datetime 'updated_at', precision: 6, null: false
t.string 'remote_ip'
t.string 'referrer'
t.integer 'sequential_id'
create_table "submissions", force: :cascade do |t|
t.integer "form_id"
t.json "data", default: {}
t.datetime "appended_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "remote_ip"
t.string "referrer"
t.integer "sequential_id"
end
create_table 'users', force: :cascade do |t|
t.string 'email'
t.string 'name'
t.string 'google_id'
t.datetime 'created_at', precision: 6, null: false
t.datetime 'updated_at', precision: 6, null: false
t.string 'crypted_password'
t.string 'salt'
t.string 'magic_login_token'
t.datetime 'magic_login_token_expires_at'
t.datetime 'magic_login_email_sent_at'
t.index ['email'], name: 'index_users_on_email', unique: true
t.index ['magic_login_token'], name: 'index_users_on_magic_login_token'
create_table "users", force: :cascade do |t|
t.string "email"
t.string "name"
t.string "google_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "crypted_password"
t.string "salt"
t.string "magic_login_token"
t.datetime "magic_login_token_expires_at"
t.datetime "magic_login_email_sent_at"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["magic_login_token"], name: "index_users_on_magic_login_token"
end
add_foreign_key 'active_storage_attachments', 'active_storage_blobs', column: 'blob_id'
add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
end

View File

@ -22,7 +22,8 @@ module SpreadsheetBackends
check_spreadsheed_headers!(data)
values = headers.map { |key| data[key] }
range = "A1:A#{COLUMN_INDEX_TO_LETTER[values.length]}1"
sheet = form.google_spreadsheet_sheet.present? ? "#{form.google_spreadsheet_sheet}!" : ''
range = "#{sheet}A1:A#{COLUMN_INDEX_TO_LETTER[values.length]}1"
value_range = Google::Apis::SheetsV4::ValueRange.new(values: [values], major_dimension: 'ROWS')
result = spreadsheet_service.append_spreadsheet_value(form.google_spreadsheet_id, range, value_range, value_input_option: 'USER_ENTERED')