简体   繁体   English

每次发送电子邮件时如何在每行中标记一个单元格

[英]How mark a cell in each row every time an email is sent

I am looking for a way to get a cell to mark if the email gets sent from a google sheet.如果电子邮件是从 Google 表格发送的,我正在寻找一种方法来标记单元格。 For an example if the email sent it would say in column P "email sent" on that row or something like that.例如,如果发送的电子邮件会在该行的 P 列中显示“已发送电子邮件”或类似内容。 I'm pretty new to this I found some helpful similar questions but I just don't know how to set it up.我对此很陌生,我发现了一些有用的类似问题,但我只是不知道如何设置。 I think I need to use an IF command but don't know how or where to set it我想我需要使用 IF 命令,但不知道如何或在哪里设置它

function SendStudentEmails() {

  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour").activate();//use name of active sheet

  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var lr = ss.getLastRow();

  var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DO NOT DELETE! Parent Email Template").getRange(1, 1).getValue();

  var quotaLeft = MailApp.getRemainingDailyQuota();

  if((lr-1) > quotaLeft) {
    Browser.msgBox("You have " + quotaLeft + " left and you're trying to send" (lr-1) + " emails. Emails were not sent")
  } else {

    for (var i = 2;i<=lr;i++){  

    var currentEmail = ss.getRange(i,14).getValue(); // 15 is the email column        
    var currentStudent = ss.getRange(i, 4).getValue(); // 4 is student name column
    var currentMessage = ss.getRange(i, 2).getValue(); // 2 is the message column
    var currentMissingAssingment = ss.getRange(i, 9).getValue(); // 9 is the missing assingment column

    var massageBody = templateText.replace("<<Student Name>>",currentStudent).replace("<<Message>>",currentMessage).replace("<<Missing Assingment>>",currentMissingAssingment);
    var subjectLine = currentStudent +" Has Homework Hour"; 


   if (currentEmail.trim() !== '')// checks if email is not blank  
   MailApp.sendEmail(currentEmail, currentStudent, massageBody); 

  } 

 }

}

I'm new to answering, but I hope this helps you out!我是新来的回答,但我希望这可以帮助你! It sounds like you want to mark next to each row of a sent email, in which case, you already have your if/for cases setup to do this (except that you appear to be missing one set of braces for the last if test, as below).听起来您想在已发送电子邮件的每一行旁边进行标记,在这种情况下,您已经设置了 if/for 案例来执行此操作(除了最后一个 if 测试似乎缺少一组大括号,如下)。 You can just add whatever text you want to column P in the row after the email gets sent.发送电子邮件后,您可以在行中的 P 列中添加任何您想要的文本。 Here is that section of the code with the added part to add text to column P:这是代码的一部分,其中添加了将文本添加到 P 列的部分:

if (currentEmail.trim() !== '')// checks if email is not blank {  
   MailApp.sendEmail(currentEmail, currentStudent, massageBody); 
   ss.getRange(i,16).setValue("email sent") // writes text (email sent) into column P (col 16), row i (the current row in the for loop)
    }
  } 

You could use setBackground("color") too if you want to change background color of your cell.如果您想更改单元格的背景颜色,也可以使用setBackground("color")

Here is a small example:这是一个小例子:

ss.getRange(5, 15).setBackground("green") - it will change background color to green in cell N5 ss.getRange(5, 15).setBackground("green") - 它将单元格 N5 中的背景颜色更改为绿色

This is how it looks like in your snippet, (below MailApp, trigger setBackground after MailApp does the work):这就是您的代码段中的样子(在 MailApp 下方,在 MailApp 完成工作后触发 setBackground):

function SendStudentEmails() {

    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Homework Hour").activate();

    var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var lastRow = ss.getLastRow();
    var templateText = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DO NOT DELETE! Parent Email Template").getRange(1, 1).getValue();
    var quotaLeft = MailApp.getRemainingDailyQuota();

    if ((lastRow - 1) > quotaLeft) {

        Browser.msgBox("You have " + quotaLeft + " left and you're trying to send " + (lastRow - 1) + " emails. Emails were not sent")

    } else {

        for (var i = 2; i <= lastRow; i++) {

            var currentEmail = ss.getRange(i, 15).getValue(); // 15 is the email column        
            var currentStudent = ss.getRange(i, 4).getValue(); // 4 is student name column
            var currentMessage = ss.getRange(i, 2).getValue(); // 2 is the message column
            var currentMissingAssingment = ss.getRange(i, 9).getValue(); // 9 is the missing assingment column

            var massageBody = templateText.replace("<<Student Name>>", currentStudent).replace("<<Message>>", currentMessage).replace("<<Missing Assingment>>", currentMissingAssingment);
            var subjectLine = currentStudent + " Has Homework Hour";


            if (currentEmail.trim() !== '' && ss.getRange(i,15).getBackground !== 'green') { // checks if email is not blank  
                MailApp.sendEmail(currentEmail, currentStudent, massageBody);
                ss.getRange(i, 15).setBackground("green") // change cell background to green 

            }

        }

    }

}

Reference:参考:

暂无
暂无

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

相关问题 jQuery:如何选择表中除每行的最后一个单元格外的每个单元格? - jQuery: how to select every cell in a table except the last in each row? 标记行是重要的,并在每次加载时保存它 - Mark Row as important and keep it saved each time it loads javascript如何在每个单元格中添加一行? - javascript how to add a row every forth cell? 取消选中所有内容时显示消息,并在每次标记每个检查时显示其他消息 - Show message when unchecking everything and show other message every time I mark each check 我如何访问在Word或Excel中添加的表并使用Office.js遍历每一行和每个单元格 - How can i Access the table added in Word or Excel and iterate throght each and every row and cell using Office.js 在两列的每一行中的每个单元格中添加1,直到到达空白单元格-Google Spreadsheet - Add 1 to each cell in every row of two columns until a blank cell is reached - Google Spreadsheet 无法使用 JQuery 或 Javascript 将每个空单元格标记为空单元格 - Failed to mark every empty cell as Empty Cell with JQuery or Javascript 如何将数组复制到从某个单元格开始的列的每个单元格,而不是复制到行的每个单元格? - How can I copy an array to each cell of a column starting from a certain cell, instead of to each cell of a row? 每次单击表格单元格时,我都想控制台记录每个单元格的值,每次单击时? 什么是正确的称呼? - Every time I click the tables cell I want to console.log the value with each every cell, every time I click? what is the proper calling? 如果每个单元格都是子组件,如何获取行索引? - How can I get row index if every cell is a child component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM