简体   繁体   中英

Google Sheets - Code to send an email to an address upon dropdown

I'm trying to set up my dropdown options in Google sheets to trigger email responses after a selection a made from the dropdown. For example, When "Ready For QA" "Error Found or "Approved" is selected in column I from the dropdown, an email should be triggered and sent to the email address in column H that includes Subject line: "Line item is ready for QA" with information from all cells in the row A through P that includes the headers from each column.

Am I missing anything from my code below?

https://docs.google.com/spreadsheets/d/1qwyvtSo_mzHztZJ7MJLfPy8TlvPYqCLIEKC9APBWeMQ/edit#gid=78026112

var sheetname = "march";
//Installable Trigger 
function sendEmail(e) {
  // Get sheet, row and column where the event was triggered 
  var sheet = e.source.getSheetByName("march");
  var range = e.range;
  var col = range.getColumn();
  var row = range.getRow();
  // Check if the event meets your conditions
  if (e.value === "Ready for QA" && col === 9) {
    // Get headers and data where the "Ready for QA" was set
    var headers =  sheet.getRange("A1:P1").getValues()[0];
    var data = sheet.getRange(row, 1, 1, 16).getValues()[0];
    MailApp.sendEmail({
      to: data[7], // user's email Also
      subject: `Line item is ${e.value}`,
      // Combine the right header with the right data and send them as the body
      body: headers.map((el, index) => `${el}: ${data[index]}`).join("\n")
    });
  }
}

在下拉列表中选择准备 QA 后发送给我的错误消息图片

For sending an email meeting the conditions and the format you want, you have to set an Installable Trigger and use this code I prepared:

//Installable Trigger 
function sendEmail(e) {
  // Get sheet, row and column where the event was triggered 
  var sheet = e.source.getSheetByName("YOUR-SHEET-NAME");
  var range = e.range;
  var col = range.getColumn();
  var row = range.getRow();
  // Check if the event meets your conditions
  if (col === 9) {
    // Get headers and data where the "Ready for QA" was set
    var headers =  sheet.getRange("A1:P1").getValues()[0];
    var data = sheet.getRange(row, 1, 1, 16).getValues()[0];
    MailApp.sendEmail({
      to: data[7], // user's email Also
      subject: `Line item is ${e.value}`,
      // Combine the right header with the right data and send them as the body
      body: headers.map((el, index) => `${el}: ${data[index]}`).join("\n")
    });
  }
}

Notice how I used the e argument, which is the Event Object for the onEdit event trigger to get the sheet values. Also, I would recommend you set your column Order ID format as Plain text because Apps Script could take it as a huge number and therefore set it into scientific notation.

Now, for setting up the installable trigger, do the following:

1) Go to your Apps Script project.

2) Click Edit->Current project's triggers.

3) Click "+ Add Trigger".

4) Select :

  • Choose which function to run -> Function Name (in this case sendEmail ).

  • Select event source-> From spreadsheet.

  • Select event type -> On edit.

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