需要幫助...我有一個腳本,它基本上向不同的收件人發送多封電子郵件(報告)。但是,當收件人的電子郵件地址出錯時,腳本會停止(例如,如果沒有提供電子郵件地址或收件人的電子郵件地址無效)。
我需要幫助來放置一行代碼,告訴腳本跳過有錯誤的那個并繼續到下一個收件人。
我將不勝感激提供的任何指導。尊敬的,卡里姆
這是下面的代碼...
function emailALL() {
var ui = SpreadsheetApp.getUi();
var response = ui.alert('Confirm', 'Are you sure you want to email all the reports for this
class?', ui.ButtonSet.YES_NO);
if (response == ui.Button.YES) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const studentTerm = ss.getSheetByName("Student Report");
const sheetId = studentTerm.getSheetId();
const sheet = ss.getSheetByName("Marks Master");
const students = sheet.getRange("Ao2:Ao41").getValues();
var loopCount= sheet.getRange("Ao1").getValues();
var url_base = "https://docs.google.com/spreadsheets/d/" ss.getId() "/";
let url = ss.getUrl();
url = '#gid=';
url = sheetId;
Logger.log("Url: ", url);
ss.setActiveSheet(studentTerm);
for(var i=0; i<loopCount 1; i ){
ss.getRange('C7').setValue(students[i]);
// const sheet = ss.getRange(1, 1, 46, 16);
// Subject of the email message
const words = ss.getRange('C7');
const title = words.getValues()[0];
const term = ss.getRange("D9");
const terms = term.getValues()[0];
const email = ss.getRange('c3').getValues()[0];
const school = ss.getRange('C1').getValues()[0];
const subject = school ": " title "_Term_" terms " Report";
const motto = ss.getRange('C2').getValues()[0];
const body = "Good day Parent/Guardian, \n\nPlease find attached, school term report for " title "." "\n\nRegards, \n" school "\n" motto "\n";
//"Good day Parent/Guardian," & vbLf & vbLf _
// & "Please find attached, " & Title & "'s School Term Report." & vbLf & vbLf _
// & "Regards," & vbLf _
//& School & vbLf _
// & Motto & vbLf
// Email Text. You can add HTML code here - see ctrlq.org/html-mail
// let sheet = ss.getSheetName("Student-Term");
// const unformattedUrl = studentTerm.getUrl();
// Logger.log("SpreadSheet Url " unformattedUrl);
// let formattedUrl = unformattedUrl.split("/");
// formattedUrl = formattedUrl.slice(0, formattedUrl.length - 1);
// formattedUrl = formattedUrl.join("/");
// formattedUrl = formattedUrl "/export?";
const exportOptions ='export?exportFormat=pdf&format=pdf' //export as pdf
// Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
(sheetId ? ('&gid=' sheetId) : ('&id=' spreadsheetId))
// following parameters are optional...
'&size=letter' // paper size
'&portrait=true' // orientation, false for landscape
'&fitw=false' // fit to width, false for actual size
'&sheetnames=false&printtitle=false&pagenumbers=true' //hide optional headers and footers
'&gridlines=false' // hide gridlines
'&fzr=false'; // do not repeat row headers (frozen rows) on each page
var params = {method:"GET",headers:{"authorization":"Bearer " ScriptApp.getOAuthToken()}};
Logger.log(url exportOptions);
var response = UrlFetchApp.fetch(url_base exportOptions, params);
var blob = response.getBlob().setName(title '.pdf')
var mailOptions = {
attachments:blob
}
//var pdfFile = ss.getBlob().getAs('application/pdf').setName("Pdf1");
//// Send the PDF file as an attachement
GmailApp.sendEmail(email, subject, body, mailOptions);
Utilities.sleep(1000)
}
}
else {}
}
uj5u.com熱心網友回復:
后
const email = ss.getRange('c3').getValues()[0];
添加
if(email === '') continue;
以上將處理用于輸入電子郵件的單元格為空白(空)的情況。處理所有其他情況的一種簡單方法是使用 try .. catch
代替
GmailApp.sendEmail(email, subject, body, mailOptions);
經過
try {
GmailApp.sendEmail(email, subject, body, mailOptions);
} catch(error) {
console.log(error.message, error.stack);
continue;
}
如需更復雜的方法,請查看在 JavaScript 中驗證電子郵件地址的最佳方法是什么?
參考
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch
有關的
-
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446218.html
標籤:javascript 谷歌应用脚本 例外 错误处理
