├── .gitignore ├── README.md ├── project.clj ├── bin └── setup.sh ├── src └── gsheets_demo │ └── core.clj ├── LICENSE └── doc └── howto.md /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | /resources/GSheetDemo-041db3d758a1.json 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gsheets-demo 2 | 3 | A demo project showing how to use Google's GData Spreadsheets API from Clojure. 4 | 5 | ## Usage 6 | 7 | See the [HOWTO](doc/howto.md). 8 | 9 | ## License 10 | 11 | Copyright © 2016 Ray Miller 12 | 13 | Distributed under the Eclipse Public License either version 1.0 or (at 14 | your option) any later version. 15 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject gsheets-demo "0.1.0-SNAPSHOT" 2 | :description "Google Sheets Demo" 3 | :url "http://github.com/ray1729/gsheets-demo" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"] 7 | [com.google.api-client/google-api-client "1.21.0"] 8 | [com.google.gdata/gdata-core "1.0"] 9 | [com.google.gdata/gdata-spreadsheet "3.0"]]) 10 | -------------------------------------------------------------------------------- /bin/setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | function log () { 6 | echo "$1" >&2 7 | } 8 | 9 | function install_artifact () { 10 | log "Installing artifact $2" 11 | mvn install:install-file -DgroupId="$1" -DartifactId="$2" -Dversion="$3" -Dfile="$4" \ 12 | -Dpackaging=jar -DgeneratePom=true 13 | } 14 | 15 | R="${HOME}/.m2/repository" 16 | V="1.47.1" 17 | U="http://storage.googleapis.com/gdata-java-client-binaries/gdata-src.java-${V}.zip" 18 | 19 | if test -r "${R}/com/google/gdata/gdata-core/1.0/gdata-core-1.0.jar" \ 20 | -a -r "${R}/com/google/gdata/gdata-spreadsheet/3.0/gdata-spreadsheet-3.0.jar"; 21 | then 22 | log "Artifacts up-to-date" 23 | exit 0 24 | fi 25 | 26 | log "Downloading $U" 27 | cd $(mktemp -d) 28 | wget "${U}" 29 | unzip "gdata-src.java-${V}.zip" 30 | 31 | install_artifact com.google.gdata gdata-core 1.0 gdata/java/lib/gdata-core-1.0.jar 32 | 33 | install_artifact com.google.gdata gdata-spreadsheet 3.0 gdata/java/lib/gdata-spreadsheet-3.0.jar 34 | -------------------------------------------------------------------------------- /src/gsheets_demo/core.clj: -------------------------------------------------------------------------------- 1 | (ns gsheets-demo.core 2 | (:require [clojure.java.io :as io]) 3 | (:import com.google.gdata.client.spreadsheet.SpreadsheetService 4 | com.google.gdata.data.spreadsheet.SpreadsheetFeed 5 | com.google.gdata.data.spreadsheet.WorksheetFeed 6 | com.google.gdata.data.spreadsheet.CellFeed 7 | com.google.api.client.googleapis.auth.oauth2.GoogleCredential 8 | com.google.api.client.json.jackson2.JacksonFactory 9 | com.google.api.client.googleapis.javanet.GoogleNetHttpTransport 10 | java.net.URL 11 | java.util.Collections)) 12 | 13 | (def application-name "gsheetdemo-v0.0.1") 14 | 15 | (def credentials-resource (io/resource "GSheetDemo-041db3d758a1.json")) 16 | 17 | (def oauth-scope "https://spreadsheets.google.com/feeds") 18 | 19 | (def spreadsheet-feed-url (URL. "https://spreadsheets.google.com/feeds/spreadsheets/private/full")) 20 | 21 | (defn get-credential 22 | [] 23 | (with-open [in (io/input-stream credentials-resource)] 24 | (let [credential (GoogleCredential/fromStream in)] 25 | (.createScoped credential (Collections/singleton oauth-scope))))) 26 | 27 | (defn init-service 28 | [] 29 | (let [credential (get-credential) 30 | service (SpreadsheetService. application-name)] 31 | (.setOAuth2Credentials service credential) 32 | service)) 33 | 34 | (defn list-spreadsheets 35 | [service] 36 | (.getEntries (.getFeed service spreadsheet-feed-url SpreadsheetFeed))) 37 | 38 | (defn find-spreadsheet-by-title 39 | [service title] 40 | (let [spreadsheets (filter (fn [sheet] (= (.getPlainText (.getTitle sheet)) title)) 41 | (list-spreadsheets service))] 42 | (if (= (count spreadsheets) 1) 43 | (first spreadsheets) 44 | (throw (Exception. (format "Found %d spreadsheets with name %s" 45 | (count spreadsheets) 46 | title)))))) 47 | 48 | (defn list-worksheets 49 | [service spreadsheet] 50 | (.getEntries (.getFeed service (.getWorksheetFeedUrl spreadsheet) WorksheetFeed))) 51 | 52 | (defn find-worksheet-by-title 53 | [service spreadsheet title] 54 | (let [worksheets (filter (fn [ws] (= (.getPlainText (.getTitle ws)) title)) 55 | (list-worksheets service spreadsheet))] 56 | (if (= (count worksheets) 1) 57 | (first worksheets) 58 | (throw (Exception. (format "Found %d worksheets in %s with name %s" 59 | (count worksheets) 60 | spreadsheet 61 | title)))))) 62 | 63 | (defn get-cells 64 | [service worksheet] 65 | (map (memfn getCell) (.getEntries (.getFeed service (.getCellFeedUrl worksheet) CellFeed)))) 66 | 67 | (defn to-nested-vec 68 | [cells] 69 | (mapv (partial mapv (memfn getValue)) (partition-by (memfn getRow) cells))) 70 | 71 | (defn fetch-worksheet 72 | [service {spreadsheet-title :spreadsheet worksheet-title :worksheet}] 73 | (if-let [spreadsheet (find-spreadsheet-by-title service spreadsheet-title)] 74 | (if-let [worksheet (find-worksheet-by-title service spreadsheet worksheet-title)] 75 | (to-nested-vec (get-cells service worksheet)) 76 | (throw (Exception. (format "Spreadsheet '%s' has no worksheet '%s'" 77 | spreadsheet-title worksheet-title)))) 78 | (throw (Exception. (format "Spreadsheet '%s' not found" spreadsheet-title))))) 79 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /doc/howto.md: -------------------------------------------------------------------------------- 1 | # Reading Google Sheets from Clojure 2 | 3 | One of the user stories I had to tackle in a recent sprint was to import data maintained by a non-technical staff member in a Google Spreadsheet into our analytics database. I quickly found a [Java API for Google Spreadsheets](https://developers.google.com/google-apps/spreadsheets/) that looked promising but turned out to be more tricky to get up and running than at first glance. In this article, I show you how to use this library from Clojure and avoid some of the pitfalls I fell into. 4 | 5 | ## Google Spreadsheets API 6 | 7 | The [GData Java client](https://github.com/google/gdata-java-client) referenced in the [Google Spreadsheets API documentation](https://developers.google.com/google-apps/spreadsheets/) uses an old XML-based protocol, which is mostly deprecated. We are recommended to use the newer, [JSON-based client](https://github.com/google/google-api-java-client). After chasing my tail on this, I discovered that Google Spreadsheets does not yet support this new API and we *do* need the GData client after all. 8 | 9 | ## The first hurdle: dependencies 10 | 11 | The GData Java client is not available from Maven, so we have to [download a zip archive](http://storage.googleapis.com/gdata-java-client-binaries/gdata-src.java-1.47.1.zip). The easiest way to use these from a Leiningen project is to use `mvn` to install the required jar files in our local repository and specify the dependencies in the usual way. This handy script automates the process, only downloading the archive if necessary. (For this project, we only need the `gdata-core` and `gdata-spreadsheet` jars, but the script is easily extended if you need other components.) 12 | 13 | #!/bin/bash 14 | 15 | set -e 16 | 17 | function log () { 18 | echo "$1" >&2 19 | } 20 | 21 | function install_artifact () { 22 | log "Installing artifact $2" 23 | mvn install:install-file -DgroupId="$1" -DartifactId="$2" -Dversion="$3" -Dfile="$4" \ 24 | -Dpackaging=jar -DgeneratePom=true 25 | } 26 | 27 | R="${HOME}/.m2/repository" 28 | V="1.47.1" 29 | U="http://storage.googleapis.com/gdata-java-client-binaries/gdata-src.java-${V}.zip" 30 | 31 | if test -r "${R}/com/google/gdata/gdata-core/1.0/gdata-core-1.0.jar" \ 32 | -a -r "${R}/com/google/gdata/gdata-spreadsheet/3.0/gdata-spreadsheet-3.0.jar"; 33 | then 34 | log "Artifacts up-to-date" 35 | exit 0 36 | fi 37 | 38 | log "Downloading $U" 39 | cd $(mktemp -d) 40 | wget "${U}" 41 | unzip "gdata-src.java-${V}.zip" 42 | 43 | install_artifact com.google.gdata gdata-core 1.0 gdata/java/lib/gdata-core-1.0.jar 44 | 45 | install_artifact com.google.gdata gdata-spreadsheet 3.0 gdata/java/lib/gdata-spreadsheet-3.0.jar 46 | 47 | Once we've installed these jars, we can configure dependencies as follows: 48 | 49 | (defproject gsheets-demo "0.1.0-SNAPSHOT" 50 | :description "Google Sheets Demo" 51 | :url "https://github.com/ray1729/gsheets-demo" 52 | :license {:name "Eclipse Public License" 53 | :url "http://www.eclipse.org/legal/epl-v10.html"} 54 | :dependencies [[org.clojure/clojure "1.8.0"] 55 | [com.google.gdata/gdata-core "1.0"] 56 | [com.google.gdata/gdata-spreadsheet "3.0"]]) 57 | 58 | ## The second hurdle: authentication 59 | 60 | This is a pain, as the documentation for the GData Java client is incomplete and at times confusing, and the examples it ships with no longer work as they use a deprecated OAuth version. The example Java code in the documentation tells us: 61 | 62 | ``` Java 63 | // TODO: Authorize the service object for a specific user (see other sections) 64 | ``` 65 | 66 | The other sections were no more enlightening, but after more digging and reading of source code, I realised we can use the `google-api-client` to manage our OAuth credentials and simply pass that credentials object to the GData client. This library is already available from a central Maven repository, so we can simply update our project's dependencies to pull it in: 67 | 68 | :dependencies [[org.clojure/clojure "1.8.0"] 69 | [com.google.api-client/google-api-client "1.21.0"] 70 | [com.google.gdata/gdata-core "1.0"] 71 | [com.google.gdata/gdata-spreadsheet "3.0"]] 72 | 73 | ## OAuth credentials 74 | 75 | Before we can start using OAuth, we have to register our client with Google. This is done via the 76 | [Google Developers Console](https://console.developers.google.com/). See [Using OAuth 2.0 to Access Google APIs](https://developers.google.com/identity/protocols/OAuth2) for full details, but here's a quick-start guide to creating credentials for a service account. 77 | 78 | Navigate to the [Developers Console](https://console.developers.google.com/). Click on *Enable and manage APIs* and select *Create a new project*. Enter the project name and click *Create*. 79 | 80 | Once project is created, click on *Credentials* in the sidebar, then the *Create Credentials* drop-down. As our client is going to run from cron, we want to enable server-to-server authentication, so select *Service account key*. On the next screen, select *New service account* and enter a name. Make sure the *JSON* radio button is selected, then click on *Create*. 81 | 82 | Copy the downloaded JSON file into your project's `resources` directory. It should look something like: 83 | 84 | { 85 | "type": "service_account", 86 | "project_id": "gsheetdemo", 87 | "private_key_id": "041db3d758a1a7ef94c9c59fb3bccd2fcca41eb8", 88 | "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n", 89 | "client_email": "gsheets-demo@gsheetdemo.iam.gserviceaccount.com", 90 | "client_id": "106215031907469115769", 91 | "auth_uri": "https://accounts.google.com/o/oauth2/auth", 92 | "token_uri": "https://accounts.google.com/o/oauth2/token", 93 | "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", 94 | "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/gsheets-demo%40gsheetdemo.iam.gserviceaccount.com" 95 | } 96 | 97 | We'll use this in a moment to create a `GoogleCredential` object, but before that navigate to Google Sheets and create a test spreadsheet. Grant read access to the spreadsheet to the email address found in `client_email` in your downloaded credentials. 98 | 99 | ## A simple Google Spreadsheets client 100 | 101 | We're going to be using a Java client, so it should come as no surprise that our namespace imports a lot of Java classes: 102 | 103 | (ns gsheets-demo.core 104 | (:require [clojure.java.io :as io]) 105 | (:import com.google.gdata.client.spreadsheet.SpreadsheetService 106 | com.google.gdata.data.spreadsheet.SpreadsheetFeed 107 | com.google.gdata.data.spreadsheet.WorksheetFeed 108 | com.google.gdata.data.spreadsheet.CellFeed 109 | com.google.api.client.googleapis.auth.oauth2.GoogleCredential 110 | com.google.api.client.json.jackson2.JacksonFactory 111 | com.google.api.client.googleapis.javanet.GoogleNetHttpTransport 112 | java.net.URL 113 | java.util.Collections)) 114 | 115 | We start by defining some constants for our application. The crenentials resource is the JSON file we downloaded from the developer console: 116 | 117 | (def application-name "gsheetdemo-v0.0.1") 118 | 119 | (def credentials-resource (io/resource "GSheetDemo-041db3d758a1.json")) 120 | 121 | (def oauth-scope "https://spreadsheets.google.com/feeds") 122 | 123 | (def spreadsheet-feed-url (URL. "https://spreadsheets.google.com/feeds/spreadsheets/private/full")) 124 | 125 | With this in hand, we can create a `GoogleCredential` object and initialize the Google Sheets service: 126 | 127 | (defn get-credential 128 | [] 129 | (with-open [in (io/input-stream credentials-resource)] 130 | (let [credential (GoogleCredential/fromStream in)] 131 | (.createScoped credential (Collections/singleton oauth-scope))))) 132 | 133 | (defn init-service 134 | [] 135 | (let [credential (get-credential) 136 | service (SpreadsheetService. application-name)] 137 | (.setOAuth2Credentials service credential) 138 | service)) 139 | 140 | Let's try it at a REPL: 141 | 142 | lein repl 143 | 144 | user=> (require '[gsheets-demo.core :as gsheets]) 145 | nil 146 | user=> (def service (gsheets/init-service)) 147 | #'user/service 148 | user=> (.getEntries (.getFeed service 149 | gsheets/spreadsheet-feed-url 150 | com.google.gdata.data.spreadsheet.SpreadsheetFeed)) 151 | (#object[com.google.gdata.data.spreadsheet.SpreadsheetEntry 0x43ab2a3e "com.google.gdata.data.spreadsheet.SpreadsheetEntry@43ab2a3e"]) 152 | 153 | Great! We can see the one spreadsheet we granted our service account read access. Let's wrap this up in a function and implemnet a helper to find a spreadsheet by name: 154 | 155 | (defn list-spreadsheets 156 | [service] 157 | (.getEntries (.getFeed service spreadsheet-feed-url SpreadsheetFeed))) 158 | 159 | (defn find-spreadsheet-by-title 160 | [service title] 161 | (let [spreadsheets (filter (fn [sheet] (= (.getPlainText (.getTitle sheet)) title)) 162 | (list-spreadsheets service))] 163 | (if (= (count spreadsheets) 1) 164 | (first spreadsheets) 165 | (throw (Exception. (format "Found %d spreadsheets with name %s" 166 | (count spreadsheets) 167 | title)))))) 168 | 169 | Back at the REPL: 170 | 171 | user=> (def spreadsheet (gsheets/find-spreadsheet-by-title service "Colour Counts")) 172 | user=> (.getPlainText (.getTitle spreadsheet)) 173 | "Colour Counts" 174 | 175 | A spreadsheet contains one or more worksheets, so the next functions we implement take a `SpreadsheetEntry` object and list or search worksheets: 176 | 177 | (defn list-worksheets 178 | [service spreadsheet] 179 | (.getEntries (.getFeed service (.getWorksheetFeedUrl spreadsheet) WorksheetFeed))) 180 | 181 | (defn find-worksheet-by-title 182 | [service spreadsheet title] 183 | (let [worksheets (filter (fn [ws] (= (.getPlainText (.getTitle ws)) title)) 184 | (list-worksheets service spreadsheet))] 185 | (if (= (count worksheets) 1) 186 | (first worksheets) 187 | (throw (Exception. (format "Found %d worksheets in %s with name %s" 188 | (count worksheets) 189 | spreadsheet 190 | title)))))) 191 | 192 | ...and at the REPL: 193 | 194 | user=> (def worksheets (gsheets/list-worksheets service spreadsheet)) 195 | user=> (map (fn [ws] (.getPlainText (.getTitle ws))) worksheets) 196 | ("Sheet1") 197 | 198 | Our next function returns the cells belonging to a worksheet: 199 | 200 | (defn get-cells 201 | [service worksheet] 202 | (map (memfn getCell) (.getEntries (.getFeed service (.getCellFeedUrl worksheet) CellFeed)))) 203 | 204 | This gives us a flat list of `Cell` objects. It will be much more convenient to work in Clojure with a nested vector of the cell values: 205 | 206 | (defn to-nested-vec 207 | [cells] 208 | (mapv (partial mapv (memfn getValue)) (partition-by (memfn getRow) cells))) 209 | 210 | We now have all the building blocks for the function that will be the main entry point to our minimal Clojure API: 211 | 212 | (defn fetch-worksheet 213 | [service {spreadsheet-title :spreadsheet worksheet-title :worksheet}] 214 | (if-let [spreadsheet (find-spreadsheet-by-title service spreadsheet-title)] 215 | (if-let [worksheet (find-worksheet-by-title service spreadsheet worksheet-title)] 216 | (to-nested-vec (get-cells service worksheet)) 217 | (throw (Exception. (format "Spreadsheet '%s' has no worksheet '%s'" 218 | spreadsheet-title worksheet-title)))) 219 | (throw (Exception. (format "Spreadsheet '%s' not found" spreadsheet-title))))) 220 | 221 | With this in hand: 222 | 223 | user=> (def sheet (gsheets/fetch-worksheet service {:spreadsheet "Colour Counts" :worksheet "Sheet1"})) 224 | #'user/sheet 225 | user=> (clojure.pprint/pprint sheet) 226 | [["Colour" "Count"] 227 | ["red" "123"] 228 | ["orange" "456"] 229 | ["yellow" "789"] 230 | ["green" "101112"] 231 | ["blue" "131415"] 232 | ["indigo" "161718"] 233 | ["violet" "192021"]] 234 | nil 235 | 236 | Our `to-nested-vec` function returns the cell values as strings. I could have used the `getNumericValue` method instead of `getValue`, but then `to-nested-vec` would have to know what data type to expect in each cell. Instead, I used [Plumatic Schema](https://github.com/plumatic/schema) to define a schema for each row, and used its [data coercion](http://plumatic.github.io/schema-0-2-0-back-with-clojurescript-data-coercion/) features to coerce each column to the desired data type - but that's a blog post for another day. 237 | 238 | Code for the examples above is available on Github . We have barely scratched the surface of the Google Spreadsheets API; check out the [API Documentation](https://developers.google.com/google-apps/spreadsheets/) if you need to extend this code, for example to create or update spreadsheets. 239 | --------------------------------------------------------------------------------