I have multiple Google Forms set up to take in data (just numbers). That data is then grabbed using an onFormSubmit function with a manually programmed trigger. Is there a way I can use just one trigger for multiple forms and have them go to the appropriate spreadsheet?
For example, I have a form called dry storage and another called freezer (and have 6 other forms and sheets), with corresponding sheets in a spreadsheet. Is there a way that when the form is submitted, that it takes the data, and places it in the correct spreadsheet? I have it working when I manually type in the sheet name, but I would like it to be done dynamically in case more sheets and forms need to be added so a new function and trigger doesn't have to be created every time.
The forms created have already been linked to the spreadsheet. I basically want every linked form submitted to get the formID of the form it was submitted, and be able to match it to the corresponding sheet name.
In a perfect world, the spreadsheet is determined by the form it is submitted on by name. Is it possible to do this dynamically?
I would like to not have 8 triggers with 8 functions that all do the exact same thing, with the only difference is the formID and corresponding sheet being passed.
The onSubmit function does exactly what I want it to do for one form and one spreadsheet, but I would like it to be able to take in any form and map it to the correct sheet.
The onFormSubmit function was me attempting to use events to generalize it, but with no avail, as I am unfamilar with how events work. I've searched high and low for an example that matches what I am trying to do on the Google website, and StackOverflow. Any examples or points on how events work would be beneficial as well.
Thank you!
function onFormSubmit(e)
{
var res = e.values // get the data from the form
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Dry Storage"); // manually get the sheet it should be going to, I'd like this to be dynamic
for (var a = 0; a < res.length; a++)
{
sheet.getRange(7,(a+1)).setValue(e.values[a]) // set the form values in the sheet
}
function trigger()
{
var form = FormApp.openById(DryStorageFormID) // formID for dry storage. is there a way that this ID can be grabbed dynaically from the submitted form?
ScriptApp.newTrigger('onSubmit')
.forForm(form)
.onFormSubmit()
.create();
}
function onSubmit()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); // get active spreadsheet
var targetSheet = ss.getSheetByName("Dry Storage"); // data should be sent to this sheet. I would like this info to be somehow passed to the function instead of have it hard-coded.
var form = FormApp.openById(DryStorageFormID); // DryFormID
try{
var formResponses = form.getResponses();
}
catch(e)
{
console.error("No Data!");
}
var itemResponses = formResponses[formResponses.length - 1].getItemResponses();
for (var j = 0; j < itemResponses.length; j++)
{
var itemResponse = itemResponses[j];
var offset = j + 2
targetSheet.getRange(offset, (FR.order +1)).setValue(itemResponse.getResponse())
}
}
e.range.getSheet()
/ – CooperonFormSubmit
is not the way if you want a single trigger. What exact actions should be done for each form submission? If you have already linked the forms with the spreadsheet, each form will already have its own sheet, where form submission data will be written. What do you want to do with this data? – Iamblichussheet name
through that. If theForm
or theFormResponse
(returned by the event object) contain information on thesheet name
though, you could retrieve that. Also, I don't know if you're doing this already, but you could programmatically create the different triggers, without having to write 8trigger
functions (you'd just have to define and array with the differentformId
and loop through it. – Iamblichus