Stop Sheets from turning script text into formulas

TL;DR — CSV notes, Form answers, and API strings that start with = + - or @ become live formulas the moment your script writes them. Escape those strings before setValues so Logs and imports stay plain text — built on escapeLeadingFormula.

You pasted a vendor CSV into a staging tab with Apps Script. Most rows looked fine. One “Notes” cell started with =HYPERLINK("https://…"). Sheets evaluated it. Another row had a phone number stored as the string +15551212 and Sheets turned it into a number (or a formula error). Nobody attacked you on purpose — but the sheet now has live formulas and mangled values you didn’t ask for.

Formula injection (and its quieter cousin, signed-number coercion) happens whenever untrusted or external text hits setValue / setValues without a guard. The fix is tiny and boring: escape leading formula characters before you write.

TL;DR

  • Strings starting with =, +, -, @, or a tab make Sheets parse the cell as a formula or signed number
  • Run free-text fields through escapeLeadingFormula before setValues
  • The helper prefixes a '; Sheets stores plain text and getValue() returns the string without the quote
  • Leave real numbers as numbers; only escape strings you mean as text
  • Apply it on every import / Log / Notes path, not just the one that bit you

Using the Apps Script editor a lot? NitroGAS drops free themes & snippets right into script.google.com — optional Co-Pilot when you want a boost.

What goes wrong

// Looks innocent. It isn't, if note can come from a CSV or a Form. sheet.getRange(row, noteCol).setValue(note);
Input string What Sheets often does
=1+1 Live formula → 2
=HYPERLINK("…") Clickable link (or an error)
+15551212 Number / formula-ish coercion
-Summary Sometimes a formula parse attempt
@mention leftover Newer Sheets formula syntax in some locales

Your script didn’t “run a formula.” Sheets did, because the cell content looked like one.

What not to do

  • Don’t try to strip leading = only — +, -, @, and tab are in the same family
  • Don’t HTML-escape and call it done; this is a Sheets parse problem, not an HTML one
  • Don’t escape numeric columns you need for SUM — coerce those to real numbers on purpose
  • Don’t rely on “our vendors would never” — paste errors and exports from other tools do this constantly

The pattern

function writeNotesColumn(sheet, rows) { // rows: [{ _row, note }, ...] from a CSV / Form / API var values = rows.map(function (r) { return [escapeLeadingFormula(r.note)]; }); sheet.getRange(2, 4, values.length, 1).setValues(values); }

The whole helper:

function escapeLeadingFormula(value) { if (value === null || value === undefined) return value; if (typeof value !== 'string') return value; if (/^[=+\-@\t]/.test(value)) return "'" + value; return value; }

Full notes and edge cases live on the escape-leading-formula snippet page.

Escape by column, not by whole row

A row object usually mixes types: Amount should stay a number, Status a short string, Notes free text. Escape the free-text keys explicitly:

var TEXT_COLS = ['Notes', 'Subject', 'Raw payload']; function sanitizeRow_(row) { var out = {}; Object.keys(row).forEach(function (k) { out[k] = row[k]; }); TEXT_COLS.forEach(function (k) { if (k in out) out[k] = escapeLeadingFormula(out[k]); }); return out; }

That pairs cleanly with header-mapped writes (write-block-leaving-formula-cols if you need to leave formula columns untouched).

Imports and Logs are the hot paths

  • CSV / Drive import: escape every text column as you build the setValues block
  • Form → sheet: especially short-answer and paragraph fields
  • Error / Log tabs: stack traces rarely start with =, but pasted request bodies do
  • Mirror / copy jobs: if the source cell already shows text, getValue() is usually fine — still escape if you’re copying display strings from an untrusted system

Soft Co-Pilot note

NitroGAS keeps escapeLeadingFormula a search away. Paste your import function and the header list, and Co-Pilot can wrap the free-text columns for you. You still spot-check one poisoned row on purpose (=1+1 in Notes) before you trust the path. Free core; Co-Pilot is a pass when you want the draft.

Closing checklist

  • Free-text columns run through escapeLeadingFormula before setValues
  • Numeric columns are real numbers (or deliberately left alone)
  • A test row with =1+1 and +15551212 stays plain text after import
  • Log / Notes / Subject paths are covered, not just the happy-path import

Happy Coding!