Sheetshow.Coord (Sheetshow v0.1.0)

Copy Markdown View Source

Where a cell is: a 0-indexed row and col, and optionally the sheet (tab) it belongs to.

iex> Sheetshow.Coord.new(3, 1, "Costs")
%Sheetshow.Coord{row: 3, col: 1, sheet: "Costs"}
iex> Sheetshow.Coord.new(3, 1, "Costs") |> Sheetshow.Coord.to_a1()
"Costs!B4"

Coordinates sort row-major within a sheet, so Enum.sort(coords, Sheetshow.Coord) gives reading order.

Summary

Functions

Row-major order within a sheet; sheets order by name, nil first. For Enum.sort/2 and friends.

Parses a single-cell A1 reference.

Same as from_a1/1, raising on failure.

Builds a coordinate.

Moves a coordinate by rows down and cols right. Negative deltas move up and left; moving off the sheet raises ArgumentError.

Prints as A1, with the sheet when there is one.

Types

t()

@type t() :: %Sheetshow.Coord{
  col: non_neg_integer(),
  row: non_neg_integer(),
  sheet: String.t() | nil
}

Functions

compare(a, b)

@spec compare(t(), t()) :: :lt | :eq | :gt

Row-major order within a sheet; sheets order by name, nil first. For Enum.sort/2 and friends.

iex> coords = [Sheetshow.Coord.new(1, 0), Sheetshow.Coord.new(0, 5), Sheetshow.Coord.new(0, 2)]
iex> Enum.sort(coords, Sheetshow.Coord) |> Enum.map(&Sheetshow.Coord.to_a1/1)
["C1", "F1", "A2"]

from_a1(a1)

@spec from_a1(String.t()) :: {:ok, t()} | {:error, Sheetshow.Error.t()}

Parses a single-cell A1 reference.

iex> Sheetshow.Coord.from_a1("B4")
{:ok, %Sheetshow.Coord{row: 3, col: 1, sheet: nil}}
iex> Sheetshow.Coord.from_a1("'Q1 costs'!$B$4")
{:ok, %Sheetshow.Coord{row: 3, col: 1, sheet: "Q1 costs"}}
iex> {:error, %Sheetshow.Error{reason: :invalid_a1}} = Sheetshow.Coord.from_a1("A1:B2")

from_a1!(a1)

@spec from_a1!(String.t()) :: t()

Same as from_a1/1, raising on failure.

new(row, col, sheet \\ nil)

@spec new(non_neg_integer(), non_neg_integer(), String.t() | nil) :: t()

Builds a coordinate.

iex> Sheetshow.Coord.new(0, 0)
%Sheetshow.Coord{row: 0, col: 0, sheet: nil}

shift(coord, rows, cols)

@spec shift(t(), integer(), integer()) :: t()

Moves a coordinate by rows down and cols right. Negative deltas move up and left; moving off the sheet raises ArgumentError.

iex> Sheetshow.Coord.new(1, 1) |> Sheetshow.Coord.shift(2, -1)
%Sheetshow.Coord{row: 3, col: 0, sheet: nil}

to_a1(coord)

@spec to_a1(t()) :: String.t()

Prints as A1, with the sheet when there is one.

iex> Sheetshow.Coord.to_a1(%Sheetshow.Coord{row: 0, col: 26})
"AA1"
iex> Sheetshow.Coord.to_a1(%Sheetshow.Coord{row: 0, col: 0, sheet: "Q1 costs"})
"'Q1 costs'!A1"