使用JS(Google AppScripts)将生成的表单链接推送至Google SpreadSheet
解决方案
首先明确你现有代码的2个核心问题:
- 表单链接赋值错误:
Logger.log()方法无返回值,导致你存储的url_form全部为undefined,且lista_url仅存储链接未绑定对应群组编号,无对应关系无法生成两列表格 - 导出代码环境不兼容:你写的
excelformat是浏览器端JavaScript代码,而Google表单/表格的自动化脚本是服务端运行的Google Apps Script(GAS),不存在document等浏览器全局对象,因此无法执行,还会触发join报错(因为你遍历的infoArray是字符串/undefined类型,不是数组,没有join方法)
可行实现方案(直接写入Google Spreadsheet,符合需求)
以下代码直接在原GAS脚本基础上修改,运行后会自动创建新的Google表格,按你要求的格式存储群组和对应表单链接:
var group = [137, 138, 139] // 存储[群组编号, 表单链接]的二维数组 var groupUrlList = [] function createForm(groupId) { // create & name Form var item = "Speaker Information Form"; var form = FormApp.create(item) .setTitle(item); // single line text field form.addTextItem() .setTitle(groupId.toString()) .setRequired(true); // multi-line "text area" item = "Short biography (4-6 sentences)"; form.addParagraphTextItem() .setTitle(item) .setRequired(true); // radiobuttons item = "Handout format"; var choices = ["1-Pager", "Stapled", "Soft copy (PDF)", "none"]; form.addMultipleChoiceItem() .setTitle(item) .setChoiceValues(choices) .setRequired(true); // (multiple choice) checkboxes item = "Microphone preference (if any)"; choices = ["wireless/lapel", "handheld", "podium/stand"]; form.addCheckboxItem() .setTitle(item) .setChoiceValues(choices); // 正确获取表单链接 var formUrl = form.getPublishedUrl() Logger.log('Group: %s, Published URL: %s', groupId, formUrl) // 把群组和对应链接以数组形式存入列表,方便后续写入表格 groupUrlList.push([groupId, formUrl]) } function generateFormLinksAndSaveToSheet(){ // 生成所有表单 group.forEach(function(item){ createForm(item) }) // 创建新表格,你也可以用SpreadsheetApp.openById('你的表格ID')指定已有表格 var targetSheet = SpreadsheetApp.create('群组表单链接汇总').getActiveSheet() // 写入表头 var header = ['Group', 'Link'] targetSheet.appendRow(header) // 批量写入所有群组和链接数据 targetSheet.getRange(2, 1, groupUrlList.length, 2).setValues(groupUrlList) // 可选:弹窗提示生成完成,可直接跳转查看表格 SpreadsheetApp.getUi().showModalDialog( HtmlService.createHtmlOutput('<a target="_blank" href="' + targetSheet.getParent().getUrl() + '">点击打开汇总表格</a>'), '生成完成' ) } // 运行这个主函数即可完成所有操作 // generateFormLinksAndSaveToSheet()
代码使用说明
- 把原脚本全部替换为上述代码
- 点击运行
generateFormLinksAndSaveToSheet函数,首次运行需要授权脚本的表单、表格操作权限 - 运行完成后会自动生成汇总表格,你可以在Google Drive里找到,格式完全符合你要求的两列布局
内容的提问来源于stack exchange,提问作者alb
相关产品推荐
相关产品推荐

