在主函数中运行2个函数

时间:2019-04-10 19:49:04

标签: javascript google-apps-script google-sheets

我是一个初学者,如果我的代码看起来有点凌乱,我感到抱歉。

我正在尝试编写一个函数,以便在列中的单元格等于send_email时将电子邮件发送到特定的电子邮件地址。电子邮件正文需要包含来自单元格等于send_email的行中的数据。

这是我的代码:

  function warnStatusBeginDay() {
  // This function imports house data, every day, between 0am and 1am, and sends an email if the time left to answer the 'acta de observacion' is 3 or 7 days from the deadline

  //Check when to send email

  var checkValues = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('service_1_2_main').getRange('V2:AQ').getValues();

  for (var i in checkValues) {

    if (checkValues[i][4] === 'send_email') {
      //Send email notification

      function sendMail_7() {
      // Build email body
      var email_head = "https://i.imgur.com/aaa.jpg";
      var house_id = checkValues[i][10];
      var project = checkValues[i][7];
      var name = checkValues[i][14];
      var address = checkValues[i][19];
      var neigh = checkValues[i][21];
      var municipality = checkValues[i][17];
      var country = checkValues[i][18];
      var acta_date = Utilities.formatDate(checkValues[i][13],"GMT-0500","d MMM yyyy");
      var acta_date_limit = Utilities.formatDate(checkValues[i][12],"GMT-0500","EEE, d MMM yyyy");
      var record_id = checkValues[i][11];
      var lat = checkValues[i][8];
      var lng = checkValues[i][9];
      var imageURL = checkValues[i][20];
      var mapURL = "http://maps.googleapis.com/maps/api/staticmap?center="+lat+","+lng+"&zoom=15&size=300x300&maptype=hybrid&markers=color:red%7C"+lat+","+lng+"&key=myKey";
      var mapURL2 = "https://www.google.com/maps/search/?api=1&query="+lat+","+lng;

      var body = "<p>" +
                  "<p><img src='" + email_head + "' width='269' height='70' alt='Build Change - Sistema de notificación'></p>" + 
                  "<i>[Este es un mensaje automatizado, por favor no responda a este correo]</i>" + "<br>" + "<br>" +    
                  "La vivienda a continuación recibió una acta de observación el " + acta_date + ". El plazo limite para responder al acta vence en <b>7 días</b> desde hoy." + "<br>" + "<br>" +
                  "<b>Código ID vivienda: </b>" + house_id + "<br>" +
                  "<b>Proyecto: </b>" + project + "<br>" +
                  "<b>Nombre y apellido propietario: </b>" + name + "<br>" +
                  "<b>Fecha vencimiento acta de observación: </b>" + acta_date_limit + "<br>" + "<br>" +
                  "<b>Código ID Fulcrum: </b><a href=\"https://web.fulcrumapp.com/records/" + record_id + "\">" + record_id + "</a><br>" +
                  "<b>Latitud y longitud: </b><a href=\"" + mapURL2 + "\">" + lat + ", " + lng + "</a><br>" +
                  "<b>Barrio/Comuna/Localidad/Sector: </b>" + neigh + "<br>" +                  
                  "<b>Dirección: </b>" + address + "<br>" +
                  "<b>Municipalidad: </b>" + municipality + "<br>" +
                  "<b>País: </b>" + country + "<br>" +
                  "<p><a href='https://web.fulcrumapp.com/records/" + record_id + "' title='Open in Fulcrum'><img src='" + mapURL + "'></a>" + "     " + "<img src='" + imageURL + "' height='300 alt='Imagén fachada vivienda'></p>" +
                "</p>";

      // Send email
        MailApp.sendEmail({
          to: "myemail.dev@gmail.com",
          subject: house_id + " - Acta de observación en vencimiento (7 días restantes)",
          htmlBody: body
        });
      }      
      sendMail_7();            
    }
    else {
      continue;
    }  
  }

  SpreadsheetApp.flush();

  for (var j in checkValues) {

    if (checkValues[j][6] === 'send_email') {
      //Send email notification

      function sendMail_3() {
      // Build email body
      var email_head = "https://i.imgur.com/aaa.jpg";
      var house_id = checkValues[j][10];
      var project = checkValues[j][7];
      var name = checkValues[j][14];
      var address = checkValues[j][19];
      var neigh = checkValues[j][21];
      var municipality = checkValues[j][17];
      var country = checkValues[j][18];
      var acta_date = Utilities.formatDate(checkValues[j][13],"GMT-0500","d MMM yyyy");
      var acta_date_limit = Utilities.formatDate(checkValues[j][12],"GMT-0500","EEE, d MMM yyyy");
      var record_id = checkValues[j][11];
      var lat = checkValues[j][8];
      var lng = checkValues[j][9];
      var imageURL = checkValues[j][20];
      var mapURL = "http://maps.googleapis.com/maps/api/staticmap?center="+lat+","+lng+"&zoom=15&size=300x300&maptype=hybrid&markers=color:red%7C"+lat+","+lng+"&key=myKey";
      var mapURL2 = "https://www.google.com/maps/search/?api=1&query="+lat+","+lng;

      var body = "<p>" +
                  "<p><img src='" + email_head + "' width='269' height='70' alt='Build Change - Sistema de notificación'></p>" + 
                  "<i>[Este es un mensaje automatizado, por favor no responda a este correo]</i>" + "<br>" + "<br>" +    
                  "La vivienda a continuación recibió una acta de observación el " + acta_date + ". El plazo limite para responder al acta vence en <b>3 días</b> desde hoy." + "<br>" + "<br>" +
                  "<b>Código ID vivienda: </b>" + house_id + "<br>" +
                  "<b>Proyecto: </b>" + project + "<br>" +
                  "<b>Nombre y apellido propietario: </b>" + name + "<br>" +
                  "<b>Fecha vencimiento acta de observación: </b>" + acta_date_limit + "<br>" + "<br>" +
                  "<b>Código ID Fulcrum: </b><a href=\"https://web.fulcrumapp.com/records/" + record_id + "\">" + record_id + "</a><br>" +
                  "<b>Latitud y longitud: </b><a href=\"" + mapURL2 + "\">" + lat + ", " + lng + "</a><br>" +
                  "<b>Barrio/Comuna/Localidad/Sector: </b>" + neigh + "<br>" +                  
                  "<b>Dirección: </b>" + address + "<br>" +
                  "<b>Municipalidad: </b>" + municipality + "<br>" +
                  "<b>País: </b>" + country + "<br>" +
                  "<p><a href='https://web.fulcrumapp.com/records/" + record_id + "' title='Open in Fulcrum'><img src='" + mapURL + "'></a>" + "     " + "<img src='" + imageURL + "' height='300 alt='Imagén fachada vivienda'></p>" +
                "</p>";

      // Send email
        MailApp.sendEmail({
          to: "myemail.dev@gmail.com",
          subject: house_id + " - Acta de observación en vencimiento (3 días restantes)",
          htmlBody: body
        });
      }      
      sendMail_3();
    }
    else {
      return;
    }  
  }   
}

