AgentSkillsCN

Advanced Formulas

高级公式

SKILL.md

Advanced XLSX Formulas

Modern Excel 365/2024+ formula capabilities for custom functions, array manipulation, and advanced data processing.

Version Requirements

Function GroupMinimum Version
LAMBDA, LETExcel 365, Excel 2021
LAMBDA Helpers (MAP, REDUCE, SCAN, etc.)Excel 365, Excel 2024
Array Functions (HSTACK, TAKE, TEXTSPLIT, etc.)Excel 365, Excel 2024
GROUPBY, PIVOTBYExcel 365 (Preview)
Regex FunctionsExcel 365 (Preview)

Pre-Flight Validation Checklist

Before outputting advanced formulas, verify:

code
- [ ] Excel version supports the function (check table above)
- [ ] LAMBDA parameters use valid identifiers (letters, numbers, underscores)
- [ ] LET variable names don't conflict with cell references (avoid single letters)
- [ ] Array dimensions are compatible for operations
- [ ] Spill range has sufficient empty cells for output
- [ ] Regex patterns follow PCRE2 syntax
- [ ] Nested functions don't exceed 64 levels

LAMBDA Function System

Create custom reusable functions without VBA.

Basic LAMBDA Syntax

code
=LAMBDA([parameter1, parameter2, ...], calculation)

Inline usage:

code
=LAMBDA(x, x^2)(5)

Returns: 25

Named LAMBDA (Reusable)

Define in Name Manager (Formulas > Name Manager):

  • Name: SQUARE
  • Refers to: =LAMBDA(x, x^2)

Then use anywhere:

code
=SQUARE(5)

LAMBDA with Multiple Parameters

code
=LAMBDA(base, rate, years, base * (1 + rate) ^ years)

Optional Parameters with ISOMITTED

code
=LAMBDA(value, [decimals],
  IF(ISOMITTED(decimals),
    ROUND(value, 2),
    ROUND(value, decimals)
  )
)

LAMBDA Helper Functions

FunctionPurposeReturns
MAPApply function to each elementArray (same size)
REDUCEAccumulate to single valueSingle value
SCANAccumulate showing each stepArray (same size)
MAKEARRAYGenerate array with custom logicNew array
BYROWApply function to each rowColumn array
BYCOLApply function to each columnRow array

MAP - Transform Each Element

code
=MAP(array, LAMBDA(x, transformation))
=MAP(A1:A10, LAMBDA(x, x * 1.1))

Multiple arrays:

code
=MAP(A1:A10, B1:B10, LAMBDA(a, b, a * b))

REDUCE - Accumulate Values

code
=REDUCE(initial_value, array, LAMBDA(accumulator, current, operation))
=REDUCE(0, A1:A10, LAMBDA(acc, val, acc + val))

Product of all values:

code
=REDUCE(1, A1:A10, LAMBDA(acc, val, acc * val))

SCAN - Running Accumulation

code
=SCAN(initial_value, array, LAMBDA(accumulator, current, operation))
=SCAN(0, A1:A10, LAMBDA(acc, val, acc + val))

Returns running total at each position.

MAKEARRAY - Generate Custom Array

code
=MAKEARRAY(rows, cols, LAMBDA(row, col, calculation))
=MAKEARRAY(5, 5, LAMBDA(r, c, r * c))

Generates multiplication table.

BYROW / BYCOL - Row/Column Operations

Sum each row:

code
=BYROW(A1:C10, LAMBDA(row, SUM(row)))

Max of each column:

code
=BYCOL(A1:C10, LAMBDA(col, MAX(col)))

LET Function for Optimization

Define named variables within formulas to improve performance and readability.

Basic Syntax

code
=LET(name1, value1, [name2, value2, ...], calculation)

Eliminate Duplicate Calculations

Before (calculates XLOOKUP twice):

code
=IF(XLOOKUP(A1, B:B, C:C) > 100, XLOOKUP(A1, B:B, C:C) * 0.1, 0)

After (calculates once):

code
=LET(
  price, XLOOKUP(A1, B:B, C:C),
  IF(price > 100, price * 0.1, 0)
)

Multi-Step Calculations

code
=LET(
  sales, SUM(B2:B100),
  costs, SUM(C2:C100),
  profit, sales - costs,
  margin, profit / sales,
  TEXT(margin, "0.0%")
)

Variable Naming Rules

  • Must start with letter
  • Can contain letters, numbers, underscores
  • Cannot match cell references (avoid: A1, R1C1, single letters)
  • Case-insensitive

Modern Array Functions

Combining Arrays

HSTACK (horizontal):

code
=HSTACK(A1:A10, B1:B10, C1:C10)

VSTACK (vertical):

code
=VSTACK(A1:C1, A10:C10, A20:C20)

Extracting Portions

TAKE (from start/end):

code
=TAKE(A1:D100, 5)           // first 5 rows
=TAKE(A1:D100, -5)          // last 5 rows
=TAKE(A1:D100, 5, 2)        // first 5 rows, first 2 cols
=TAKE(A1:D100, , -1)        // last column only

DROP (remove from start/end):

code
=DROP(A1:D100, 1)           // remove header row
=DROP(A1:D100, , 1)         // remove first column
=DROP(A1:D100, 1, -1)       // remove first row and last column

Selecting Specific Rows/Columns

CHOOSEROWS:

code
=CHOOSEROWS(A1:D100, 1, 5, 10)       // rows 1, 5, 10
=CHOOSEROWS(A1:D100, -1, -2, -3)     // last 3 rows reversed

CHOOSECOLS:

code
=CHOOSECOLS(A1:D100, 4, 1, 2)        // columns D, A, B in that order

Reshaping Arrays

TOCOL (flatten to column):

code
=TOCOL(A1:C10)                       // all values in one column
=TOCOL(A1:C10, 1)                    // ignore blanks
=TOCOL(A1:C10, 2)                    // ignore errors
=TOCOL(A1:C10, 3)                    // ignore blanks and errors

TOROW (flatten to row):

code
=TOROW(A1:C10)

WRAPROWS (1D to 2D by rows):

code
=WRAPROWS(A1:A12, 4)                 // 12 values into 3 rows x 4 cols
=WRAPROWS(A1:A10, 4, "-")            // pad incomplete row with "-"

WRAPCOLS (1D to 2D by columns):

code
=WRAPCOLS(A1:A12, 3)                 // 12 values into 3 rows x 4 cols

EXPAND (resize with padding):

code
=EXPAND(A1:B2, 5, 5)                 // expand to 5x5 with #N/A
=EXPAND(A1:B2, 5, 5, 0)              // expand to 5x5 with 0

Advanced Text Functions

TEXTSPLIT

Split by delimiter:

code
=TEXTSPLIT(A1, " ")                  // split by space
=TEXTSPLIT(A1, ", ")                 // split by comma-space
=TEXTSPLIT(A1, {",", ";"})           // split by comma OR semicolon

Split into rows and columns:

code
=TEXTSPLIT(A1, ",", ";")             // comma for cols, semicolon for rows

TEXTBEFORE / TEXTAFTER

Extract portions:

code
=TEXTBEFORE("John Smith", " ")       // "John"
=TEXTAFTER("John Smith", " ")        // "Smith"
=TEXTBEFORE("a-b-c-d", "-", 2)       // "a-b" (before 2nd delimiter)
=TEXTAFTER("a-b-c-d", "-", -1)       // "d" (after last delimiter)

Regex Functions (Preview)

REGEXTEST (check pattern):

code
=REGEXTEST(A1, "\d{3}-\d{4}")        // TRUE if phone pattern
=REGEXTEST(A1, "^[A-Z]")             // TRUE if starts with uppercase

REGEXEXTRACT (extract matches):

code
=REGEXEXTRACT(A1, "\d+")             // extract first number
=REGEXEXTRACT(A1, "\d+", 0)          // extract all numbers (array)

REGEXREPLACE (replace pattern):

code
=REGEXREPLACE(A1, "\s+", " ")        // normalize whitespace
=REGEXREPLACE(A1, "[^0-9]", "")      // keep only digits

Aggregation Functions (Preview)

GROUPBY

Group and aggregate data:

code
=GROUPBY(row_fields, values, function)
=GROUPBY(A2:A100, C2:C100, SUM)

Multiple grouping fields:

code
=GROUPBY(HSTACK(A2:A100, B2:B100), C2:C100, SUM)

With options:

code
=GROUPBY(A2:A100, C2:C100, SUM, 3, 0, 0, 1)

PIVOTBY

Create pivot-style summary:

code
=PIVOTBY(row_fields, col_fields, values, function)
=PIVOTBY(A2:A100, B2:B100, C2:C100, SUM)

IMAGE Function

Embed image from URL:

code
=IMAGE(url, [alt_text], [sizing], [height], [width])
=IMAGE("https://example.com/logo.png", "Logo", 0)

Sizing options:

  • 0 = Fit cell, maintain aspect ratio
  • 1 = Fill cell, ignore aspect ratio
  • 2 = Original size
  • 3 = Custom dimensions

Formula Construction Workflow

code
1. IDENTIFY   -> Determine if standard functions suffice or advanced needed
2. OPTIMIZE   -> Use LET to cache repeated calculations
3. MODULARIZE -> Extract reusable logic into named LAMBDA
4. TRANSFORM  -> Use array functions for data reshaping
5. VALIDATE   -> Check version compatibility and spill requirements
6. DOCUMENT   -> Use meaningful LET variable names for clarity

Common Patterns Quick Reference

TaskFormula Pattern
Apply function to each cell=MAP(range, LAMBDA(x, ...))
Running total=SCAN(0, range, LAMBDA(a,b, a+b))
Custom aggregation=REDUCE(init, range, LAMBDA(a,b, ...))
Sum each row=BYROW(range, LAMBDA(r, SUM(r)))
Combine arrays side-by-side=HSTACK(arr1, arr2)
Get top N rows=TAKE(SORT(range, col, -1), N)
Split text to columns=TEXTSPLIT(text, delimiter)
Extract with regex=REGEXEXTRACT(text, pattern)
Group and sum=GROUPBY(categories, values, SUM)

Additional Resources