我是剧本新手。我想发送基于多个单元格值的电子邮件。电子邮件正在运行,但有时我只发送1个PO,所以我当前的代码将发送PO1 + 9空行,并且电子邮件现在超长。
我认为我需要创建一个IF语句,但是不确定从哪里开始。
function mailtest() {
var TO = SpreadsheetApp.getActiveSheet().getRange('N3').getValue();
var CC = SpreadsheetApp.getActiveSheet().getRange('N5').getValue();
var SUBJECT = SpreadsheetApp.getActiveSheet().getRange('L9').getValue();
var TEXT = SpreadsheetApp.getActiveSheet().getRange('L11').getValue();
var TEXT2 = SpreadsheetApp.getActiveSheet().getRange('L13').getValue();
var PO1 = SpreadsheetApp.getActiveSheet().getRange('L15').getValue();
var PO2 = SpreadsheetApp.getActiveSheet().getRange('L16').getValue();
var PO3 = SpreadsheetApp.getActiveSheet().getRange('L17').getValue();
var PO4 = SpreadsheetApp.getActiveSheet().getRange('L18').getValue();
var PO5 = SpreadsheetApp.getActiveSheet().getRange('L19').getValue();
var PO6 = SpreadsheetApp.getActiveSheet().getRange('L20').getValues();
var PO7 = SpreadsheetApp.getActiveSheet().getRange('L21').getValues();
var PO8 = SpreadsheetApp.getActiveSheet().getRange('L22').getValues();
var PO9 = SpreadsheetApp.getActiveSheet().getRange('L23').getValues();
var P10 = SpreadsheetApp.getActiveSheet().getRange('L24').getValues();
var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
MailApp.sendEmail({
to: TO,
subject: SUBJECT,
cc: CC,
htmlBody:
TEXT+"<br><br>"+
TEXT2+"<br><br>"+
//here is the problem ? I dont want PO3&4&5&6 if I only have 2 PO.
PO1+"<br>"+
PO2+"<br>"+
PO3+"<br>"+
PO4+"<br>"+
PO5+"<br>"+
PO6+"<br>"+
PO7+"<br>"+
PO8+"<br>"+
PO9+"<br>"+
P10+"<br><br>"+
signature
});
}
答案 0 :(得分:2)
仅当变量具有值时,您才希望在电子邮件中包含变量。 以下代码在几个方面与您的代码有所不同。
sheet.getRange(15,12,10,1).getValues();
这样可以评估内容,而无需复杂的变量处理for (var i=0; i<PO.length;i++){
和if ( if (POValue){){
POText = POText+POValue+"<br>"
);否则,该采购订单不包含在电子邮件文本中。function so58564238() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "58564238";
var sheet = ss.getSheetByName(sheetname);
var POText = "";
var PO = sheet.getRange(15,12,10,1).getValues();
for (var i=0; i<PO.length;i++){
var POValue = PO[i][0];
if (POValue){
POText = POText+POValue+"<br>"
}
}
Logger.log(POText)
}
合并mailtest
function mailtest() {
var TO = SpreadsheetApp.getActiveSheet().getRange('N3').getValue();
var CC = SpreadsheetApp.getActiveSheet().getRange('N5').getValue();
var SUBJECT = SpreadsheetApp.getActiveSheet().getRange('L9').getValue();
var TEXT = SpreadsheetApp.getActiveSheet().getRange('L11').getValue();
var TEXT2 = SpreadsheetApp.getActiveSheet().getRange('L13').getValue();
var POText = "";
var PO = sheet.getRange(15,12,10,1).getValues();
for (var i=0; i<PO.length;i++){
var POValue = PO[i][0]
if (POValue){
POText = POText+POValue+"<br>"
}
}
var signature = Gmail.Users.Settings.SendAs.list("me").sendAs.filter(function(account){if(account.isDefault){return true}})[0].signature;
MailApp.sendEmail({
to: TO,
subject: SUBJECT,
cc: CC,
htmlBody:
TEXT+"<br><br>"+
TEXT2+"<br><br>"+
POText+"<br>"+
signature
});
}