Need "On Form Submit" event trigger only based on one form in a multiple sheet workbook

186 views
Skip to first unread message

Wild Bears ME

unread,
Mar 25, 2021, 6:30:21 PM3/25/21
to Google Apps Script Community
Good afternoon!
I have two Google forms connected to two separate worksheets in a Google sheet workbook.  I have only one "From Spreadsheet On Form Submit" event trigger in this script. Now, when either form submits, the script below runs.  But I just one form to trigger my script below, not the other form. How can I trigger the script below based on one form only?  Thank you!

Vincent

function afterFormSubmit(e) {
const info=e.values;
const PDFFile=createPDF(info);
const entryRow=e.range.getRow();
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(entryRow,40).setValue(PDFFile.getUrl());
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(entryRow,41).setValue(PDFFile.getName());
}

function createPDF(info) {

// const info={
// 'Today Date': ['2021/01/14'],
// 'Check-in Date': ['2021/01/14'],
// 'Check-out Date': ['2021/01/15']
// };

const pdfFolder=DriveApp.getFolderById("1FF4ucjbORORQeikAGO32rwcorZzB_5IP");
const tempFolder=DriveApp.getFolderById("1O5ZNjCLNLE6OnuyInkhWaobPjlJ0yCXM");
const templateDoc=DriveApp.getFileById("1s-i-KbDHo_RHRqhZtFXPAkx3aJYdGs9CVQWDAyUEnmM");

const newTempFile=templateDoc.makeCopy(tempFolder);
const openDoc=DocumentApp.openById(newTempFile.getId());

const body=openDoc.getBody();
body.replaceText("{todayDate}",info['Today Date'][0]);
body.replaceText("{checkInDate}",info['Check-in Date'][0]);
body.replaceText("{checkOutDate}",info['Check-out Date'][0]);
body.replaceText("{guest1FirstName}",info['Guest 1 First Name'][0]);
body.replaceText("{guest1LastName}",info['Guest 1 Last Name'][0]);
body.replaceText("{guest1DriverLicense}",info['Guest 1 Driver License'][0]);
body.replaceText("{guest1DOB}",info['Guest 1 DOB'][0]);
body.replaceText("{guest2FirstName}",info['Guest 2 First Name'][0]);
body.replaceText("{guest2LastName}",info['Guest 2 Last Name'][0]);
body.replaceText("{guest2DriverLicense}",info['Guest 2 Driver License'][0]);
body.replaceText("{guest2DOB}",info['Guest 2 DOB'][0]);
body.replaceText("{address}",info['Address'][0]);
body.replaceText("{city}",info['City'][0]);
body.replaceText("{state}",info['State'][0]);
body.replaceText("{zip}",info['ZIP'][0]);
body.replaceText("{cellPhone}",info['Cell Phone Number'][0]);
body.replaceText("{email}",info['Email Address'][0]);
body.replaceText("{totalAdults}",info['Total Adults'][0]);
body.replaceText("{totalChildren}",info['Total Children'][0]);
body.replaceText("{emContactPhone}",info['Emergency Contact Number'][0]);
body.replaceText("{emName}",info['Emergency Contact Name'][0]);
body.replaceText("{emRelationship}",info['Emergency Contact Relationship'][0]);
body.replaceText("{rvDescription}",info['RV Description'][0]);
body.replaceText("{pvDescription}",info['Personal Vehicle Description'][0]);
body.replaceText("{petNumber}",info['Number of Pets'][0]);
body.replaceText("{petDescription}",info['Pet Description'][0]);
body.replaceText("{nameOnCreditCard}",info['Name on Credit Card'][0]);
body.replaceText("{billingZIP}",info['BILLING ZIP'][0]);
body.replaceText("{creditCardNumber}",info['Credit Card Number'][0]);
body.replaceText("{expirationDate}",info['Expiration Date'][0]);
body.replaceText("{cvv}",info['CVV'][0]);
body.replaceText("{signature}",info['Signature'][0]);

openDoc.saveAndClose();

const blobPDF=newTempFile.getAs(MimeType.PDF);
const PDFFile=pdfFolder.createFile(blobPDF).setName("Regisration-"+info['Guest 1 First Name'][0]+"-"+
info['Guest 1 Last Name'][0]+"-"+info['Check-in Date'][0]);

tempFolder.removeFile(newTempFile);
return PDFFile;
}

CBMServices Web

unread,
Mar 25, 2021, 7:14:35 PM3/25/21
to google-apps-sc...@googlegroups.com
This trigger is workbook based and not sheet based so it will trigger on either format submit.

What you can do is check the sheet that is active and if it is not the one you want, then just terminate it.

Use get active sheet and if not right one, then just return otherwise continue with processing.


--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1ae77f9a-0345-4017-9bb6-79e3bcd9d323n%40googlegroups.com.

Alan Wells

unread,
Mar 25, 2021, 7:23:03 PM3/25/21
to Google Apps Script Community
You can get the sheet tab id of the sheet tab that received the answers.

function  afterFormSubmit  (e) {

  var sheetTabId = e.range.getSheet().getSheetId();//Get the ID number of the sheet tab that just got the answers

  if (sheetTabId !== "ID_to_Use") {
    return;
  }
}

You can also get the Form URL of the linked Form to the sheet if you wanted to check it that way.

SpreadsheetApp.getActiveSheet().getFormUrl()

Martin Molloy

unread,
Mar 25, 2021, 7:28:57 PM3/25/21
to google-apps-sc...@googlegroups.com
Try adding

 var sheetName =  SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getSheetName()
 if(sheetName != 'YourTargetSheetName'){
 return
 }




Wild Bears ME

unread,
Mar 25, 2021, 7:37:50 PM3/25/21
to Google Apps Script Community
Perfect! All suggestions work. Thank you so much!

Vincent

Reply all
Reply to author
Forward
0 new messages