【问题标题】:Is there a way to get some desired range of a table to send email based on the value of a column?有没有办法根据列的值来获取某个表的所需范围以发送电子邮件?
【发布时间】:2019-04-24 03:06:11
【问题描述】:

我的部分日常工作是向供应商发送一系列 google sheet 单元格,以索取材料样品以制作背包。

我编写了一些非常有用的脚本。但是有了这个,我真的不知道“显示一些尝试过的代码”。请看我下面的描述。

  1. 我希望有一个脚本,该脚本通过列供应商和状态来决定哪个供应商通过来自 [ 项目名称 (i, 1) 的信息向 MAIL(i,6) 发送电子邮件:UNIT(i ,5) ]

  2. 我希望只向 STATUS 值 = false(未选中)的行发送电子邮件,并且在发送电子邮件后,我会让脚本将单元格值从 false → true 更改(所以下次如果我再次运行脚本,它不会将信息复制到接收者)

  3. 对我来说困难的部分是我不知道如何通过供应商名称收集信息。因此,使用上面的 google 表格,我很想按顺序发送 3 封电子邮件:

    一个。供应商C@gmail.com(一星)→发送第21行+第24行+第26行(仅A列到E+然后检查单元格)

    b.供应商B@gmail.com(YKK)→发送第22行+第25行(仅A列到E+然后检查单元格)

    c。供应商A@gmail.com (DUCKSAN) → 仅发送第 27 行(仅 A 列到 E + 然后检查单元格)(因为第 23 行已选中 - 表示我已经发送或我现在不想发送)

照片:

I put pictures in Google Photo here to show you guys:

表格

一个

b

c


问题更新 根据我对这个问题的第一个信息,以下 Tedinoz 的代码对我很有用。

但是如果在“HTS”表中,电子邮件列被材料代码列替换,然后我将在“开发”表中包含所有供应商信息,我每天使用它来监控所有品牌或通过创建仅用于监控供应商的表格(请就此提出建议)。请再次检查stackoverflow电子表格,我包含了“开发”表以供参考(向下滚动到供应商信息行开始的第42行)


【问题讨论】:

  • 欢迎。请分享您的电子表格,或制作一份您可以分享的副本。仅查看表格的快照是不够的,我们需要查看整个电子表格(例如,第 18 行以上的行等)。我建议你阅读How do I ask a good question?How to create a Minimal, Complete, and Verifiable example
  • docs.google.com/spreadsheets/d/… 请检查我的电子表格,它包含 2 张表格:1/ 示例:以上信息 2/ HTS:通常是我使用的表格类型(我经常按在我发送之前命名,但我提出上述问题,因为我认为如果我忘记排序,那么一切都不会出错)
  • 我了解为什么您在发送前按名称对“HTS”进行排序。您如何“取消排序”回到原始状态?
  • 我不明白你为什么问我关于“unsort”的问题。 “示例”是我认为非常适合该问题的示例。 “HTS”是我在向供应商发送电子邮件之前总是先排序的订购表类型,以避免丢失信息。
  • 请参阅 Simple Mail Merge tutorial 获取 Google Apps 脚本作为您正在寻找的一个很好的例子。

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


【解决方案1】:

