Passing variables to @macros

Greetings, friends:

(Preface)
Excel is outdated. I thought I loved LET() and LAMBDA(), but I can no longer spend my waking life with VBA and reflectionless formulas. Just in the same way that Julia was created to satisfy greedy linguists, I should not feel ashamed for wanting Julian spreadsheets.

In reviewing similar topics to this request, I see that macros may not currently have direct access to named values/variables. My inchoate dream is to narrow down a declarative spreadsheet dialect that uses the tersest-possible syntax to avoid frightening my non-developer coworkers with “code” (Excel’s formula bar is essentially a DSL, but I digress). By creating the spreadsheet anew upon each run, I wonder if this would allow for quicker structural edits, which currently occupy far too much of my time, by faking immutability through a sort of Thesean ship approach.

(Telos)
Is it at all possible to pass values directly to @macro calls? I originally tried instantiating cell functions dynamically to avoid the infamous macro compilation overhead (e.g., “A1(expr)”, “B2(expr)”, …, depending on the underlying file shape and size), but the macro syntax is too clean by comparison. It feels like constructing spreadsheets directly from the command line, and I want to keep it. Can I eat my cake, or merely claim possession?

Thank you all in advance for your constructive and/or destructive criticism!

Looking at the screenshot, the macros are attempting things that should be done by functions.

Then dedicate the macro to handling the syntax:

macro cell(cell_reference, value, sheet)
  # cell_reference treated as input symbol, not variable
  # value is likely never a symbol, but if it is it'd be treated as a variable
  # sheet treated as a variable
  # esc(input) to get input from call scope, not definition scope
  quote
    _cell( $( Expr(:quote, cell_reference) ), $(esc(value)), $(esc(sheet)) )
  end
end

macro cell(cell_reference, value)
  quote
    _cell( $( Expr(:quote, cell_reference) ), $(esc(value)) )
  end
end

function _cell(cell_reference, value, sheet=new_sheet)
  # your previous cell code
end
julia> @macroexpand @cell A1 47 name # peek at output expression
quote
    #= REPL[7]:7 =#
    Main._cell(:A1, 47, name)
end
3 Likes

Thank you for this! After a quick sleep on the matter I realized I plainly transgressed my own cardinal rule: DTSTTCPW.

By initially optimizing for “fewest possible tokens per cell/range assignment”, I may be prematurely sacrificing the benefits of broadcasting which will likely pay dividends down the line.