Voronchuk / elixir_google_spreadsheets

Elixir library to read and write data of Google Spreadsheets.
MIT License
79 stars 37 forks source link
elixir elixir-library google-spreadsheets service-accounton

Elixir Google Spreadsheets

Elixir library to read and write data of Google Spreadsheets.

This library is based on Google Cloud API v4 and uses Google Service Accounts to manage it's content.

Integration with Ecto

Check ecto_gss if you need to integrate your Google Spreadsheet with Ecto changesets for validation and other features.

Setup

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
  2. On the Add credentials to your project page, create Service account key.
  3. Select your project name as service account and JSON as key format, download the created key and rename it to __service_account.json__.
  4. Press Manage service accounts on a credential page, copy your Service Account Identifier: [projectname]@[domain].iam.gserviceaccount.com
  5. Create or open existing Google Spreadsheet document on your Google Drive and add Service Account Identifier as user invited in spreadsheet's Collaboration Settings.
  6. Add {:elixir_google_spreadsheets, "~> 0.3"} to mix.exs under deps function, add :elixir_google_spreadsheets in your application list.
  7. Add __service_account.json__ in your config.exs or other config file, like dev.exs or prod.secret.exs. config :elixir_google_spreadsheets, json: "./config/service_account.json" |> File.read!
  8. Run mix deps.get && mix deps.compile.

API limits

All Google API limits, suggested params are the following:

config :elixir_google_spreadsheets, :client,
  request_workers: 50,
  max_demand: 100,
  max_interval: :timer.minutes(1),
  interval: 100,
  result_timeout: :timer.minutes(10)

Since elixir 1.14 the following request params are used by default, you can modify them as :request_opts:

  [
    timeout: :timer.seconds(8),
    recv_timeout: :timer.seconds(5),
    ssl: [
      versions: [:"tlsv1.2"],
      verify: :verify_peer,
      depth: 99,
      cacerts: :certifi.cacerts(),
      customize_hostname_check: [
        match_fun: :public_key.pkix_verify_hostname_match_fun(:https)
      ],
      reuse_sessions: false,
      crl_check: true,
      crl_cache: {:ssl_crl_cache, {:internal, [http: 30000]}}
    ]
  ]

Usage

Initialise spreadsheet thread with it's id which you can fetch from URL:

    {:ok, pid} = GSS.Spreadsheet.Supervisor.spreadsheet("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXX")

Or if you wish to edit only a specific list:

    {:ok, pid} = GSS.Spreadsheet.Supervisor.spreadsheet(
        "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXX",
        list_name: "my_list3"
    )

Sample operations:

Last function param of GSS.Spreadsheet function calls support the same Keyword options (in snake_case instead of camelCase), as defined in Google API Docs.

We also define column_from and column_to Keyword options which control range of cell which will be queried.

Default values:

Suggestions