12
$\begingroup$

I am working on a personal project involving a CloudDeploy[ ] that reads data off a Google Doc and then works with it. Ideally, the Google Doc is either a text document or a spreadsheet which contains a single string, which is what I want Mathematica to read as input. Example here.

The kind of stuff I would like to do the the string is very simple, but I am stuck at the very beginning. This:

string = Import[theURLstring];

obviously fails miserably. Can someone help?

More details
- I looked at this past question, but it didn't help me.
- The reason I want to use Google Doc rather than a databin, say, is that I want my friends to add to the string without them to know basically any technical details. Basically I want to be as easy as: "open the doc, add to the document, save, close" on the input side, and "open this webpage to see the results" on the output side.
- a document on dropbox would be OK, but only if there is no way of doing it on Google.
- I also don't have much technical internet knowledge, so bear with me if I am asking the impossible or if this question is very simple.

$\endgroup$

2 Answers 2

7
$\begingroup$

This seems to work:

URLFetch["https://docs.google.com/document/export?format=txt&\
id=17m1JfjEbrna7e9INZv-FXZQ9yQJd8d1Uu2LFEPyT_ZI&token=\
AC4w5VhkHSfLIe2xvAUWQC9XHb1lAmM7Xw%3A1460476462625"]

(* "When the soil were finally depleted of all nutrients, we \
realised that paper isn't edible. Or  is it? Now all paper is \
digestible, we eat the wrapping of our consumer products and instead \
of recycling paperwork, we eat it. This is thanks to  GM bacteria \
that can digest cellulose into human nutrients. When someone finally \
found a way to make the bacteria produce human microdoses of cocaine \
without killing  itself in the process, scientist and governments \
decided it was for the best of society, GDP growth (and balance \
sheets of the corporations backing them) to put the bacteria straight \
into our guts, monthly. This was legally OK because nobody is being \
sold cocaine. Now workdays are up to 12 hours and sleeptime down to \
4. When dating, we do the socialising at coffee shops, which sell \
cannabis, to take the edge off the day." *)

I got the URL by starting the download process and looking at the HTTP headers that were sent (you can see these in the Developer Tools for your browser).

Might be better to present this in pieces:

url = "https://docs.google.com/document/export";

URLFetch[url, 
 "Parameters" -> {"format" -> "txt", 
   "id" -> "17m1JfjEbrna7e9INZv-FXZQ9yQJd8d1Uu2LFEPyT_ZI", 
   "token" -> "AC4w5VhkHSfLIe2xvAUWQC9XHb1lAmM7Xw:1460476462625"}]

or you can save to a file with:

URLSave[
 URLBuild[url, {"format" -> "txt", 
   "id" -> "17m1JfjEbrna7e9INZv-FXZQ9yQJd8d1Uu2LFEPyT_ZI", 
   "token" -> "AC4w5VhkHSfLIe2xvAUWQC9XHb1lAmM7Xw:1460476462625"}], 
 FileNameJoin[{$TemporaryDirectory, "gDoc"}]]
(* "C:\\Users\\user1\\AppData\\Local\\Temp\\gDoc" *)
$\endgroup$
4
  • $\begingroup$ Thanks! that works. However, I do not understand the second part, where you "present it in pieces". In what way is it better? $\endgroup$
    – Andrea
    Commented Apr 13, 2016 at 13:59
  • 1
    $\begingroup$ Mainly that it shows how one could programmatically access these documents. You would just need to get the "id" and "token". The URL is put in the first URLFetch is really made up of pieces and one can take advantage of that. $\endgroup$
    – chuy
    Commented Apr 14, 2016 at 13:15
  • $\begingroup$ Would the id and token change if I change the folder of the file? $\endgroup$
    – Andrea
    Commented Apr 14, 2016 at 18:09
  • 1
    $\begingroup$ it likely would $\endgroup$
    – chuy
    Commented Apr 14, 2016 at 20:54
1
$\begingroup$

The format of the direct download link is https://docs.google.com/document/d/<<file id>>/export?format=doc or format=txt or format=pdf, etc. Just write a small piece of function to replace the final part of the sharing URL to export?format=xxx and you get the direct (actually formatted) download link. For example, define function

googleDoc[gurl_, format_] := 
 StringJoin@@Riffle[
  ReplacePart[
   StringSplit[gurl, "/" ],
   -1 -> StringJoin["export?format=", format]
  ], "/"
]

Copy the share link of your document, and say

URLDownload[ googleDoc[ "share link here", "doc" ] ]

or something like that.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.