简体   繁体   English

Google脚本发送包含工作表中值的电子邮件

[英]Google Script to Send Email containing values from Sheet

I am trying to get a script working that hooks into a google sheet, pulls information that is taken of the phone, and sends it via email once marked. 我正在尝试获取一个脚本,该脚本可以挂接到Google工作表中,提取手机中获取的信息,并在标记后通过电子邮件发送。 So far I have this, from sources on the internet and a bit of customising. 到目前为止,我已经从互联网上获得了一些定制信息。

function sendApprovalEmail() {

var sheetNameToWatch = "SHEETNAMEGOESHEREUSUALLY";
var columnNumberToWatch = 12; // column A = 1, B = 2, etc.
var valueToWatch = "SENT";

var date = 5;
var name = 9;
var number = 10;
var message = 11;

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();

if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var emailAddress = "MYEMAILGOESHEREUSUALLY";
var subject = "Missed Call Notifcation";
var email = "Hello! "+date+" "+name+" "+number+" "+message+"";
MailApp.sendEmail(emailAddress, subject, email);
}
}

The above works, triggers, and sends properly. 以上内容可以正常工作,触发和发送。 The only thing it doesn't do is suck in the Date, Name, Number and Message. 它唯一不做的就是吸取日期,名称,编号和消息。

These need to be unique for each email, based on the line that was just marked as SENT. 根据刚刚标记为SENT的行,每封电子邮件都必须具有唯一性。 Usually, there are only a couple a day, never at the same time. 通常,只有一天,而不是同一天。

So if Row 23 is marked as SENT, it needs A23, B23, and C23. 因此,如果将第23行标记为SENT,则需要A23,B23和C23。 If row 66 is marked as SENT, it needs A66, B66, and C66. 如果第66行标记为SENT,则需要A66,B66和C66。

How do I get the script to look up values in column 5, 9, 10 and 11 OF THE ROW that it has detected 'SENT' in? 如何获取脚本以在脚本中检测到“已发送”的行的第5、9、10和11列中查找值?

The code is using var range = sheet.getActiveCell() the get the active cell. 该代码使用var range = sheet.getActiveCell()获取活动单元格。 Then you could use range.getRow() to get the row number, then you could use something like sheet.getRange(range.getRow(), columnNumber) to get the desired cells where columnNumber could be 5, 9, 10 and/or 11. In other words, instead of 然后可以使用range.getRow()获取行号,然后可以使用诸如sheet.getRange(range.getRow(), columnNumber)来获取所需的单元格,其中columnNumber可以是columnNumber和/或11.换句话说,代替

var date = 5;

use 采用

var date = sheet.getRange(range.getRow(), 5).getValue();

but put the above line after 但是把上面的线放在

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();

Do the same for the name, number and message variables. 对名称,数字和消息变量执行相同的操作。

Note: There are other more efficient ways like using sheet.getDataRange().getValues() which will return an array of arrays having all the sheet values, the use 注意:还有其他更有效的方法,例如使用sheet.getDataRange().getValues() ,它将返回具有所有图纸值的数组数组,

var date = data[range.getRow()][5];

and so on. 等等。

This is probably close to what your looking for: 这可能与您要寻找的接近:

First you have to settle on a format for your spreadsheet so that all operators will record the information in a standard format. 首先,您必须确定电子表格的格式,以便所有操作员都能以标准格式记录信息。 I used the following format for each of my sheets. 我为每个工作表使用以下格式。

在此处输入图片说明

All of the sheets used for recording information start with the prefix of 'ph:'. 用于记录信息的所有工作表均以“ ph:”前缀开头。 I get the entire array of sheets and I loop through them looking for the sheets that begin in 'ph:' name.slice(0,3) . 我得到了整个工作表数组,然后遍历它们以查找以'ph:' name.slice(0,3)开头的工作表。 Once found I get the range of A1:B6 and collect all of the data into one array and then loop through the array putting all of the information into an associative array using the following algorithm dObj[value in column1]=value in column2. 找到后,我得到A1:B6的范围并将所有数据收集到一个数组中,然后使用以下算法dObj [value in column1] = value in column2,遍历该数组,将所有信息放入关联数组。 So in my example you can get the name with dObj.Name and the message is obtained by using dObj.Message and so on. 因此,在我的示例中,您可以使用dObj.Name来获取名称,并通过使用dObj.Message等来获取消息。 So this associates the key in column one with the value in column2. 因此,这会将第一列中的键与第二列中的值相关联。

