Function Inventory For Prompt

functionInventoryForPrompt() returns a plain-text list of the project's top-level functions: name, argument count, and the leading comment, ready to paste into a Co-Pilot prompt. It reads the project with the Apps Script API (or you can pass {name, type, source} files yourself, for example from Get Script Project Files).

When to use

  • Inheriting a script and wanting a map of what is in it before you change anything
  • Briefing Co-Pilot with the function list next to Trigger Inventory For Prompt
  • Checking names and argument counts before you rename a handler

How to use this snippet

The helper lists unindented function name(...) declarations in SERVER_JS files. Indented or nested functions are omitted. It uses the first paragraph of the comment immediately above each function (/** ... */ or //) and skips @param / @return tags. Enable the Apps Script API in the Cloud project, turn on the Apps Script API toggle at script.google.com/home/usersettings, and add the script.projects.readonly and script.external_request scopes. If Get Script Project Files is in the same project, the helper calls it; otherwise it fetches the files itself.

/** * Plain-text list of this project's top-level functions for a Co-Pilot brief: * name, argument count, and leading comment. Pass files from * getScriptProjectFiles(scriptId) to skip the fetch; otherwise it reads the * current project via the Apps Script API. * * @param {{name: string, type: string, source: string}[]} [files] * @return {string} */ function functionInventoryForPrompt(files) { if (files == null) { var scriptId = ScriptApp.getScriptId(); files = typeof getScriptProjectFiles === 'function' ? getScriptProjectFiles(scriptId) : functionInventoryFetchFiles_(scriptId); } if (Object.prototype.toString.call(files) !== '[object Array]') { throw new Error('functionInventoryForPrompt: files must be an array of {name, type, source}'); } var found = []; var i, file, type, funcs, j; for (i = 0; i < files.length; i++) { file = files[i] || {}; type = String(file.type || ''); if (type === 'HTML' || type === 'JSON') continue; if (type && type !== 'SERVER_JS') continue; funcs = listTopLevelFunctions_(file.source || ''); for (j = 0; j < funcs.length; j++) { funcs[j].file = file.name || ''; found.push(funcs[j]); } } var lines = ['Top-level functions in this project (' + found.length + '):']; if (!found.length) lines.push('- (none)'); for (i = 0; i < found.length; i++) { lines.push(formatFunctionInventoryLine_(found[i])); } lines.push(''); lines.push( 'Only unindented function declarations are listed (nested or indented helpers are omitted). ' + 'Tell me before renaming a function, and do not change logic unless I ask.' ); return lines.join('\n'); } function functionInventoryFetchFiles_(scriptId) { var id = String(scriptId == null ? '' : scriptId).trim(); if (!id) throw new Error('functionInventoryForPrompt: scriptId required'); var url = 'https://script.googleapis.com/v1/projects/' + encodeURIComponent(id) + '/content'; var resp; try { resp = UrlFetchApp.fetch(url, { method: 'get', headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() }, muteHttpExceptions: true }); } catch (e) { throw new Error( 'functionInventoryForPrompt: could not read project files. Enable the Apps Script API, turn on the Apps Script API toggle at https://script.google.com/home/usersettings, and add the script.projects.readonly and script.external_request scopes. Or pass files from getScriptProjectFiles(scriptId).' ); } var code = resp.getResponseCode(); var body = resp.getContentText() || ''; if (code < 200 || code >= 300) { var message = ''; try { var parsed = JSON.parse(body); if (parsed && parsed.error) message = parsed.error.message || ''; } catch (e2) {} throw new Error( 'functionInventoryForPrompt: the Apps Script API is not enabled or the script cannot read this project (' + code + (message ? ': ' + message : '') + '). Enable the Apps Script API in the Cloud project, turn on the Apps Script API toggle at https://script.google.com/home/usersettings, and use the script.projects.readonly scope. Or pass files from getScriptProjectFiles(scriptId).' ); } var data = JSON.parse(body); return data.files || []; } function listTopLevelFunctions_(source) { var lines = String(source == null ? '' : source).split(/\r\n|\n|\r/); var out = []; var commentLines = []; var inBlock = false; var blockBuf = []; var i, line, trimmed, m, args, comment; for (i = 0; i < lines.length; i++) { line = lines[i]; trimmed = line.replace(/\s+$/, ''); if (inBlock) { blockBuf.push(trimmed); if (/\*\//.test(line)) { inBlock = false; commentLines = commentLines.concat(blockBuf); blockBuf = []; } continue; } if (/^\s*\/\*/.test(line)) { blockBuf = [trimmed]; if (/\*\//.test(line)) { commentLines = commentLines.concat(blockBuf); blockBuf = []; } else { inBlock = true; } continue; } if (/^\s*\/\//.test(line)) { commentLines.push(trimmed); continue; } if (trimmed === '') { continue; } // Only unindented function declarations count as top-level. m = line.match(/^function\s+([A-Za-z_$][\w$]*)\s*\(([^)]*)\)/); if (m) { args = splitFnArgs_(m[2]); comment = cleanLeadingComment_(commentLines); out.push({ name: m[1], args: args, argCount: args.length, comment: comment }); commentLines = []; continue; } commentLines = []; } return out; } function splitFnArgs_(raw) { var s = String(raw || '').trim(); if (!s) return []; var parts = s.split(','); var out = []; var i, p; for (i = 0; i < parts.length; i++) { p = parts[i].replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/, '').trim(); if (!p) continue; p = p.replace(/=.*$/, '').trim(); p = p.replace(/^[.]{3}/, '').trim(); if (p) out.push(p); } return out; } function cleanLeadingComment_(commentLines) { if (!commentLines || !commentLines.length) return ''; var lines = []; var i, s; for (i = 0; i < commentLines.length; i++) { s = String(commentLines[i]) .replace(/^\s*\/\*+\s?/, '') .replace(/\s*\*\/\s*$/, '') .replace(/^\s*\/\/+\s?/, '') .replace(/^\s*\*\s?/, ''); s = s.trim(); if (!s || s === '*' || s === '/') continue; if (/^@/.test(s)) { if (lines.length) break; continue; } lines.push(s); } var text = lines.join(' ').replace(/\s+/g, ' ').trim(); if (text.length > 300) text = text.slice(0, 297).replace(/\s+\S*$/, '') + '...'; return text; } function formatFunctionInventoryLine_(fn) { var argList = fn.args && fn.args.length ? fn.args.join(', ') : ''; var n = fn.argCount == null ? 0 : fn.argCount; var argLabel = n === 1 ? '1 arg' : n + ' args'; var comment = fn.comment ? fn.comment : '(no leading comment)'; return '- ' + fn.name + '(' + argList + ') — ' + argLabel + '. ' + comment; }

Example

function logFunctionsForBrief() { console.log(functionInventoryForPrompt()); console.log(triggerInventoryForPrompt()); } // Top-level functions in this project (3): // - refreshPageMeta(sheet) — 1 arg. Fetch URLs in column A and write values. // - extractMetaTags(html) — 1 arg. Parse <title> and <meta> tags from HTML. // - noComment() — 0 args. (no leading comment)

Paste the Execution Log into your Co-Pilot brief and ask for an explanation only, before you ask for any logic change.

Notes

  • This is a read. The script.projects.readonly scope cannot change the project. A 403 usually means the API is off, the user-settings toggle is off, or the scope is missing.
  • Simple triggers (onOpen, onEdit, doGet, doPost) still show up here if they are declared as functions; they do not show up in the trigger inventory.
  • Time-driven schedules still are not in either list. Add “runs Monday mornings” to the brief yourself.

Happy Coding!

NitroGAS Chrome Extension

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

Get the Extension