如何循环浏览一列,当达到某个值时,将根据同一行中的值发送一封电子邮件,并在设定范围内执行此操作?

How do I get cycle through a column and when a certain value is hit, an email is sent based on values in the same row, doing this for a set range?

我是编码新手,特别是使用 google 脚本。我有一个包含文档和文档 ID 的电子表格,当逾期时,截止日期会变为红色并像交通灯系统一样完成。

然而,我需要为工作设置的是当 L = 1 列中的值(基于行中的红色和其他值为真)且值为 0 时自动发送电子邮件我希望脚本通过它并向下移动通过该列。

我尝试阅读和测试有关使用 do、while 和 for 等的代码,但我就是无法让它工作。

所以我确定我设置的是低效和简陋的,但它起作用了,只是很慢 - 请参见下面的代码。

当它确实命中 1 时,它会使用激活单元格和偏移量从相应行中的单元格中获取值,并将电子邮件打包并发送。然后脚本向下移动并检查下一个单元格,但是因为我正在使用激活并调用多个函数,所以循环遍历该列需要很长时间。

列为L,使用范围为L2:L60

如果有人能指出正确的方向,请告诉我最好的方法是什么,我将不胜感激。

function Findfirstcell() 
{
SpreadsheetApp.getActive().getRange('l2').activate();
GetLvalue();  
}

function GetLvalue() 
{
var st= SpreadsheetApp.getActive().getActiveSheet().getActiveCell().getValue();
if (st < 1)
Movetonext();
else
Createemail();
}

function Createemail() 
{
var ss= SpreadsheetApp.getActive()
var x = ss.getActiveSheet()
const s= x.getActiveCell();
var y= s.offset(0,-5);
var v= y.getValue();
var t= s.offset(0,-10);
var z= s.offset(0,-11);
var p= t.getValue();
var w= z.getValue();
var emailAddress = v+'@gmail.com'
var message = p+" is overdue - Document ID  "+w
var subject = w+"  is Overdue"
MailApp.sendEmail(emailAddress, subject, message);
var w= s.offset(0,-3);
w.setValue('Sent')
var end= s.offset(1,0);
end.activate();
GetLvalue();
}

function Movetonext()
{
var ss= SpreadsheetApp.getActive();
var s= ss.getActiveSheet().getActiveCell();
var end= s.offset(1,0);
end.activate();
var v= s.getValue();
if (v < 0) 
ss.getRange('a1').activate();
else
GetLvalue();
}

我已经尝试了其他几种方法,但都没有成功,如下所示,它在第一行执行此操作(可能是因为值为 1),但它不会继续向下列。 我假设条件失败,触发函数 createemail 并且没有任何东西可以循环返回

function Findfirstcell() 
{
var ss= SpreadsheetApp.getActiveSheet().getActiveCell('l2').activate;

// Or 

var ss= SpreadsheetApp.getActiveSheet().getRange('l2:l60');

for(ss<0; ss>0; ss++){createemail();}
}

function createemail()
{
var st= SpreadsheetApp.getActive().getCurrentCell();
var y= st.offset(0,-5);
var v= y.getValue();
var t= st.offset(0,-10);
var z= st.offset(0,-11);
var p= t.getValue();
var w= z.getValue();
var emailAddress = v+'@gmail.com'
var message = p+" is overdue - Document ID  "+w
var subject = w+"  is Overdue"
MailApp.sendEmail(emailAddress, subject, message);
}

任何指导将不胜感激

我相信你的目标如下。

  • 您想检查“L”列的值是0还是1
  • 当值为1且“I”列的值不是Sent时,您想使用“A”、“B”和“”列的值发送电子邮件G".
  • 发送电子邮件时,您想将 Sent 的值放入“I”列。
  • 您想减少脚本的处理成本。

这样的话,下面的流程如何?

  1. 从活动中检索值 sheet。
  2. 检查“I”和“L”列的值并发送电子邮件和return范围列表。
  3. 发送电子邮件时,“已发送”的值被放入“I”列。

当此流程反映到 Google Apps 脚本时,它变成如下。

示例脚本:

function myFunction() {
  // 1. Retrieve values from the active sheet.
  const sheet = SpreadsheetApp.getActiveSheet();
  const [, ...values] = sheet.getDataRange().getValues();

  // 2. Check the value of column "I" and "L" and send the email and return the range list.
  const rangeList = values.reduce((ar, [a, b, , , , , g, , i, , , l], r) => {
    if (l > 0 && i != "Sent") {
      var emailAddress = g + '@gmail.com';
      var message = b + " is overdue - Document ID  " + a;
      var subject = a + "  is Overdue";
      MailApp.sendEmail(emailAddress, subject, message);
      ar.push(`i${r + 2}`);
    }
    return ar;
  }, []);

  // 3. When the email is sent, the value of "Sent" is put to the column "I".
  if (rangeList.length > 0) sheet.getRangeList(rangeList).setValue("Sent");
}

参考文献: