From e64fa197aa5a68a225a2595e11924033add468bf Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 10 Jun 2020 15:50:15 +0200 Subject: [PATCH] Add support to choose the sheet within google spreadsheets --- app/controllers/forms_controller.rb | 2 +- app/models/form.rb | 5 +- app/views/forms/edit.html.erb | 13 ++ ...5_add_google_spreadsheet_sheet_to_forms.rb | 5 + db/schema.rb | 134 +++++++++--------- lib/spreadsheet_backends/google_sheets.rb | 3 +- 6 files changed, 91 insertions(+), 71 deletions(-) create mode 100644 db/migrate/20200610131725_add_google_spreadsheet_sheet_to_forms.rb diff --git a/app/controllers/forms_controller.rb b/app/controllers/forms_controller.rb index 8084bf5..d941881 100644 --- a/app/controllers/forms_controller.rb +++ b/app/controllers/forms_controller.rb @@ -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 diff --git a/app/models/form.rb b/app/models/form.rb index b51c6f2..b01fdab 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -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) diff --git a/app/views/forms/edit.html.erb b/app/views/forms/edit.html.erb index 76ce773..54d75e5 100644 --- a/app/views/forms/edit.html.erb +++ b/app/views/forms/edit.html.erb @@ -52,6 +52,19 @@

The URL where the user will be redirected after submission

+ <% if @form.google? %> + +
+ +
+ <%= f.text_field :google_spreadsheet_sheet, class: 'input', autocomplete: 'off' %> +
+

Which sheet within the spreadsheet should be used? Defaults to the first sheet. - make sure you enter the name exactly like in your spreadsheet.

+
+ + <% end %> + + <% if @form.airtable? %>
diff --git a/db/migrate/20200610131725_add_google_spreadsheet_sheet_to_forms.rb b/db/migrate/20200610131725_add_google_spreadsheet_sheet_to_forms.rb new file mode 100644 index 0000000..cd92680 --- /dev/null +++ b/db/migrate/20200610131725_add_google_spreadsheet_sheet_to_forms.rb @@ -0,0 +1,5 @@ +class AddGoogleSpreadsheetSheetToForms < ActiveRecord::Migration[6.0] + def change + add_column :forms, :google_spreadsheet_sheet, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 3f72713..cfb8137 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/lib/spreadsheet_backends/google_sheets.rb b/lib/spreadsheet_backends/google_sheets.rb index d5f92d1..7be241f 100644 --- a/lib/spreadsheet_backends/google_sheets.rb +++ b/lib/spreadsheet_backends/google_sheets.rb @@ -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')