A Google Form with fields you already know from a sheet row — name, email, client id — and a recipient who shouldn't have to retype them. buildPrefilledFormUrl(formUrl, entryMap, rowObject) builds a /viewform URL with entry.NNN=value query params, URL-encoded, and skips blank values.
When to use
- Emailing each person a Form link with their row already filled in
- Kickoff, feedback, or intake forms where the sheet already has half the answers
- Writing the prefilled URL back onto the row for a mail-merge later
How to use this snippet
/**
* Build a Google Form viewform URL with entry.NNN=value params.
* Blank/null values are skipped. `entryMap` keys match rowObject fields;
* values are the numeric entry ids from the Form's prefill link.
*
* @param {string} formUrl any Form URL (edit/viewform); normalized to viewform
* @param {Object<string, string|number>} entryMap { fieldName: entryId }
* @param {Object} rowObject
* @return {string}
*/
function buildPrefilledFormUrl(formUrl, entryMap, rowObject) {
rowObject = rowObject || {};
entryMap = entryMap || {};
var base = String(formUrl || '').trim();
if (!base) throw new Error('buildPrefilledFormUrl: formUrl is required');
// Normalize to the viewform endpoint; strip existing query/hash.
base = base.replace(/\/(edit|viewform|formResponse).*$/, '/viewform');
if (base.indexOf('/viewform') === -1) {
base = base.replace(/\/?$/, '/') + 'viewform';
}
base = base.split('?')[0].split('#')[0];
var parts = [];
Object.keys(entryMap).forEach(function (field) {
var entryId = entryMap[field];
if (entryId == null || entryId === '') return;
var val = Object.prototype.hasOwnProperty.call(rowObject, field) ? rowObject[field] : '';
if (val == null) return;
var s = String(val);
if (!s) return; // skip blanks
parts.push('entry.' + entryId + '=' + encodeURIComponent(s));
});
return parts.length ? base + '?' + parts.join('&') : base;
}
Example
// From the Form: …/viewform?usp=pp_url&entry.111=Name&entry.222=Email
var ENTRY = { Name: '111', Email: '222', ClientId: '333' };
var FORM = 'https://docs.google.com/forms/d/abc123/edit';
function linkForRow(row) {
return buildPrefilledFormUrl(FORM, ENTRY, row);
}
// → https://docs.google.com/forms/d/abc123/viewform?entry.111=Ada%20Lovelace&entry.222=ada%40x.com
Tips:
- Get entry ids from the Form editor: Get pre-filled link, fill sample values, copy the URL, and read the
entry.NNNnumbers. - Blanks are omitted on purpose so the respondent still sees an empty field instead of a prefilled empty string quirk.
- The helper always targets
/viewform. Passing an/editURL is fine. - Prefilled links do not submit the form; the person still hits Submit.
Tip: NitroGAS Co-Pilot can turn your prefill URL into the entryMap object. Paste one sample link.
Happy Coding!