基本上,我是通过get.Values构建一个数组,然后检查:

  • 数组第五列的哪一行等于send_emailcheckValues[i][4] === 'send_email'),然后发送与第五列中等于send_email的单元格数量一样多的电子邮件数组,这要归功于功能sendMail_7
  • 数组中Sevent列的哪一行等于send_emailcheckValues[j][6] === 'send_email'),然后发送与第7列中等于send_email的单元格数量一样多的电子邮件数组,这要归功于功能sendMail_3

函数sendMail_7可以正常工作,但是我无法理解从for (var j in checkValues) {...开始的脚本第二部分的工作方式。

实际上,脚本的最后一个花括号用红色突出显示,所以我认为有问题,但是我不知道是什么。

我还尝试将两个发送邮件的功能sendMail_7sendMail_3放在主要功能warnStatusBeginDay之外。在这种情况下,主要功能warnStatusBeginDay的最后一个大括号为绿色,但是以此方式在电子邮件正文中定义的变量(即var name = checkValues[i][14]var municipality = checkValues[i][17])不起作用。

我不确定我的解释是否明确,但这是我第一次使用Google App脚本/ javascript,我是编码的初学者。

有什么建议吗?

非常感谢, 斯特凡诺

1 个答案:

答案 0 :(得分:1)

如果您的第一个j不满足library(data.table) df <- as.data.table(data.frame(seqnames,start,end,strand,block,cont_ID)) sp <- split(df,f = cont_ID) grouping <- function(x){ x[, block_lag:= shift(block, fill = "lag")] x[is.na(block_lag), cumsum_block_lag:=1] x[!is.na(block_lag), cumsum_block_lag:=1+cumsum(block!=(block_lag+1))] x[, index:=paste0(LETTERS[cumsum_block_lag],"_",cont_ID)] x[, cumsum_block_lag:=NULL] x[, block_lag:=NULL] } index_sp <- rbindlist(lapply(sp, grouping)) index_sp <- index_sp[order(block)] # seqnames start end strand block cont_ID index #1: H7 0 10 * 1 001 A_001 #2: H7 11 20 * 2 001 A_001 #3: H7 0 10 * 3 004 A_004 #4: H7 11 20 * 4 004 A_004 #5: H7 0 10 * 5 003 A_003 #6: H7 21 30 * 6 001 B_001 #7: H7 31 40 * 7 001 B_001 #8: H7 11 20 * 8 003 B_003 中的方程式,那么您会因为(checkValues[j][6] === 'send_email'而退出循环