We are using Google Forms to collect data from our field reps while they are out at a job site. We use a script to fill drop down values for 4 different fields and that runs periodically. This saves them from having to type it out and possible make mistakes.
All of those fields are pre-determined, so if they select Project #1, we already know the values of address, manager, and client. The values are stored in a second tab of the results form and could theoretically be used as a lookup.
I question whether these fields need to be included at all, but when the user submits the form, the data is then emailed using an add-on and it uses some of those fields in the subject. I suppose the recipients want the info as well.
Can I make my own triggers? If I had a trigger for when the project ID changed, I would think I could update the others from a spreadsheet.
Or if there was a OnNextPage trigger that I could use to fill those values.
Is there anyway to add/change values when the form is submitted?
I think the following is what I want to do (thanks @OMilo), but I am struggling with the lookups:
function onFormSubmit(e) { Logger.log("[METHOD] onFormSubmit");
sendEmail(e.range); }
function sendEmail(range) {
// Here we get choice the users selected in the form: var itemResponse = e.response.getItemResponses()[1557784536]; // Use whatever index is the item, in my case it's 0. var kmbProjectNumber = itemResponse.getResponse();
//LOOKUP KMB PROJECT NUMBER IN SHEET: "New - KMB Design Visit Closeout (Responses)" TAB: "KMB Lookups"
var ss = SpreadsheetApp.OpenByID('REDACTED');
var lookups = ss.getSheetByName("KMB Lookups");
//In Column named'KMB Project ID' find record from bottom where value = kmbProjectNumber
//Get value for column named 'Site Address'
//Get value for column named 'KMB Project Manager'
//Get value for column named 'Client Assigning this Work'
// FETCH SPREADSHEET //
var values = range.getValues();
var row = values[0];
//WRITE FOUND VALUES TO SPREADSHEET
//Set row[3] to Site Address
//Set row[4] to Project Manger
//Set row[5] to Client
var svProjectManager = row[4]; //Update manually //E
var svClient = row[5]; //Update manually //F
// EXTRACT VALUES //
var svTimeStamp = row[0]; //A
var svInitials = row[1]; //B
var svProjectID = row[2]; //C
var svSiteAddress = row[3]; //Update manually //D
var svProjectManager = row[4]; //Update manually //E
var svClient = row[5]; //Update manually //F
var svNOCNeeded = row[6]; //G
// PREPARE EMAIL //
var emailRecipients = "REDACTED";
var emailSubject = "Field Visit Completed for "+svProjectManager+" "+svProjectID;
var emailBody = "" //Format using variables set
// SEND EMAIL //
}