【问题标题】:How to send data from spreadsheet by email using google scripts如何使用谷歌脚本通过电子邮件从电子表格发送数据
【发布时间】:2018-10-15 11:39:23
【问题描述】:

我正在使用 Google 表单进行在线注册。一旦有人提交,一封电子邮件将发送到他们输入的电子邮件。数据是他们在表格中输入的内容。因此,这封电子邮件用作数据确认电子邮件。现在的问题是,我所做的测试是成功的,但是电子邮件包含所有数据。我的问题是,如何通过电子邮件从特定行发送特定电子邮件的数据

这是我所做的(从某人那里复制的)

      function sendEmail() {

 //setup function
 var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
 var StartRow = 3;
 var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
 var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
 var AllValues = WholeRange.getValues();

 var message = "";
  //iterate loop
 for (i in AllValues) {

 //set current row
 var CurrentRow = AllValues[i];

 //define column to check if sent
 var EmailSent = CurrentRow[11];

 //if row has been sent, then continue to next iteration
 if (EmailSent == "sent") 
 continue;

 //set HTML template for information
 message +=
  "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
  "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
  "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
  "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
  "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
  "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
  "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
  "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
  "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";

  //set the row to look at
  var setRow = parseInt(i) + StartRow;

  //mark row as "sent"
  ActiveSheet.getRange(setRow, 11).setValue("sent");
  }

 //define who to send grants to 
 var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";

 //set subject line
 var Subject = "Grants";


  //send the actual email  
  MailApp.sendEmail({
  to: SendTo,
  cc: "",
  subject: Subject,
  htmlBody: message,
   });
    }

【问题讨论】:

    标签: email google-apps-script


    【解决方案1】:

    我最近就此事回复了similar question。对我来说,将问题分解成我们需要的部分总是比较容易的。在这种情况下,我们需要:所有的响应,那么我们需要匹配一个响应

    最好的方法是首先将表单响应读取为数​​组:

    function formResponsesToArray() {
      var app = FormApp.openById(...),
      responses = app.getResponses(), stringResponses = []
    
      responses.forEach(function(r) {
        var response = []
        r.getItemResponses().forEach(function(i) {
          response.push(i.getResponse())
        })
    
        stringResponses.push(response)
      })
    
      Logger.log(stringResponses)
    }
    

    如果您知道这些人的电子邮件,那么我们可以得到他们的回复,假设电子邮件是他们在表单中输入的第一件事:

    var theirEmail = "hello@hello.com"
    var theirResponse = stringResponses.filter(function(response) {
      return response[0] === theirEmail
    })
    

    然后您可以使用 theirResponse 获取他们在表单中输入的信息!

    【讨论】:

    • 所以意思是呃..我将响应变成一个“数组”,就像一组数据。然后在电子邮件中的消息中我所做的应该是 var 响应?那是对的吗?我如何结合谷歌表单数据和电子表格??
    【解决方案2】:

    您共享的代码基本上收集了电子邮件状态字段没有“已发送”值的所有行数据,并在最后编写一条消息并发送它。 根据您的情况,您需要为电子邮件状态字段没有“已发送”值的每一行发送不同的邮件。

    我在循环中添加了 mailApp 语法,仅用于发送不同的邮件,并对消息变量进行了一些更改。

     function sendEmail() {
    
     //setup function
     var ActiveSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
     var StartRow = 3;
     var RowRange = ActiveSheet.getLastRow() - StartRow + 1;
     var WholeRange = ActiveSheet.getRange(StartRow,1,RowRange,11);
     var AllValues = WholeRange.getValues();
    
      //iterate loop
     for (i in AllValues) {
    
     //set current row
     var CurrentRow = AllValues[i];
    
     //define column to check if sent
     var EmailSent = CurrentRow[11];
    
     //if row has been sent, then continue to next iteration
     if (EmailSent == "sent") 
     continue;
    
     //set HTML template for information
     //here i have removed the "+" sign to create a new message for its respective row
    var message=
      "<p><b>Found by: </b>" + CurrentRow[1] + "</p>" +
      "<p><b>Title: </b>" + CurrentRow[2] + "</p>" +
      "<p><b>Agency: </b>" + CurrentRow[3] + "</p>" +
      "<p><b>Summary: </b>" + CurrentRow[4] + "</p>" +
      "<p><b>Due: </b>" + CurrentRow[5] + "</p>" +
      "<p><b>Posted: </b>" + CurrentRow[6] + "</p>" +
      "<p><b>Total Funding: </b>" + CurrentRow[7] + "</p>" +
      "<p><b>Announcement Number: </b>" + CurrentRow[8] + "</p>" +
      "<p><b>Useful Links: </b>" + CurrentRow[9] + "</p><br><br>";
    
      //here you can add the email address field to whom you want to send emails.
      //e.g. var SendTo=CurrentRow[1];
       //define who to send grants to 
       var SendTo = "emailaddress1@gmail.com" + "," + "emailaddress2@gmail.com";
    
       //set subject line
       var Subject = "Grants";
    
        //send the actual email  
        MailApp.sendEmail({
        to: SendTo,
        cc: "",
        subject: Subject,
        htmlBody: message,
         });
    
    
      //set the row to look at
      var setRow = parseInt(i) + StartRow;
    
      //mark row as "sent"
      ActiveSheet.getRange(setRow, 11).setValue("sent");
      }
      }
    

    希望对你有帮助,谢谢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2012-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2020-12-15
      相关资源
      最近更新 更多