求助:Google Sheets App Script无法生成表单响应编辑链接
Google Sheets脚本无法生成表单响应编辑链接的问题与解决
我把Google表单和Google表格关联后,在时间戳列前面插了第1列,表头设成「Edit Url」,想让这列自动填充对应表单响应的编辑链接。用下面的脚本运行后无报错但完全没效果,已经确认表单开启了「提交后立即编辑响应」的权限,调整过表单URL和工作表名称也没用,最终找到两个关键解决办法:
// Form URL var formURL = 'https://docs.google.com/forms/d/form-id/viewform'; // Sheet name used as destination of the form responses var sheetName = 'Form Responses 1'; /* * Name of the column to be used to hold the response edit URLs * It should match exactly the header of the related column, * otherwise it will do nothing. */ var columnName = 'Edit Url' ; // Responses starting row var startRow = 2; function getEditResponseUrls(){ var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues(); var columnIndex = headers[0].indexOf(columnName); var data = sheet.getDataRange().getValues(); var form = FormApp.openByUrl(formURL); for(var i = startRow-1; i < data.length; i++) { if(data[i][0] != '' && data[i][columnIndex] == '') { var timestamp = data[i][0]; var formSubmitted = form.getResponses(timestamp); if(formSubmitted.length < 1) continue; var editResponseUrl = formSubmitted[0].getEditResponseUrl(); sheet.getRange(i+1, columnIndex+1).setValue(editResponseUrl); } } }
解决办法
将「Edit Url」列移至表格最后一列
原脚本默认时间戳在表格第1列(data[i][0]),如果把Edit Url列放在第1列,脚本会错误地把这列的内容当作时间戳去匹配表单响应,自然找不到对应记录。移到最后一列后,时间戳回到第1列,脚本就能正确获取时间戳匹配响应。把表单URL换成带
/edit后缀的地址
原脚本里用的是表单的填写入口(/viewform结尾),必须替换成表单的编辑后台地址(/edit结尾),这样FormApp.openByUrl()才能正确关联到表单对象,进而获取到响应的编辑链接。
内容的提问来源于stack exchange,提问作者Keith Bryant
相关产品推荐
相关产品推荐

