Skip to content

Instantly share code, notes, and snippets.

@maxweber
Created August 15, 2023 18:11
Show Gist options
  • Save maxweber/e573221fe421aa78046bd4b03119f783 to your computer and use it in GitHub Desktop.
Save maxweber/e573221fe421aa78046bd4b03119f783 to your computer and use it in GitHub Desktop.
Send requests to the Google Sheets API (v4).
(ns app.metric.google-sheets
"Allows to send requests to the Google Sheets API (v4).
See: https://developers.google.com/sheets/api/reference/rest"
(:require [sv.gcloud.client :as gcloud-client]
[app.env :as env]))
(defn create-client
[params]
(gcloud-client/create-client
(let [scopes ["https://www.googleapis.com/auth/spreadsheets"]]
(merge
(when-let [service-account (env/get-env "GCLOUD_SERVICE_ACCOUNT")]
{:get-access-token (gcloud-client/impersonate-get-access-token
{:scopes scopes
:service-account service-account})})
{:scopes scopes}
params))))
(defn batch-update-request
"Builds a request-map that applies one or more updates to the
spreadsheet.
See: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate"
[{:keys [id body]}]
(assert id)
{:request-method :post
:url (format "https://sheets.googleapis.com/v4/spreadsheets/%s/values:batchUpdate"
id)
:form-params (merge {:valueInputOption "USER_ENTERED"}
body)
:content-type :json
:as :json
})
(comment
((create-client {})
(batch-update-request
{:id "1e-b4NH8aj-Ge8YYic..."
:body {:data [{:range "Sheet1"
:values [["hello" 1]
["world" (rand-int 10000)]]}]}}))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment