# `build_xlsx_workbook` — Build Styled Multi-Sheet XLSX Workbook

> Part of [Sheet SQL & Multi-Tab XLSX Engine API](https://sheet-sql.apikeg.com/llms.txt) (`com.apikeg/sheet-sql`)

Compiles 1–20 structured JSON datasets into a genuine Office Open XML (.xlsx) workbook with bold shaded headers, frozen top rows, auto-sized columns, numeric formatting, formula-injection protection, and live Excel summary formulas (SUM, AVERAGE, MIN, MAX, COUNT).

**Why call instead of DIY:** Generates compliant PKZIP/OpenXML spreadsheets at the edge in <15ms without Python, openpyxl, SheetJS bloat, or broken CSV exports.

- **REST Endpoint:** `POST https://sheet-sql.apikeg.com/v1/build-xlsx-workbook`
- **MCP Server:** `https://sheet-sql.apikeg.com/mcp` (tool: `build_xlsx_workbook`)
- **Pricing:** $0.003 USD per multi-sheet .xlsx workbook generation (x402 instant micropayment)
- **Free Dry-Run:** `POST https://sheet-sql.apikeg.com/v1/build-xlsx-workbook?dry_run=true`

## Input JSON Schema
```json
{
  "type": "object",
  "properties": {
    "fileName": {
      "type": "string",
      "default": "workbook.xlsx",
      "minLength": 1,
      "maxLength": 128,
      "description": "Output .xlsx filename metadata (automatically suffixed with .xlsx if omitted)."
    },
    "sheets": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "description": "Worksheet tab name (sanitized and capped at Excel's 31-character limit)."
          },
          "columns": {
            "type": "array",
            "items": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128
            },
            "minItems": 1,
            "maxItems": 200,
            "description": "Optional explicit column order. If omitted, column keys are inferred from `rows` in insertion order."
          },
          "headers": {
            "type": "object",
            "additionalProperties": {
              "type": "string",
              "minLength": 1,
              "maxLength": 128
            },
            "description": "Optional human-friendly header display labels keyed by column name (e.g. { mrr_usd: 'MRR (USD)' })."
          },
          "rows": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {},
              "additionalProperties": true,
              "description": "Row object mapping column names to typed cell values (number, string, boolean, or null)."
            },
            "minItems": 0,
            "maxItems": 10000,
            "description": "Structured JSON rows to populate on this worksheet."
          },
          "freezeHeader": {
            "type": "boolean",
            "default": true,
            "description": "Freeze top header row (`<pane ySplit=\"1\" state=\"frozen\"/>`)."
          },
          "coerceCurrencyNumbers": {
            "type": "boolean",
            "default": false,
            "description": "Coerce currency-formatted string cells (e.g. '$1,250.50') into typed numeric Excel cells."
          },
          "allowCellFormulas": {
            "type": "boolean",
            "default": false,
            "description": "When true, string cells starting with '=' (e.g. '=B2*C2') emit native OpenXML <f> formulas; when false, they stay literal strings to prevent formula injection."
          },
          "summaryRowFormulas": {
            "type": "object",
            "additionalProperties": {
              "type": "string",
              "enum": [
                "SUM",
                "AVERAGE",
                "MIN",
                "MAX",
                "COUNT",
                "COUNTA"
              ],
              "description": "Spreadsheet summary aggregation function (SUM, AVERAGE, MIN, MAX, COUNT, COUNTA)."
            },
            "description": "Optional summary row formulas keyed by column name (e.g. { revenue: 'SUM', deal_size: 'AVERAGE' })."
          },
          "summaryRowLabel": {
            "type": "string",
            "minLength": 1,
            "maxLength": 64,
            "description": "Optional label placed in column A of the summary row (default 'Total')."
          }
        },
        "required": [
          "name",
          "rows"
        ],
        "additionalProperties": false,
        "description": "Worksheet definition for an OpenXML .xlsx workbook."
      },
      "minItems": 1,
      "maxItems": 20,
      "description": "1 to 20 worksheets to compile into the multi-tab .xlsx archive."
    }
  },
  "required": [
    "sheets"
  ],
  "additionalProperties": false
}
```

## Examples
### Generate a two-tab CFO revenue workbook with SUM and AVERAGE formulas
```bash
curl -X POST "https://sheet-sql.apikeg.com/v1/build-xlsx-workbook" \
  -H "Content-Type: application/json" \
  -d '{"fileName":"q4-revenue-summary.xlsx","sheets":[{"name":"Regional Revenue","freezeHeader":true,"coerceCurrencyNumbers":true,"allowCellFormulas":false,"summaryRowFormulas":{"q4_revenue":"SUM","win_rate":"AVERAGE"},"rows":[{"region":"North America","q4_revenue":125000.5,"win_rate":0.34},{"region":"Europe","q4_revenue":84500.25,"win_rate":0.29}]}]}'
```
Expected output: Returns base64-encoded OpenXML .xlsx archive with frozen headers, auto-sized columns, numeric formatting, and =SUM(B2:B3) / =AVERAGE(C2:C3) summary row.
