问题:基于电子邮件的Google表单回复

时间:2017-05-24 20:47:12

标签: javascript

我找到了我正在寻找的部分答案。这是我更新的问题:在表单提交时,根据表单中提供的答案生成自定义电子邮件。我遇到的问题是当前代码不提供在最后一行找到的数据。我收到的电子邮件包含来自右表,右列但错误行的数据。我想要与最后一行相关的数据(最后一个表单提交)。我该怎么做?

function sendEmail(range) {
Logger.log("[METHOD] sendEmail");


var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('THIS SHEET HAS THE DATA I WANT');   
var values = sheet.getDataRange().getValues()
var row = values[0];

var Name = row[1];
var teacherEmail = row[2];
var principalEmail = row[3];
var riskType = row[13];
var riskTodo = row[14];

var emailRecipients = "franzplangger@gmail.com";
var emailSubject = "Trip plan submitted by "+teacherEmail+"";
var emailBody = "This is static text that gets pre-filled, the text below is taken directly from the google sheet (and based on what the teacher clicks)<p>\
<h3 style='line-height:90%'> These are the hazards: "+riskType+"<br /> <br>(These are the defenses "+riskTodo+")";

// SEND EMAIL //
MailApp.sendEmail({
to: emailRecipients,
subject: emailSubject,
htmlBody: emailBody
});
}

这是表单的链接:https://docs.google.com/spreadsheets/d/1EUPTkTtJkPA9hEdrb7TM-VOzy6_-ib0Qg9-KZezAgPQ/edit?usp=sharing

1 个答案:

答案 0 :(得分:0)

我设法找到了我的问题的答案(在程序员或两个人的帮助下)。我在这里发布答案,以防它帮助其他人。我有3个需求。

首先:我需要从特定工作表中获取电子邮件数据(表单答案需要在单独的工作表上完成一些计算)。

其次,我需要找到该表的最后一行。

第三:因为我在第二张纸上的计算在所有单元格中重复(为了在回答新表格时自动化处理),我需要将最后一行标识为字符串(单元格中的可见文本)而不是内部的值细胞(大多数细胞中的背景配方被视为具有值而不是空的细胞)。

以下是代码:

function sendEmail(range) {

     //Get values in last row of sheet (sheet has a repeating formula in background.
     // This requires more code to identify last row with 
     // written content as opposed to last row with formula in it)
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetname'); //this goes
                                             //to the specific sheet I want the data from  
var values = sheet.getDataRange().getValues();

var column_2_Data;
    column_2_Data = sheet.getRange(1,2,sheet.getLastRow(),1).getValues();
    column_2_Data = column_2_Data.toString();//this converts column 2 to string
    column_2_Data = column_2_Data.split(",");//this converts to a 1D array
        //I chose column 2 because this column did not have a formula in it. 
        //It ended at the same row as the last form submission.  

 var lastRowOfData = column_2_Data.indexOf("");//this gets the index of the empty element 
                                              //(the position of the empty row)
 var lastRow = lastRowOfData - 1; //data is zero indexed - subtract 1

      //code to link customized portion of the email with specific columns in sheet
 var row = values[lastRow];
 var name = row[1];
 var personXEmail = row[2];
 var bossEmail = row[3];
 var riskType = row[13];
 var riskTodo = row[14];

      //email content
 var emailRecipients = ""+bossEmail+"";
 var emailSubject = "Trip plan submitted by "+personXEmail+"";
 var emailBody = "This is static text that gets pre-filled, the text below is taken directly from the google sheet (and based on what the person clicks)<p>\
                  <h3 style='line-height:90%'> These are the hazards: "+riskType+"<br /> <br>(These are the defenses "+riskTodo+")";

     // SEND EMAIL
 MailApp.sendEmail({
 to: emailRecipients,
 subject: emailSubject,
 htmlBody: emailBody
});
}

如果您只需要从没有重复公式的工作表的最后一行获取数据,则可以使用这个更简单的代码。

function sendEmail(range) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetname');   
var values = sheet.getDataRange().getValues();
var lastRow;
    lastRow = values.length - 1;//data is zero indexed - subtract 1
var row = values[lastRow];

     //the rest would be 
var name = row[1];
var personXEmail = row[2];
    //etc......

我希望这会有所帮助。