NL2Formula: Generating Spreadsheet Formulas from Natural Language Queries

Wei Zhao, Zhitao Hou, Siyuan Wu, Yang Gao, Haoyu Dong, Yao Wan, Hongyu Zhang, Yulei Sui, Haidong Zhang

Abstract

Writing formulas on spreadsheets, such as Microsoft Excel and Google Sheets, is a widespread practice among users performing data analysis. However, crafting formulas on spreadsheets remains a tedious and error-prone task for many end-users, particularly when dealing with complex operations. To alleviate the burden associated with writing spreadsheet formulas, this paper introduces a novel benchmark task called NL2Formula, with the aim to generate executable formulas that are grounded on a spreadsheet table, given a Natural Language (NL) query as input. To accomplish this, we construct a comprehensive dataset consisting of 70,799 paired NL queries and corresponding spreadsheet formulas, covering 21,670 tables and 37 types of formula functions. We realize the NL2Formula task by providing a sequence-to-sequence baseline implementation called fCoder. Experimental results validate the effectiveness of fCoder, demonstrating its superior performance compared to the baseline models. Furthermore, we also compare fCoder with an initial GPT-3.5 model (i.e., text-davinci-003). Lastly, through in-depth error analysis, we identify potential challenges in the NL2Formula task and advocate for further investigation.

Introduction

Spreadsheets are the backbone of modern data analysis, yet writing complex formulas remains a significant barrier for many users. While tools like FlashFill have simplified basic string manipulations, they often fail when faced with multi-step analytical logic—such as calculating averages based on specific conditional criteria.

Unlike the well-established Text2SQL task, which translates natural language into database queries, the NL2Formula task introduces unique challenges. Spreadsheet tables lack rigid database schemas and can be positioned anywhere on a grid. Furthermore, formulas must reference specific cell coordinates (e.g., A2:B11) rather than abstract column names, requiring the model to have a deep spatial understanding of the table layout.

Examples of the NL2Formula task, showing (a) an analysis query requiring complex logic and (b) a calculation query based on specific cell values.

Methodology and Dataset Construction

The researchers treat formula generation as a sequence-to-sequence problem, introducing $f$CODER, a framework based on the T5 (Text-to-Text Transfer Transformer) architecture. To enable the model to process spreadsheet data, the grid is flattened into a sequence containing row and column indices alongside cell content, which is then concatenated with the user's natural language query.

Data Generation via SQL-to-Formula

Because high-quality natural language to spreadsheet formula data is scarce, the authors developed a sophisticated rule-based converter. This system takes existing Text2SQL datasets (like WikiSQL and Spider) and programmatically translates SQL logic into equivalent Excel functions. For example, a WHERE clause combined with a MAX aggregate might be converted into a MAXIFS function or a combination of MIN and FILTER functions.

Rule-based conversion logic used to translate SQL queries into executable spreadsheet formulas.

The resulting NL2Formula Dataset contains 70,799 pairs of NL queries and formulas across 21,670 tables, covering 37 different formula functions categorized by difficulty (Simple, Medium, Complex).

Key Insights and Results

The study yielded several critical findings regarding how AI models interact with spreadsheets:

  • Layout Sensitivity: Models currently rely heavily on absolute coordinates. When a table was shifted from cell A1 to B2, accuracy dropped to just 6.7%, suggesting that models "memorize" positions rather than understanding spatial relationships.
  • Performance Benchmarks: The $f$CODER-Large model achieved 70.6% Exact Match (EM) and 77.1% Execution Accuracy (ERA). It significantly outperformed general-purpose models like GPT-3.5, which struggled with the specific syntax of "Analysis" category formulas.
  • Execution vs. Syntax: Many formulas that failed the "Exact Match" test still produced the correct numerical result. This highlights the flexibility of the Excel formula language, where multiple valid syntax paths can lead to the same answer.

Conclusion and Future Work

While $f$CODER represents a major step forward, several challenges remain. Current models are limited by context windows (512 tokens), making them unsuitable for massive spreadsheets. Furthermore, the models struggle with "layout-agnostic" reasoning and multi-table lookups. Future research will focus on expanding function coverage and improving the model's ability to handle tables regardless of their physical location on the grid.

Summary updated

BibTeX
@inproceedings{zhao2024nl,
  author = {Wei Zhao and Zhitao Hou and Siyuan Wu and Yang Gao and Haoyu Dong and Yao Wan and Hongyu Zhang and Yulei Sui and Haidong Zhang},
  title = {{NL2Formula: Generating Spreadsheet Formulas from Natural Language Queries}},
  booktitle = {EACL (Findings)},
  year = {2024},
  url = {https://openreview.net/forum?id=hBNsJtju8J},
}
Figure