I work for a charter school and when I get new students I have to fill out an equipment request form for the student through Google Forms. I would like to be able to automatically submit a response without having to fill the form out every time. I already have all the information about the student that I would need in a Google Sheet.
I do not have edit access to the Form and can only use Google app script for coding because I do not have server access or admin access on my work laptop. So this is really my only option for automation.
So far I have been able to make a fetch call with the Form url and an object using the entryIDs for the questions.
const obj = {
"entry.1111": tName,
"entry.2222": sFirst,
"entry.3333": sLast,
"entry.4444": 'Option 1', //multiple answer question
"entry.5555": 'yes',
"entry.6666": 'yes',
"entry.7777": 'minor',
"entry.8888": 'initial'
}
const options = {
'method': 'post',
'payload': obj
}
UrlFetchApp.fetch(url,options)
This works as long as I do not need to include more than one answer per question, but for some students I need to do just that. If I try to send a string like "Option 1, Option 2" the script fails.
Edit: My code does not have the comment in the object.
CodePudding user response:
When the multiple answers for one question are sent, unfortunately, I thought that JSON object might not be able to be directly used for the payload. Because the same key is used in this case. And, even when the array is used for the value, an error occurs. This has already been mentioned in your comment.
So in this answer, I would like to propose to add it to the query parameter. In this case, I think that there are 2 patterns for your situation.
Pattern 1:
In this pattern, all values are used as the query parameters.
// This script is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function(obj) {
return this Object.keys(obj).reduce(function(p, e, i) {
return p (i == 0 ? "?" : "&")
(Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
return str e "=" encodeURIComponent(f) (j != obj[e].length - 1 ? "&" : "")
},"") : e "=" encodeURIComponent(obj[e]));
},"");
}
function myFunction() {
const url = "### your URL ###"; // Please set your URL.
const query = {
"entry.1111": tName,
"entry.2222": sFirst,
"entry.3333": sLast,
"entry.4444": ['Option 1', 'Option 2'],
"entry.5555": 'yes',
"entry.6666": 'yes',
"entry.7777": 'minor',
"entry.8888": 'initial'
};
const endpoint = url.addQuery(query);
const options = {'method': 'post'};
const res = UrlFetchApp.fetch(endpoint, options);
console.log(res.getContentText());
}
Pattern 2:
In this pattern, only the question of the multiple answers is sent as the query parameters.
// This script is from https://gist.github.com/tanaikech/70503e0ea6998083fcb05c6d2a857107
String.prototype.addQuery = function(obj) {
return this Object.keys(obj).reduce(function(p, e, i) {
return p (i == 0 ? "?" : "&")
(Array.isArray(obj[e]) ? obj[e].reduce(function(str, f, j) {
return str e "=" encodeURIComponent(f) (j != obj[e].length - 1 ? "&" : "")
},"") : e "=" encodeURIComponent(obj[e]));
},"");
}
function myFunction() {
const url = "### your URL ###"; // Please set your URL.
const query = {
"entry.4444": ['Option 1', 'Option 2'],
};
const obj = {
"entry.1111": tName,
"entry.2222": sFirst,
"entry.3333": sLast,
"entry.5555": 'yes',
"entry.6666": 'yes',
"entry.7777": 'minor',
"entry.8888": 'initial'
};
const endpoint = url.addQuery(query);
const options = {
'method': 'post',
'payload': obj
};
const res = UrlFetchApp.fetch(endpoint, options);
console.log(res.getContentText())
}