getResponseEditUrls(form, {sinceDate}) returns rows of [timestamp, respondentEmail, editUrl] for a Google Form, so you can write the edit link next to the matching response row. The form must allow respondents to edit after submit. Anyone with the link can edit that response.
When to use
- Writing an edit URL column onto the form-response sheet
- Sending a confirmation email that includes a link to fix answers
- Recovering edit links for responses after
sinceDatewithout re-running the whole form
How to use this snippet
Turn on Allow response editing in the form (Settings → Responses). Collect email addresses if you want respondentEmail filled in and if you plan to email the link. form can be a Form object, a form id, or a form URL. sinceDate is optional (Date or yyyy-MM-dd); when set, only responses at or after that time are returned (the same rule as Form.getResponses(timestamp)). The first row is a header, ready for setValues.
/**
* Return [timestamp, respondentEmail, editUrl] rows for a form, including a
* header row. form is a Form, form id, or form URL. opts.sinceDate is optional
* (Date or yyyy-MM-dd) and limits the list to responses at or after that time.
* Throws if the form does not allow editing after submit. An empty email is
* normal when the form does not collect addresses. Anyone with an edit URL
* can change that response.
*
* @param {Form|string} form
* @param {{sinceDate?: Date|string}} [opts]
* @return {Array<Array<*>>}
*/
function getResponseEditUrls(form, opts) {
opts = opts || {};
var f = resolveForm_(form);
if (typeof f.canEditResponse !== 'function' || !f.canEditResponse()) {
throw new Error(
'getResponseEditUrls: the form must allow respondents to edit after submit (Settings → Responses → Allow response editing)'
);
}
var responses;
if (opts.sinceDate == null || opts.sinceDate === '') {
responses = f.getResponses();
} else {
var since = toDate_(opts.sinceDate);
responses = f.getResponses(since);
}
var rows = [['timestamp', 'respondentEmail', 'editUrl']];
var i, r, email, url;
for (i = 0; i < responses.length; i++) {
r = responses[i];
email = '';
try {
email = r.getRespondentEmail() || '';
} catch (e) {}
url = r.getEditResponseUrl();
rows.push([r.getTimestamp(), email, url]);
}
return rows;
}
function resolveForm_(form) {
if (form && typeof form.getResponses === 'function') return form;
var s = String(form == null ? '' : form).trim();
if (!s) {
throw new Error('getResponseEditUrls: form required (Form, form id, or form URL)');
}
if (/^https?:/i.test(s)) return FormApp.openByUrl(s);
return FormApp.openById(s);
}
function toDate_(value) {
if (Object.prototype.toString.call(value) === '[object Date]') {
if (isNaN(value.getTime())) throw new Error('getResponseEditUrls: sinceDate is not a valid date');
return value;
}
var s = String(value).trim();
var m = s.match(/^(\d{4})-(\d{2})-(\d{2})$/);
var d;
if (m) {
d = new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
} else {
d = new Date(s);
}
if (isNaN(d.getTime())) throw new Error('getResponseEditUrls: sinceDate is not a valid date');
return d;
}
Example
function writeEditUrlColumn() {
var form = FormApp.openById('FORM_ID');
var rows = getResponseEditUrls(form);
var sheet = SpreadsheetApp.getActive().getSheetByName('Edit links');
sheet.clearContents();
sheet.getRange(1, 1, rows.length, 3).setValues(rows);
}
Match timestamp and respondentEmail back to the destination response sheet if you want the link on the original row. For new responses only, pass {sinceDate: lastRun} or call e.response.getEditResponseUrl() from an on-form-submit trigger.
Notes
- Treat an edit URL like a password. Anyone who has it can change that response, even if they are not the respondent. Prefer a confirmation email to the respondent over a widely shared sheet column.
getEditResponseUrl()throws if the form does not allow editing; this helper turns that into a clear error before it loops.- Forms can also include an edit link on its own confirmation page when editing is allowed. This helper is for putting the link in a sheet or in your own email, and for recovering links from past responses.
Happy Coding!
