Setting up Triggers in Google Apps for Debugging

In my previous article I wrote about setting up a workflow for users to enter data into a Google form and the information is merged into a document and a PDF is created. You can read that article here. When you are trying to diagnose issues triggers with notifications can really help your debugging problems. Lets take a look at setting up triggers in Google Apps scripting.

We are going to pickup in our script editor in Google apps (Spreadsheet – Tools – Script editor)

 

 

Here you should see your code displayed for your different functions:

To setup our trigger click Resources, Current Scripts Triggers.

 

 

A popup should come up that looks like this:

Click to add a new trigger. The options are very simple, especially for what we want to do. In your code you have to link the code to the form and this is where you do that. In the image below you can see we are running our function when the form is submitted. To receive an email click the notifications button at the end.

 When you click “notifications” you should see another popup that allows you to select immediately . This allows you to get your debug information immediately.

 

Now that notifications are setup you will get emails that look like this:

 

 

When there are issues.

 

If you have questions comment below or email me at TJ at TJhouston.com

 

 

Thanks

 

TJ Houston

TJ Houston the Vice President of Managed services at Epiphany Management Group. TJ leads professional development in all areas of technology and loves developing ways to make teachers, students, and staff's lives easier with the use of technology. TJ also loves photography and his portfolio can be found at http://www.tjshots.com. All of the views on this site are his own.

  • Melanie King

    Lots of stuff here now deprecated in the new Google Scripts.. https://code.google.com/p/google-apps-script-issues/issues/detail?id=5017 using the following get’s it working again.

    1. Replaced all instances of docsList with DriveApp
    2. Replaced all instances (with quotes) of “document” and “DOCUMENT” with MimeType.GOOGLE_DOCS (no quotes)
    3. Replaced all instances of getFileType with getMimeType
    4. Replaced all instances of file.addToFolder(folder) with folder.addFile(file)
    5. Replaced all instances of file.removeFromFolder(DriveApp.getRootFolder()) to DriveApp.removeFile(file);

  • Hung Phan

    Thank you for the script, TJ. When I setup the current trigger, it doesn’t offer the same option as yours. I have the “From form” rather than “From spreadsheet”. Is there recent change to Google Script or am I missing something that connect the Google Forms and its spreadsheet reponses?

    Thank you!

  • Robb Delaney

    I stumbled upon this a few months ago, and finally put it to use today. TJ, What an AWESOME script. Other than having to make the DriveApp code change.. it works perfect just the way it is. I do not get any errors when something isn’t filled in.. it just shows blank on my PDF.

    Again.. love this script!! (I have attached it here as well)

    reiterate:
    if your trigger is not setup within your script editor, notifications and PDF wont work.

    reiterate:
    if you push play on the script editor and it fails saying: “Cannot read property “values” from undefined. (line 11, file “”) ” chances are your code is working… you just need to submit the form with information so your “VALUES” contain information.

    // Get template ID from the Google Docs TEMPLATE you create and name it

    var docTemplate = “15-vyZD9roBn4deXxxxxxxxxi80CqPIn-fNis4_Jgg”;
    var docName = “NorthMetro-KarateCampForm”;
    // When Form Gets submitted
    function onFormSubmit(e) {

    //Get information from form and set our variables

    // The values are in numerical sync with the webform, meaning that the first question ased is e.values[1], second qeustion e.values[2].. and so on.
    var email_address = “robb@gmail.com”; //This is the address it will always go to by default.

    var first_name = e.values[1];
    var last_name = e.values[2];
    var session = e.values[3];
    var phone = e.values[4];
    var street = e.values[5];
    var city = e.values[6];
    var zipcode = e.values[7];
    var email = e.values[8];

    //Student Variables
    var student_first_name = e.values[9];
    var student_last_name = e.values[10];
    var student_age = e.values[11];
    var student_birthday = e.values[13];
    var student_allergyquestion = e.values[14];
    var student_allergydetails = e.values[15];
    var student_physquestion = e.values[16];
    var student_physdetails = e.values[17];

    var waiver_medical_checkbox = e.values[18];

    // Get document template, copy it as a new temp doc, and save the Doc’s id
    var copyId = DriveApp.getFileById(docTemplate)
    .makeCopy(docName+’ for ‘+last_name)
    .getId();

    // Open the temporary document
    var copyDoc = DocumentApp.openById(copyId);

    // Get the document’s body section
    var copyBody = copyDoc.getActiveSection();

    // Replace place holder keys,in our google doc template
    // Parent or Guardian information
    copyBody.replaceText(‘keyFirstName’, first_name);
    copyBody.replaceText(‘keyLastName’, last_name);
    copyBody.replaceText(‘keySession’, session);
    copyBody.replaceText(‘keyPhone’, phone);
    copyBody.replaceText(‘keyStreet’, street);
    copyBody.replaceText(‘keyCity’, city);
    copyBody.replaceText(‘keyZipcode’, zipcode);
    copyBody.replaceText(‘keyEmail’, email);

    // Student Information
    copyBody.replaceText(‘keyStudentFirstName’, student_first_name);
    copyBody.replaceText(‘keyStudentLastName’, student_last_name);
    copyBody.replaceText(‘keyStudentAge’, student_age);
    copyBody.replaceText(‘keyStudentBirthday’, student_birthday);
    copyBody.replaceText(‘keyStudentAllergyQuestion’, student_allergyquestion);

    copyBody.replaceText(‘keyStudentAllergyDetails’, student_allergydetails);
    copyBody.replaceText(‘keyStudentPhyNeedsQuestion’, student_physquestion);
    copyBody.replaceText(‘keyStudentPhyNeedsDetails’, student_physdetails);

    // Contract Details
    copyBody.replaceText(‘keyWaiverMedicalCheckbox ‘, waiver_medical_checkbox );

    // Save and close the temporary document

    copyDoc.saveAndClose();

    // Convert document to PDF

    var pdf = DriveApp.getFileById(copyId).getAs(“application/pdf”);

    // Attach PDF and send the email

    var subject = “Karate Camp Session Enrollment – ” + session +” – “+ last_name + “”;

    var body = “Here is the Camp Enrollment form for ” + last_name + “”;

    MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});

    // Delete temp file set to True.. otherwise False will save it in your drive.

    DriveApp.getFileById(copyId).setTrashed(false);

    }

  • daniellafan

    Lets say you wanted the process to automatically convert the grade level to another value. Specifically if you wanted to single out high school sophomores. If the grade level value is ten then that cell is automatically changed to “sophomore” automatically upon submission. I’d like to know if this is possible to do through script and not formulas, etc. Thanks!