From e367ff170b5998ff4a28b261b4088f0eee5a6c54 Mon Sep 17 00:00:00 2001 From: milad Date: Wed, 16 Aug 2017 13:24:59 +0430 Subject: [PATCH] added 3 examples for google sheets API these 3 minimalistic examples should demonstrate how to use google sheets APIs. There are also some explanations for each of the examples. --- examples/gogol-examples.cabal | 3 ++ examples/src/Example/Sheets.hs | 54 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 examples/src/Example/Sheets.hs diff --git a/examples/gogol-examples.cabal b/examples/gogol-examples.cabal index 93702fc5ce..779096a696 100644 --- a/examples/gogol-examples.cabal +++ b/examples/gogol-examples.cabal @@ -19,6 +19,7 @@ library exposed-modules: Example.Storage + , Example.Sheets build-depends: base >= 4.7 @@ -28,6 +29,8 @@ library , gogol == 0.3.0.* , gogol-core , gogol-storage + , gogol-sheets , lens , resourcet , text + , aeson diff --git a/examples/src/Example/Sheets.hs b/examples/src/Example/Sheets.hs new file mode 100644 index 0000000000..40cde62c6e --- /dev/null +++ b/examples/src/Example/Sheets.hs @@ -0,0 +1,54 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Example.Sheets where + +--------------------------------------------------------------------------------- +import Network.Google.Resource.Sheets.Spreadsheets.Get +import Network.Google.Sheets +import Network.Google + +import Control.Lens ((.~), (<&>)) +import Data.Text (Text) +import System.IO (stdout) +import Data.Aeson.Types + +--------------------------------------------------------------------------------- + +-- | +-- This gets the Information about an spreadsheet. +-- In order to be able to run these examples you need to +-- create a service acount from google's developers console +-- and copy the dowloaded json file to ~/.config/gcloud/application_default_credentials.json. +-- +-- you must also share with your service the spreadsheet that you want to get the info of. +-- In order to do this you must share the sheet with the email address of your service +-- which is in your downloaded service config file. +-- +-- after doing above step just pass the sreadsheet id to the function. +exampleGetSheet :: Text -> IO Spreadsheet +exampleGetSheet sheetID = do + lgr <- newLogger Debug stdout + env <- newEnv <&> (envLogger .~ lgr) . (envScopes .~ spreadsheetsScope) + runResourceT . runGoogle env $ + send (spreadsheetsGet sheetID ) + +-- | +-- you pass the sheet id and a range (eg. "sheet1!A1:C3") in that sheet +-- and it retreives the values in the specified range +exampleGetValue :: Text -> Text -> IO ValueRange +exampleGetValue sheetID range = do + lgr <- newLogger Debug stdout + env <- newEnv <&> (envLogger .~ lgr) . (envScopes .~ spreadsheetsScope) + runResourceT . runGoogle env $ + send (spreadsheetsValuesGet sheetID range ) + + +exampleAppendValue :: Text -> Text -> [[Value]] -> IO AppendValuesResponse +exampleAppendValue sheetID range val = do + lgr <- newLogger Debug stdout + env <- newEnv <&> (envLogger .~ lgr) . (envScopes .~ spreadsheetsScope) + runResourceT . runGoogle env $ + send (svaValueInputOption .~ Just "USER_ENTERED" $ spreadsheetsValuesAppend + sheetID + ( vrMajorDimension .~ Just VRMDRows $ vrValues .~ val $ vrRange .~ Just range $ valueRange) + range )