简体   繁体   中英

Google Form to pull data from spreadsheet and email to user

I have a spreadsheet containing all of the annual leave balances for each staff member. I have a form that the staff will fill out, and I would like it to pull the their data from the spreadsheet and send them an email with the result.

I am having trouble with the matching of form data to the spreadsheet data, and extracting the related cells. Here is the code:

function SendGoogleForm(e) {  
  try {      

    var subject = "Leave Balance Request";  
    var s = SpreadsheetApp.openByUrl("url").getSheetByName("Check Leave");

    var email = e.namedValues["BCA National email address"];
    var sheet =  SpreadsheetApp.openByUrl("url").getSheetByName("Leave Summary");
    var cell = sheet.getRange("F3:F3");
    var getcell = cell.getCell(1,1);
    var pdate = getcell.setValue(e.namedValues['END date of leave requested']);
    var annualvalue = sheet.getRange("H6:H").getValues();
    var emaildata = sheet.getRange("A6:A").getValues();
    var username = e.namedValues["Username"];

    var message = "Your" + " " + e.namedValues["Type of leave requested"] + " " + "balance        is below" + "\n\n";   

    if (username == emaildata)  { message += "Annual Leave" + " : " +    annualvalue               + "\n\n"; }

    MailApp.sendEmail(email, subject, message);

  } 

  catch (e) {
    Logger.log(e.toString());
  }
}

I tried this: How do I search for and find the coordinates of a row in Google Spreadsheets

But couldn't get it to work...

Any suggestions would be greatly appreciated!

When you write var emaildata = sheet.getRange("A6:A").getValues(); , you are getting an array of arrays with strings. You will need to iterate in a for loop to test each value.

Something like that should work :

var message = "Your" + " " + e.namedValues["Type of leave requested"] + " " + "balance        is below" + "\n\n";   
for(var n in emaildata){
  if (username == emaildata[n][0])  { message += "Annual Leave" + " : " +    annualvalue               + "\n\n"; }
}
MailApp.sendEmail(email, subject, message);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM