0
votes

Who can help me. I would like to say sorry if I can't explain everything correctly as English is not my first language. What I am doing is to create a Google Forms. This are the example.

1st Part:

Google Form 1 | Spreadsheet 1: In the google form, once informations entered. Spreadsheet updated each row. IT person will fix the problem according to the report. Once IT person is done, he will use Google Form 2.

enter image description here

2nd Part:

Google Form 2 | Spreadsheet 2: IT person use 2nd Google Form to close tickets or pending tickets. If IT person submit the form, it is sending an email to the Email Address found in the Spreadsheet 1, and also say the Ticket ID(column found on Spreadsheet 1 and 2), say Ticket Status(column found on Spreadsheet 2), say Report Problem Details(column found on Spreadsheet 1) on the email message. How to do?

This is the code I make.

function myFunction(e) {

// declare variables
   var Timestamp = e.values[0];
   var TicketID = e.values[1];
   var Email = e.values[2];
   var Status = e.values[3];

var ss = SpreadsheetApp.openById(INSERT SPREADSHEET ID 1 INSIDE);
var DetailsCol = 6; // column number of Report Problem Details from Spreadsheet 1

/* This is the part I not know how to get. I want to compare Ticket ID 
from Spreadsheet 1 to Spreadsheet 2, and if they’re same, then it returns the value of 
“Report Problem Details” that is associate to the Ticket ID match, 
and save it to Spreadsheet 2 column F, then send email with this value.*/
var ss1value = ss.getDataRange().getValues(); 
  for(var i = 0; i<data.length;i++){
    if(data[i][1] == TicketID){  //start 0 index
        return i+1;
    }
  }
ss1value.getRange(i, 5).setValue(Report Problem Details);

// Email Send
   var Subject = "Ticket Status";
   var Message = 
 '<!DOCTYPE html> ' +
 "<html><head>" +
    "<table border='1' width='300'>" +  
    "<tr><td>Case ID</td><td>" + TicketID +
    "<tr><td>Status</td><td>" + Status +
    "<tr><td>Problem Description</td><td>" + 'Details' +
    "</table>" +
MailApp.sendEmail(Email, Subject, "", {htmlBody: Message});

}
2
I am sorry, please not be mad. My code not working that's why I didn't posted. I'll update it above. Thank you so much. - xielieliu
Welcome. Is there a specific reason why you use 2 forms and 2 spreadsheets? What if there was just one form and one spreadsheet (with two sheets). The form is sent by the person who has the problem and updates the Form Response sheet. A script automatically copies the response to a second sheet ("support") with statof of "Open"; "support" has extra column(s) where the IT person can update the status (and maybe the date of status change, plus any other info you need). A script detects when the IT person changes the status from "Open", and an email is sent. - Tedinoz
Would you please explain how the TicketID is generated? Since the staff member is creating the form, how do you ensure that each Ticket Number is unique? - Tedinoz

2 Answers

0
votes

You are building a support ticket system using Google Forms and Google Sheets. You are having problems matching a Ticket Number from two spreadsheets.

The following scripts are offered as a way to simplify the management of the tickets so that "matching" isn't required. There are two scripts:

  • so5894534201(e): which should be installed as an Installable onFormSubmit trigger.
  • onEdit(e): a script with executes onEdit.

so5894534201(e)
The general approach is that users submit the form which updates the Form Response sheet. onFormSubmit captures the response and adds it to the "Support" sheet. It also adds an extra field "Status" to which it assigns a Data Validation rule (the options are "Open", "Pending" and "Closed"), and sets an initial value of "Open".

onEdit(e)
This watches for a change in the status on the support sheet. The anticipated action is that an email will be sent to the person who lodged the ticket. I have left this for you to complete.


function so5894534201(e) {

  // to be installaed as an Installable onFormSubmit trigger

  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // reference the sheets
  var responsesheetname = "Form Responses 1";
  var response = ss.getSheetByName(responsesheetname);
  var supportsheetname = "support";
  var support = ss.getSheetByName(supportsheetname);
  var settingssheetname = "settings";
  var settings = ss.getSheetByName(settingssheetname);

  // event objects
  //Logger.log(JSON.stringify(e)); // DEBUG

  // create temporary array to hold response values
  var responsedata = [];
  responsedata.push(e.values[0]);
  responsedata.push(e.values[1]);
  responsedata.push(e.values[2]);
  responsedata.push(e.values[3]);
  responsedata.push(e.values[4]);
  responsedata.push(e.values[5]);

  // get last row on Support
  var Avals = support.getRange("A1:A").getValues();
  var supportLR = Avals.filter(String).length;
  // Logger.log("DEBUG: the last row in support = "+supportLR)

  // get the range to the next support response
  var supportRange = support.getRange(supportLR+1,1,1,6);
  // Logger.log("DEBUG: the suppport target range = "+supportRange.getA1Notation())

  // build status cell on Support
  var cell = support.getRange(+supportLR+1,7);
  // Logger.log("DEBUG: the status cell range is "+cell.getA1Notation());

  // build the data validation rule
  var settingsRange = settings.getRange("A2:A4");
  var statusrule = SpreadsheetApp.newDataValidation()
    .requireValueInRange(settingsRange, true)
    .build();

  //set the rule
  cell.setDataValidation(statusrule);

  // Update the initial Status value 
  cell.setValue("Open");

  // Update the support range with the latest response
  supportRange.setValues([responsedata]);

}

function onEdit(e){

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var supportsheetname = "support";
  var support = ss.getSheetByName(supportsheetname);

  // get the event objects
  //Logger.log(JSON.stringify(e)); // DEBUG
  var column = e.range.getColumn();
  var sheet = e.range.getSheet().getName();
  var newStatus = e.value; // new status value
  var oldStatus = e. oldValue // old value

  // if column = column 7 (G) && sheet = "support" && newstatus isn't Open (i.e. isn't the new support response
  if(column == 7 && sheet == supportsheetname && newStatus != "Open"){
    // do stuff
    //get the data
    var row = e.range.getRow();
    var supportdataRange = support.getRange(row,1,1,7);
    Logger.log("support data range = "+supportdataRange.getA1Notation());
    Logger.log("Edited row = "+row);
    var supportdata = supportdataRange.getValues();
    var timeStamp =  supportdata[0][0];
    var ticketNumber = supportdata[0][1];
    Logger.log("Error logged:"+timeStamp+", Ticket Number:"+ticketNumber)
    var name =  supportdata[0][2];
    var phone = supportdata[0][3];
    var email = supportdata[0][4];
    var problem =   supportdata[0][5];
    Logger.log("Name:"+name+", Phone:"+phone+", Email:"+email+", Problem:"+problem);

    // Logger.log("DEBUG: onedit was triggered the old status was "+oldStatus+", and the new status is "+newStatus)
    // send an email with this information
  }
else
  {
    // nothing to do because status wasn't chnaged from "Open"
    Logger.log("DEBUG: do nothing - status wasn't changed FROM 'Open'")

  }
}

Support sheet

Support sheet snapshot


Settings sheet

Settings sheet snapshot

0
votes

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

    }

  }

}