xou/elixlsx

Please can we have a way to set cell/range properties without changing the cell text

CharlesOkwuagwu opened this issue · 1 comments

Please can we have a way to set cell/range properties without changing the cell text.

|> Sheet.set_cell("A1", bold: true, color: "#ffffff", "Employee Data", bg_color: "#0070C0")

If the content of "A1" is already set, can we have for example:

|> Sheet.format_cell("A1", bold: true, color: "#ffffff", bg_color: "#0070C0")

Here is a workaround.

  def format_cell(%Elixlsx.Sheet{} = sheet, cell, opts) when is_binary(cell) and is_list(opts) do
    {content, applied_opts} = get_cell(sheet, cell)
    Elixlsx.Sheet.set_cell(sheet, cell, content, Keyword.merge(applied_opts, opts))
  end
  
  def get_cell(%Elixlsx.Sheet{} = sheet, cell) do
    {row, col} = Elixlsx.Util.from_excel_coords0(cell)
    get_cell_content(sheet, row, col)
  end

  def get_cell(%Elixlsx.Sheet{} = sheet, row, col) do
    case get_in(sheet.rows, [Access.at(row), Access.at(col)]) do
      nil -> {"", []}
      [content | opts] -> {content, opts}
      content -> {content, []}
    end
  end