You are building a support ticket system using Google Forms and Google Sheets.
- A user submits Form#1 to Spreadsheet#1 - the Ticket Number is a unique value;
- IT support responds to the form;
- IT support updates their own records by submitting Form#2 to Spreadsheet#2;
- when IT support updates the status (on Spreadsheet#2) from something other than "Open", an email should be sent to the user;
- since Spreadsheet#2 does not have all the information in the Form Response, it is necessary to match the unique Ticket Number (from Spreadsheet#2) to the Ticket Number on Spreadsheet#1 in order to retrive the yser name, email and other relevant data.
You are having problems matching a Ticket Number between the two spreadsheets.
The following script demonstrates the method to match a value with an array. The key aspects are:
- This script must run from Spreadsheet#2
onEdit(e): the script must be on onEdit trigger so that it detects the Support updating the status (the logic for that is not included in this script)
openById(): note how Spreadsheet#1 is accessed
var formValues = formRange.getValues();: Get ALL the values from the response sheet
var formTickets = formValues.map(function(e){return e[1];});: use the javascript map method to create a temporary array of the Ticket Numbers only
var TickedID = support.getRange(editedrow,2).getValue();: get the Ticket Number from Spreadsheet#1 taking advantage of Event Objects
var result = formTickets.indexOf(TicketID);// result is zero-based.: find the match of the support Ticket Number in the array of Form Ticket Numbers; this uses the javascript indexOf method. A value of -1 means no match, otherwise the value is the zer0-based index of the ticket Number in the array.
var email = formValues[result][4];: once you know the "result" value, you can access the rest of the details for that ticket from the Form Values.
function onEdit(e){
// Spreadsheet#2 containing the IT support logs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var supportsheetname = "support";
var support = ss.getSheetByName(supportsheetname);
// Spreadsheet #1 containing the form responses
var s2id = "<insert spreadsheetID here>";
var s2 = SpreadsheetApp.openById(s2id);
var formsheetname = "Form Responses 1";
var form = s2.getSheetByName(formsheetname);
// get data from forms
var Avals = form.getRange("A1:A").getValues();
var formLR = Avals.filter(String).length;
var formRange = form.getRange(2,1,formLR-1,6);
var formValues = formRange.getValues();
//get the ticket Numbers in a temporary array
var formTickets = formValues.map(function(e){return e[1];});//[[e],[e],[e]]=>[e,e,e]
// Logger.log(formTickets);//DEBUG
// get the row that was edited by the IT support
var editedrow = e.range.getRow();
var TicketID = support.getRange(editedrow,2).getValue();
Logger.log("the Ticket ID is "+TicketID);
// find the ticket number in the responses
var result = formTickets.indexOf(TicketID);// result is zero-based.
if (result != -1){
// Logger.log("DEBUG: the result is "+result); // result is zero-based
// declare variables
var Status = e.value; // new status value
var Timestamp = formValues[result][0];
var Name = formValues[result][2];
var Phone = formValues[result][3];
var Email = formValues[result][4];
var Problem = formValues[result][5];
Logger.log("Ticket ID:"+TicketID+", Timestamp:"+Timestamp+", Status:"+Status);
Logger.log("Name: "+Name+", Phone:"+Phone+", email:"+Email+", problem:"+Problem);
// send an email using this information
}
}
UPDATE
A comparison using the more conventional loop of form values to find a match with the support Ticket.
function onEdit(e){
// example of a loop compared to map/indexOf
//so5894534203()
// Spreadsheet#2 containing the IT support logs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var supportsheetname = "support";
var support = ss.getSheetByName(supportsheetname);
// Spreadsheet #1 containing the form responses
var s2id = "1S9CozVBncgDGceqhnIC6bwK15kDAwRrPu9ObooPgKPc";
var s2 = SpreadsheetApp.openById(s2id);
var formsheetname = "Form Responses 1";
var form = s2.getSheetByName(formsheetname);
// get data from forms
var Avals = form.getRange("A1:A").getValues();
var formLR = Avals.filter(String).length;
var formRange = form.getRange(2,1,formLR-1,6);
var formValues = formRange.getValues();
// get the row that was edited by the IT support
var editedrow = e.range.getRow();
var TicketID = support.getRange(editedrow,2).getValue();
// Logger.log("DEBUG: the edited ticket is "+TicketID);
for (var i=0;i<formValues.length;i++){
// Logger.log("DEBUG: i:"+i+", ticket#:"+formValues[i][1])
if(TicketID === formValues[i][1]){
// Logger.log("DEBUG: i:"+i+" = match"); // zero based plus allow for header row
// declare variables
var Status = e.value; // new status value
var Timestamp = formValues[i][0];
var Name = formValues[i][2];
var Phone = formValues[i][3];
var Email = formValues[i][4];
var Problem = formValues[i][5];
Logger.log("Ticket ID:"+TicketID+", Timestamp:"+Timestamp+", Status:"+Status);
Logger.log("Name: "+Name+", Phone:"+Phone+", email:"+Email+", problem:"+Problem);
// send an email using this information
}
}
}