Skip to content

Instantly share code, notes, and snippets.

@gccoding
Last active June 8, 2017 16:59
Show Gist options
  • Save gccoding/573c7927651dc87f28bd15bba1f1ee76 to your computer and use it in GitHub Desktop.
Save gccoding/573c7927651dc87f28bd15bba1f1ee76 to your computer and use it in GitHub Desktop.
Writing cell data into a Spreadsheet
# Writing or updating the value into a given cell in a spreadsheet containing multiple sheets
# Reference : https://github.com/Voronchuk/elixir_google_spreadsheets/blob/master/lib/elixir_google_spreadsheets/spreadsheet.ex
# GSS.Spreadsheet.write_value(pid, "Sheet_Name", "Cell_Id",["Cell_value"])
# Eg. GSS.Spreadsheet.write_value(pid, "Sheet A", "K17:K17",["777"])
#
# For reading, you can use GSS.Spreadsheet.fetch(pid, "Sheet A!K17:K17")
#
def handle_call(
{:write_value, sheet_name, cell_id, data, options},
_from,
%{spreadsheet_id: spreadsheet_id} = state
) do
value_input_option = Keyword.get(options, :value_render_option, "USER_ENTERED")
range = "#{sheet_name}!#{cell_id}"
query = "#{spreadsheet_id}/values/#{range}?valueInputOption=#{value_input_option}"
case spreadsheet_query(:put, query, data, options ++ [range: range]) do
{:json, %{"updatedRows" => 1, "updatedColumns" => updated_columns}}
when updated_columns == 1 ->
{:reply, :ok, state}
{:error, exception} ->
{:reply, {:error, exception}, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment