我在Google Form上有一个表单,用于记录访客进入我们停车场的情况。入场所需的数据之一是通行证号码,它是以二维码的形式递给参观者的。有没有办法不再手动输入通行证号码,只需扫描二维码,通行证号码就会输入到表格中?
我知道可以使用以下脚本在工作表中执行此操作:
function doGet(e){
var ss = SpreadsheetApp.openByUrl("sheet_url");
var sheet = ss.getSheetByName("sheet_name");
return insert(e,sheet);
}
function doPost(e){
var ss = SpreadsheetApp.openByUrl("sheet_url");
var sheet = ss.getSheetByName("sheet_name");
return insert(e,sheet);
}
function insert(e,sheet){
var scannedData = e.parameter.sdata;
var d = new Date();
var ctime = d.toLocaleString();
sheet.appendRow([scannedData,ctime]);
因此,我希望在座的任何人都知道表单的脚本应该是什么样子。
当您访问部署为Web应用程序的应用程序脚本的URL时,您可以在发布Web应用程序时提供的URL后使用?
参数将参数传递到脚本中的doGet(e)
函数:
https://script.google.com/.../exec?passNo=21480&qNumber=1
e
函数接受的e
变量是一个事件对象,它包含有关事件的信息。You can acess the parameters from the URL using e.parameters
:
function doGet(e){
Logger.log(e.parameters);
}
将在日志中显示 {qNumber=[1], passNo=[21480]}
。可以提取这些参数,然后使用FormApp
Apps脚本库,您可以创建对表单的新响应,并使用FormApp
的createResponse().withItemResponse(ItemResponse)
方法以编程方式提交它,使用从url参数获取的通行号和问题号:
function doGet(e){
try {
var passNo = e.parameters.passNo;
var qNumber = e.parameters.qNumber;
var form = FormApp.openById('your-form-id-here');
var items = form.getItems();
// assuming question 1 is the pass number question
var q = items[qNumber[0] - 1].asTextItem();
var itemResponse = q.createResponse(passNo[0])
var FormResponse = form.createResponse();
FormResponse.withItemResponse(itemResponse);
FormResponse.submit();
return HtmlService.createHtmlOutputFromFile('Success')
}
catch(e){
return HtmlService.createHtmlOutputFromFile('Failure');
}
}
我还在项目中创建了两个HTML
文件,分别名为‘Success.html’和‘Failure.html’,Web应用会根据响应创建和提交是否成功返回各自的页面:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Success
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Failure
</body>
</html>
现在您需要做的就是用脚本的URL和相关参数作为定位器生成一个二维码。确保将二维码中的pNumber
参数始终设置为您要提交的问题的编号,并且URL中passNo=
之后的数字是您要扫描和输入的通行号。