javascript - 将生成的链接推送到 JS 中的 Google 电子表格(Google AppScripts)
问题描述
我正在使用脚本来自动生成表单。它基本上循环遍历元素列表,因为它们中的每一个都应该有不同的表单链接。如何将与每个组对应的生成的链接存储在谷歌电子表格中?
我想以这种方式制作电子表格:
Group Link
138 https://docs.google.com/forms/idForm138
139 https://docs.google.com/forms/idForm139
这是我的代码:
var lista_url=[]
var group=[137, 138, 139]
function createForm(group) {
// create & name Form
var item = "Speaker Information Form";
var form = FormApp.create(item)
.setTitle(item);
// single line text field
item = group;
form.addTextItem()
.setTitle(item)
.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 url_form= Logger.log('Published URL: ' + form.getPublishedUrl());
Logger.log('Group: '+group)
lista_url.push(url_form)
}
function generate_Form_links(group){
group.forEach(function(item, index){
console.log(item, index)
createForm(item)
}
}
generate_Form_links(group)
编辑:
实现这个会引发这个错误:TypeError: infoArray.join is not a function
function excelformat(lista_url) {
var result_table = lista_url
var lineArray = [];
result_table.forEach(function(infoArray, index) {
var line = infoArray.join(" \t");
lineArray.push(index == 0 ? line : line);
});
var csvContent = lineArray.join("\r\n");
var excel_file = document.createElement('a');
excel_file.setAttribute('href', 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(csvContent));
excel_file.setAttribute('download', 'Visitor_History.xls');
document.body.appendChild(excel_file);
excel_file.click();
document.body.removeChild(excel_file);
}
excelformat(lista_url)
解决方案
不确定我是否正确理解了您的任务。因此,如果您有两个数组:group
并且links
您可以通过这种方式将它们的内容添加到电子表格中:
var group = [138, 139];
var links = ['https://docs.google.com/forms/idForm138','https://docs.google.com/forms/idForm139'];
function save_arrays_to_spreadsheet(group, links) {
var ss = SpreadsheetApp.openById(ss_id); // <-- put the spreadsheet ID here
var sheet = ss.getSheets()[0]; // let it be a first sheet
for (var i in group) sheet.appendRow([group[i], links[i]]); // add a row
}
而且我不明白你代码中的这个函数在做什么:
function generate_Form_links(group){
for (var group=0; group<=group.length ; group=i++){
createForm(group) //call our function for each value in list
}
}
它获取group
(一个数组?)并将其转换为零var group = 0
,然后出现变量i
等。这对我来说似乎是一个错误。
可能,我不知道,应该有:
function generate_Form_links(group) {
for (var i=0; i<=group.length; i++) { createForm(group[i]) }
}
推荐阅读
- postgresql - Postgres查询包含骆驼案例的序列
- ssis - 无法从命令行构建 SSIS 包:缺少开关参数。/build 开关所需的配置名称
- cryptoapi - 在 coinbase API 上开发第三方交易所
- .htaccess - 使用 Codeigniter 制作的多语言网站。如何将权限设置为一种语言只有一个 IP?
- yocto - Yocto 使用 Multilib 更新到 Sumo 导致 do_image_wic 出错
- javascript - Firefox Quantum Private 浏览器不加载由 javascript 添加的脚本
- python - 在 Mac 的终端中更改用户名命令
- c# - 生成 .xsd 文件的 C# 类
- swift - 更改 UINavigationBar 的分隔符颜色
- xamarin - 如何在 Xamarin Android 中为 Checkbox 添加自定义属性?