Send information to Google Spreadsheets/Google Form

Tyler Rinker

I want to use Google google spreadsheet or form + spreadsheet to collect a response from an R user and sent it to a speadsheet.

Here is a minimal Google form: https://docs.google.com/forms/d/1tz2RPftOLRCQrGSvgJTRELrd9sdIrSZ_kxfoFdHiqD4/viewform

And the accompanying spreadsheet: https://docs.google.com/spreadsheets/d/1xemHzTdeqgMuGbsP6oqDYuMu4PymBdw7k7kMC_-Dsi8/edit#gid=102827002

I thought I'd use Jenny Bryan's googlesheets package like so:

library(googlesheets);library(dplyr)
(my_sheets <- gs_ls())

minresp <- gs_title("minimal (Responses)")
minresp %>% gs_add_row(ws = "dat", input = mtcars[20, 1:2]) 

Works great but if I include the code and someone else (i.e., who is not me) tries to use the code:

Error in gs_lookup(., "sheet_title", verbose) : 
  "minimal (Responsess)" doesn't match sheet_title of any sheet returned by gs_ls() (which should reflect user's Google Sheets home screen).

So in the vignette I see there's apart:

# Need to access a sheet you do not own?
# Access it by key if you know it!

I figured this was the ticket to allow others to input data into a spreadsheet so I tried:

minresp2 <- gs_key("1xemHzTdeqgMuGbsP6oqDYuMu4PymBdw7k7kMC_-Dsi8")

yielding:

Authentication will be used.
Error in gs_lookup(., "sheet_key", verbose) : 
  "1xemHzTdeqgMuGbsP6oqDYuMu4PymBdw7k7kMC_-Dsi8" doesn't match sheet_key of any sheet returned by gs_ls() (which should reflect user's Google Sheets home screen).

This all works if I'm logged into and authenticated via google, but how could I make it so users could add to my spreadsheet. I don't care what approach/packages one uses. It's be nice if the user could only edit the spreasheet via the use in the function and not simply look at the source and get the url and edit manually but that's less of a concern.

Essentially I want to be able to collect responses from an R user in a spreadsheeet (or other data form) and append with each new user.

jennybryan

Your users should register the Sheet with gs_key(YOUR_KEY, lookup = FALSE, visibility = "private"). These two arguments are important for two different reasons.

  • lookup = FALSE allows them to register the Sheet even if they have never visited in the browser. Note it's still important that the Sheet is "published to the web" and they have read permission.
  • visibility = "private" affects the URL we build for the eventual POST required to add a row. To POST, visibility must be "private", otherwise you get the 405 error reported in https://github.com/jennybc/googlesheets/issues/168.

I still needed to make lookup information part of the registered Google Sheet object for the example below to work for non-owners of the Sheet. Which means you'll need to install the development version from GitHub.

To achieve your goal, your user must also have write permission. How to achieve that? Easiest but yucky is to make the Sheet world writable, as done below. For a small number of people, you could grant permission individually. I know of no way to make a Sheet writable by, say, people who happen to have the key. If you really want to "bake in" this sort of permission into a function or package, I suspect you'll need to find a secure way to transmit a token, as described in my original comment.

ss <- gs_new("add-row-test", input = head(iris))
#> Sheet "add-row-test" created in Google Drive.
#> Range affected by the update: "A1:E7"
#> Worksheet "Sheet1" successfully updated with 35 new value(s).
#> Worksheet dimensions: 1000 x 26.

In the browser, do two things:
File > Publish to the web
Share button > Public on the web - Anyone on the Internet can find and edit

Now your users can access and add rows like so:

ss_key <- "114cXPTe9whThS3lmpa3neY2vplpUX1hcnM8o8Oo6QtM"
add_row_result <- ss_key %>%
  gs_key(lookup = FALSE, visibility = "private") %>%
  gs_add_row(input = c("can", "you", "hear", "me", "now?"))
#> Authorization will not be used.
#> Worksheets feed constructed with private visibility
#> Row successfully appended.
add_row_result %>%
  gs_read()
#> Accessing worksheet titled "Sheet1"
#> Source: local data frame [9 x 5]
#>
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#>          (chr)       (chr)        (chr)       (chr)   (chr)
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9           3          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5            5         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
#> 7          can         you         hear          me    now?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I allow new R users to send information to a Google Form?

From Dev

Form to send information to different pages according to which button you push

From Dev

Send date and retrieve information using PHP and AJAX in form

From Dev

Form to send information to different pages according to which button you push

From Dev

Send date and retrieve information using PHP and AJAX in form

From Dev

Radio button on click submit the form and send information to another file

From Dev

send information via form without transaction in another page PHP

From Dev

Send email when Google Form submission is updated

From Dev

Google AdWords Conversion Tracking on a form send

From Dev

Google script, send email with form details triggered by form submit

From Dev

How to send a google form using google script with all the styles

From Dev

How to get the row information from a google form grid item

From Dev

google chrome replay XHR request but it send without form data

From Dev

Google Form on Submit, Send Email Lock Doesn't Work

From Dev

google chrome replay XHR request but it send without form data

From Dev

Google Form on Submit, Send Email Lock Doesn't Work

From Dev

How to send data from HTML form to Google Spreadsheet using JavaScript?

From Dev

How to retrieve a selected row's form MySQL database and send back the information to Android?

From Dev

Send information to remote server

From Dev

Send information between applications

From Dev

How to send information to a file?

From Dev

How to send and retrieve information/

From Dev

Laravel: Posting information with a form

From Dev

Add Form Information to a Table

From Dev

Capturing Information in a form with javascript

From Dev

Add Form Information to a Table

From Dev

PHP not grabbing form information

From Dev

send mail form, but the mail is not send

From Dev

Not using a form to send form data

Related Related

  1. 1

    How can I allow new R users to send information to a Google Form?

  2. 2

    Form to send information to different pages according to which button you push

  3. 3

    Send date and retrieve information using PHP and AJAX in form

  4. 4

    Form to send information to different pages according to which button you push

  5. 5

    Send date and retrieve information using PHP and AJAX in form

  6. 6

    Radio button on click submit the form and send information to another file

  7. 7

    send information via form without transaction in another page PHP

  8. 8

    Send email when Google Form submission is updated

  9. 9

    Google AdWords Conversion Tracking on a form send

  10. 10

    Google script, send email with form details triggered by form submit

  11. 11

    How to send a google form using google script with all the styles

  12. 12

    How to get the row information from a google form grid item

  13. 13

    google chrome replay XHR request but it send without form data

  14. 14

    Google Form on Submit, Send Email Lock Doesn't Work

  15. 15

    google chrome replay XHR request but it send without form data

  16. 16

    Google Form on Submit, Send Email Lock Doesn't Work

  17. 17

    How to send data from HTML form to Google Spreadsheet using JavaScript?

  18. 18

    How to retrieve a selected row's form MySQL database and send back the information to Android?

  19. 19

    Send information to remote server

  20. 20

    Send information between applications

  21. 21

    How to send information to a file?

  22. 22

    How to send and retrieve information/

  23. 23

    Laravel: Posting information with a form

  24. 24

    Add Form Information to a Table

  25. 25

    Capturing Information in a form with javascript

  26. 26

    Add Form Information to a Table

  27. 27

    PHP not grabbing form information

  28. 28

    send mail form, but the mail is not send

  29. 29

    Not using a form to send form data

HotTag

Archive