在某种程度上,OP 的场景是独一无二的,因为它需要批量发送电子邮件给供应商,并为与每个供应商相关的项目编译 HTML 电子邮件(基于复选框的值。

这段代码:

  • 从产品组工作表中获取数据(工作表名称是一个变量,因此代码可以进一步自动化),
  • 创建供应商临时列表,
  • 遍历供应商,一次一个
    • 循环遍历数据并捕获供应商名称匹配的任何项目并且复选框未选中 (false)。
    • 供应商商品数据被逐步写入数组,完成后数组被写入临时工作表(尽管可能可以进一步微调)
    • 从临时工作表数据创建 html 消息。
    • 邮件使用 Gmail.sendEmail 发送给供应商。
    • 从临时输出表中清除临时 Take 信息
  • 将未选中的复选框重新设置为“已选中”

function so5582181508() {

  //setup spreadsheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var datasheetname = "HTS";
  var datasheet = ss.getSheetByName(datasheetname);
  var messagesheetname = "MessageOutput";
  var messagesheet = ss.getSheetByName(messagesheetname);
  var templatesheetname = "Email Template";
  var templatesheet = ss.getSheetByName(templatesheetname);  
  messagesheet.clear();

  // get the number of rows on the data sheet
  var Avals = datasheet.getRange("A1:A").getValues();
  var Alast = Avals.filter(String).length;
  //Logger.log("Alast = "+Alast);
  var htslast = datasheet.getLastRow();
  //Logger.log("htslast = "+htslast);



  // get the supplier column
  var supplierRange = datasheet.getRange(3,7,Alast-2,1);
  //Logger.log("the supplier range  = "+supplierRange.getA1Notation());
  //get the supplier data
  var supplierData = supplierRange.getValues();

  //get the status column
  var statusRange = datasheet.getRange(3,9,Alast-2,1);
  //Logger.log("the status range  = "+statusRange.getA1Notation());
  // get the status data
  var statusData = statusRange.getValues();

  var transCount = supplierData.length;
  var supplierList = [];
  var transData = datasheet.getDataRange().getValues();

  // supplierList contains the unique supplier list
  supplierData.forEach(function(x){
    if(supplierList.indexOf(x[0]) === -1 && x[0]!="" ){
        supplierList.push(x[0]);
    }                   
  });
  var supplierCount = supplierList.length;


  var itemCount = 0;
  var mailMessage = [];
  var mailItem = [];

  //build the mail item header
  var mailItemHeader = [];
  mailItemHeader.push(transData[0][0]);
  mailItemHeader.push(transData[0][1]);
  mailItemHeader.push(transData[0][2]);
  mailItemHeader.push(transData[0][3]);
  mailItemHeader.push(transData[0][4]);
  //mailItemHeader.push(transData[0][6]);

  //Logger.log("length of new array = "+supplierCount);
  //Logger.log("Number of items in table = "+transCount);


  // loop through the data, once for every supplier
  for (supplier = 0; supplier<supplierCount; supplier++){
    mailMessage=[];
    itemCount = 0;
    //Logger.log("supplier = "+supplier);
    //Logger.log("supplier = "+supplierList[supplier]);

    // now loop through the data
    // start i = 2 to allow for header
    for (var i = 2; i < transCount+2; i++) {
      mailItem=[];
      //Logger.log("i = "+i+", SupplierList: "+supplierList[supplier]+", supplier: "+transData[i][6]+", status:"+transData[i][8])

      // the suplier matches and if the checkbox is false
      if (supplierList[supplier] == transData[i][6] && transData[i][8] == false){

        // this this is the first item then push the mail header 
        if (itemCount ==0){
          mailMessage.push(mailItemHeader);
          // get the email address
          var emailAddress = transData[i][5];
          var subject = "Purchase order";

        }

        // this is a match
        var emailAddress = transData[i][5];
        //Logger.log("send email to "+supplierList[supplier]+", at "+transData[i][5]);
        //Logger.log("Item: "+transData[i][0]+", Spec: "+transData[i][1]+", color: "+transData[i][3]+", quantity: "+transData[i][4]+", Unit: "+transData[i][5]);

        // push the transation values for this row onto the mailitem array
        mailItem.push(transData[i][0]);
        mailItem.push(transData[i][1]);
        mailItem.push(transData[i][2]);
        mailItem.push(transData[i][3]);
        mailItem.push(transData[i][4]);
        //mailItem.push(transData[i][6]);

        // push the row onto the rest of the mail message data
        mailMessage.push(mailItem);
        itemCount=itemCount+1

        //update the status value to true
        statusData[i-2] = [true];
      }
      else
      {
      //Logger.log("no match");
      }

    } // end of the transaction loop for this supplier

    // define the temporary output range
    var messageRange = messagesheet.getRange(1, 1, mailMessage.length, 5);
    // paste the items details to the temporary output range
    var messageupdate = messageRange.setValues(mailMessage);
    // get the values for the items only (no header)
    var messagedata = messagesheet.getRange(2, 1, mailMessage.length-1, 5).getValues();
    //Logger.log("ROW#1 col1="+messagedata[0][0]+", column 2: "+messagedata[0][1]);
    //Logger.log("ROW#1 col1="+messagedata[1][0]+", column 2: "+messagedata[1][1]);
    //Logger.log("message data length"+messagedata.length);  
    var messageitemcount = messagedata.length;
    //Logger.log("send email to "+supplierList[supplier]+", at "+emailAddress+", message: "+mailMessage);

    // create a subject
    var emailSubject = "Purchase Order: StackOverflow Test";
    // get the email address
    var emailaddress = emailAddress;

    // message
    var messagePrefix = "Attention: "+supplierList[supplier];

    // start the build of the html message
    var columns = 5;
    var columncount=1;
    var message = 'Please supply the following products:<br><br><table style="border-collapse:collapse;" border = 1 cellpadding = 5>';
    // get the headers
    for (h=0; h<columns;h++){

      if (columncount ==1){
        var header = '<tr>';
      }

      header+='<th style="background-color:#ffeb3b">'+mailItemHeader[h]+'</th>';

      if (columncount ==5){
        header+='</tr>';
      }
      columncount=columncount+1
    }
    //Logger.log("header:"+header);

    // add the header to the mesage
    message+=header;

    // loop through the items on the temporary output and get the item values
    for(c=0;c<messageitemcount;c++){

      // increment message
      message+='<tr><td>'+messagedata[c][0]+'</td>'+'<td>'+messagedata[c][1]+'</td>'+'<td>'+messagedata[c][2]+'</td>'+'<td>'+messagedata[c][3]+'</td>'+'<td>'+messagedata[c][4]+'</td></tr>';

    }

    // finalise the message
    message+='</table>';  
    // Logger.log("DEBUG: message: "+message);//DEBUG

    // send the email
    GmailApp.sendEmail(emailaddress, emailSubject,  messagePrefix, {htmlBody: message,  });

    // clear the state from the temporary outsheet sheet
    messagesheet.clear();

  }
  //update the status range - return all to ticked (true)
  statusRange.setValues(statusData);

}

【讨论】:

  • 嗨泰德,我试过你的代码,效果很好。但是如果在“HTS”表中,电子邮件列被材料代码列替换,然后我将在“开发”表中包含所有供应商信息,我每天使用它来监控所有品牌,或者只是创建一个表监控供应商(请就此提出建议)。请再次检查 stackoverflow 电子表格,我包含了“开发”表以供参考(向下滚动到供应商信息行开始的第 42 行)
猜你喜欢
  • 1970-01-01
  • 2022-07-26
  • 1970-01-01
  • 2013-10-19
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多