I want to start off by saying I have limited coding experience in google scripts, but have had extensive experience coding in R. I have tried a using previous stackedoverflow solution (Using QR Code to enter value into Google Form?), but any time I would edit the URL, it'll say "Sorry, unable to open the file at this time."
I copied the code that was listed under code.gs into the main script and changed 'your-form-id-here' to match my google sheet's ID and added the two HTML codes for 'Success' and 'Failure', but it hasn't worked.
I have a google form that asks people to input their unique ID Number. Rather than having folks mis-type their number, I would like to set it up so that they can scan a QR code on their phone and have the Google Form auto-populate the first question that asks for their ID#. There are additional questions on the google form they need to answer, but I would just like for the first question to be auto-populated.
Here is the Google Form: https://docs.google.com/forms/d/e/1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew/viewform?usp=sf_link
The Form ID I used was 1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew, which I assumed was the ID based on the Google Form URL.
EDIT: Here is my current script for the Code.gs:
function doGet(e){
try {
var passNo = e.parameters.passNo;
var qNumber = e.parameters.qNumber;
var form = FormApp.openById('1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew');
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');
}
}
I also added the Success.html and Failure.html code exactly as written in the previous example:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Success
</body>
</html>
and
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
Failure
</body>
</html>
After deploying the script, I got the URL and practiced adding the "passNo" and "qNumber" exactly as they did in the example by adding ?passNo=21480&qNumber=1
to the end of my URL, but the URL leads to a page error:
CodePudding user response:
Issue and solution:
When I saw your current script, I noticed that at ar form = FormApp.openById('1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew');
and https://docs.google.com/forms/d/e/1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew/viewform?usp=sf_link
, you are using the ID from the URL of preview. In this case, that ID is not the Google Form ID. I thought that this might be the reason for your issue.
For example, when I tested your script by using the correct Google Form ID, no error occurs. "Success" is returned. So please modify as follows.
From:
var form = FormApp.openById('1FAIpQLSdsP0e2FsC54QY0Nyrpipi21yC-8ODjO9aRlfOMq51vODRaew');
To:
var form = FormApp.openById('### Google Form ID ###');
- In this case, please use
###
ofhttps://docs.google.com/forms/d/###/edit
.
Note:
- When you modified the Google Apps Script, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful this.
- You can see the detail of this in the report of "Redeploying Web Apps without Changing URL of Web Apps for new IDE".