Sheet Schema For Prompt

Ask any assistant to write a script for “the Orders sheet” and it will happily invent a Customer Email column you don't have. sheetSchemaForPrompt(sheet, opts) gives it the truth: plain text with the sheet name, every header with its column letter, and N sample rows with long values truncated. opts.headerOnly returns the headers alone when the data is private.

When to use

  • Briefing NitroGAS Co-Pilot (or anyone) before asking for a script that touches a sheet
  • Bug reports: “here's exactly what the sheet looks like”
  • Documentation for a handover

How to use this snippet

/** * Describe a sheet's columns (and a few sample rows) as plain text for a prompt. * * @param {Sheet} sheet * @param {Object=} opts { sampleRows: 1, maxCellChars: 40, headerOnly: false } * @return {string} */ function sheetSchemaForPrompt(sheet, opts) { opts = opts || {}; var lastCol = sheet.getLastColumn(); if (!lastCol) return 'Sheet "' + sheet.getName() + '" is empty.'; var n = opts.headerOnly ? 0 : (opts.sampleRows == null ? 1 : opts.sampleRows); var max = opts.maxCellChars || 40; var rows = sheet.getRange(1, 1, Math.min(Math.max(sheet.getLastRow(), 1), n + 1), lastCol).getDisplayValues(); function letter(i) { var s = ''; i++; while (i) { s = String.fromCharCode(65 + (i - 1) % 26) + s; i = Math.floor((i - 1) / 26); } return s; } function cut(v) { v = String(v); return v.length > max ? v.slice(0, max - 1) + '…' : v; } var lines = ['Sheet "' + sheet.getName() + '" columns:']; rows[0].forEach(function (h, i) { lines.push(' ' + letter(i) + ': ' + (h || '(blank header)')); }); if (rows.length > 1) { lines.push('Sample rows:'); rows.slice(1).forEach(function (r) { lines.push(' ' + r.map(cut).join(' | ')); }); } return lines.join('\n'); }

Example

function logOrdersSchema() { var sh = SpreadsheetApp.getActive().getSheetByName('Orders'); console.log(sheetSchemaForPrompt(sh, { sampleRows: 1 })); } // Sheet "Orders" columns: // A: Id // B: Email // C: Total // Sample rows: // 1042 | ana@example.com | 129.00

Tips:

  • Uses getDisplayValues(), so dates and currency look the way they do on screen.
  • Swap real customer data for made-up values before pasting, or use headerOnly: true.
  • Blank headers show up as (blank header): a good hint to name them before asking for code.

Tip: run it, copy the Log, and paste it at the top of your NitroGAS Co-Pilot brief so the draft uses your real column names.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free themes and snippets — plus optional Co-Pilot when you want a boost (1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension