我的日常工作的一部分是将一系列Google表格单元格发送给供应商,以要求提供材料样本来制作背包。
我写了一些非常有用的脚本。但是有了这个,我真的不知道“显示一些尝试过的代码”。请在下面查看我的描述。
我希望有一个脚本可以通过VENDOR和STATUS列运行,以根据[项目名称(i,1):UNIT(i) ,5)]
我只想将电子邮件发送到STATUS值= false(未选中)的行,并且在发送电子邮件后,我将让脚本将单元格值从false→true更改(因此,下次再次运行该脚本,它不会将信息复制到接收方)
对我来说,最难的部分是我不知道如何通过VENDOR名称收集信息。因此,使用上面的Google工作表表格,我希望按订单发送3封电子邮件:
a。 SupplierC@gmail.com(一星)→发送第21 + row24 + row26行(仅从A到E +列,然后检查单元格)
b。 SupplierB@gmail.com(YKK)→发送第22 + row25行(仅将A列发送至E +,然后检查单元格)
c。 SupplierA@gmail.com(DUCKSAN)→仅发送第27行(仅将A列发送至E +然后检查该单元格)(因为第23行已选中-表示我已经发送或我现在不想发送)
照片:
I put pictures in Google Photo here to show you guys:
表格:
a :
b :
c :
问题更新 有了我第一个问题的信息,Tedinoz的以下代码对我来说非常有用。
但是,如果在“ HTS”工作表中,“电子邮件”列替换为“材料代码”列,然后我将在“ dev”工作表中包含所有供应商信息,我将使用这些信息每天监控所有品牌或通过创建一张用于监视供应商的表(请对此提供建议)。请再次检查stackoverflow电子表格,我提供了“ dev”表作为参考(向下滚动至第42行,其中供应商信息行开始)
答案 0 :(得分:0)
在某种程度上,OP的情况是独特的,因为它需要将电子邮件分批发送给供应商,并为与每个供应商有关的项目编译HTML电子邮件(基于复选框的值。
此代码:
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);
}