Data SheetRequirement :-
I am trying to write an Google App script to send email based on their answers while form submission for travel expenses. i.e if form users choose x as their city y as their per diem rate(allowance per day) script should check the google sheet attached for predefined values and if the user had entered the value more than allocated for a city then it should email admins to review the request else email users that is auto approved.
I know this can be done via if loop but pretty new to coding struggling to pull this condition any help would be great
function onformsubmit(e){
var townCity = e.values[11];
var currency = e.values[12];
.... // other lines of function
}
function sendEmails(townCity,currency){//var imported for other function for validation
var ss = SpreadsheetApp.openById("ssid");
var sheet = ss.getSheetByName("Rates for validation check");
var lastRow = sheet.getLastRow();
var city= sheet.getRange(2, 2, 141); // Fetch the range, including the minimums column 2 for city list in data sheet
var budget= sheet.getRange(2, 3, 141); // Fetch the range, including the minimums column 3 for allocated budget for each city in dat sheet
var location = city.getValues(); // Fetch values for each row in the Range.
var location2 = budget.getValues();
Logger.log(townCity); // log test
if (townCity == location && currency == location2 ) {
Logger.log('sendemail');}
else if(townCity == location && currency != location2) {
Logger.log('dontsendemail');
}
}
Full Code
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var emailAddress = e.values[1];
var clientName = e.values[4];
var StartDate = e.values[6];
var EndDate = e.values[7];
var country = e.values[9];
var townCity = e.values[8];//this value here is not been picked correctly as the contents in the column were retried by formula from all the other city columns
var amount = e.values[12];
var currency = e.values[11];
var engagementLeaderName = e.values[2];
//Logger.log('Lets see if its here'+ townCity);
}
//funtion to validate with data sheet
function sendEmails(amount,townCity){
var ss = SpreadsheetApp.openById("ssid");
var sheet = ss.getSheetByName("Rates for validation check");
var firstRow = 2;
var firstCol = 2;
var numRows = sheet.getLastRow() - firstRow + 1;
var numCols = 2;
var allocated = sheet.getRange(firstRow, firstCol, numRows, numCols).getValues();
var index = allocated.map(function(value) {
return value[0];
}).indexOf(townCity);
if (amount > allocated[index][1]) {
// Send email:
Logger.log('send email')
}
}