Now here is the code: 现在这里是代码:

function sendApprovalEmails(){
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  for(var i=0;i<shts.length;i++){
    var sh=shts[i];
    var name=sh.getName();
    if(name.slice(0,3)=='ph:'){//finds the operator log sheets
      var dObj={};
      var rg=sh.getRange('A1:B6');//Use a fixed range so you can use the rest of the sheet for whatever
      var vA=rg.getValues();
      for(var j=0;j<vA.length;j++){
        dObj[vA[j][0]]=vA[j][1];//Builds the associative array
      }
      if(dObj.Approval=='Yes'){
        Logger.log('Send Email:\nDate: %s\nNumber: %s\nName: %s\nMessage: %s,To: %s',Utilities.formatDate(new Date(dObj.Date),Session.getScriptTimeZone(),"E MMM dd, yyyy"),dObj.Number,dObj.Name,dObj.Message,dObj.Email);
        var recipient=dObj.email;
        var subject="Missed Call Notification";
        var body = Utilities.formatString('Hello! %s %s %s %s',Utilities.formatDate(new Date(dObj.Date),Session.getScriptTimeZone(),"E MMM dd, yyyy"),dObj.Name,dObj.Number,dObj.Message);
        //MailApp.sendEmail(recipient, subject, body);
      }
    }
  }
}

In my example B1 has a data validation of either Yes or No. 在我的示例中,B1的数据验证为“是”或“否”。

Of course this is probably just a starting point. 当然,这可能仅仅是一个起点。 You can create the code to copy a master sheet and open it up with each phone call. 您可以创建代码以复制母版纸,并在每次通话时打开它。 I don't know if there is a limit to the number of sheets but there is aa limit to the number of cells in one Spreadsheet. 我不知道工作表数量是否有限制,但一张电子表格中的单元格数量是否有限制。

Documentation Reference: 文档参考:

By the way this version of your code works. 顺便说一下,此版本的代码可以正常工作。

function sendApprovalEmail() {

var sheetNameToWatch = "test";
var columnNumberToWatch = 1; 
var valueToWatch = "SENT";
var date = 5;
var name = 9;
var number = 10;
var message = 11;

var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var range = sheet.getActiveCell();

if (sheet.getName()==sheetNameToWatch && range.getColumn()==columnNumberToWatch && range.getValue() == valueToWatch) {
var emailAddress = "foundit@found.com";
var subject = "Missed Call Notifcation";
var email = Utilities.formatString('Hello! %s %s %s %s',date,name,number,message);
//MailApp.sendEmail(emailAddress, subject, email);
  Logger.log('\nemailAddress: %s\nsubject: %s\nemail: %s',emailAddress,subject,email);
}
}

I don't like to send out a lot of useless emails while debugging so I just use the Logger to record the email information. 我不喜欢在调试时发送大量无用的电子邮件,因此我仅使用Logger记录电子邮件信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从谷歌表格发送 email 表格作为 PDF 附件 - How to send email from google sheet with the sheet as a PDF attachment 从单元格值Google工作表发送电子邮件,以及电子邮件中的其他列 - Send email from cell value google sheet, with other columns in email 如何使用gs从谷歌表格发送带有格式化表格的电子邮件? - How to send an email with a formatted table from google sheet using gs? 从 Google 工作表发送电子邮件作为表格而不使用工作表转换器 - Send Email from Google sheet as a table without using sheets convertor 从 Google 表格自动发送包含表格的电子邮件 - Automatically Send Email Containing Table From Google Sheets 使用电子表格中所有新行的值发送一封电子邮件(Google Script / GAS) - Send single email with values from all new rows in a spreadsheet (Google Script / GAS) 脚本应发送一封包含表单提交值的电子邮件,但不会发送任何电子邮件 - Script should send an email containing form submission values, but no email is getting sent 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data Google Apps脚本:如何基于E列中的值从A列中提取值,并通过一封电子邮件发送所有值? - Google Apps Script: How to pull values from column A based on values in column E and send all values in one email? Google App Script + Google Sheet + HTML 模态对话框输入表单 - 获取值并构建 email - Google App Script + Google Sheet + HTML modal dialog input form - Get values and build email
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM