如何让我的 Google 应用脚本跳过错误并转到下一项
How to get my Google App Script to skip an error and go to the next item
需要帮助...
我有一个脚本,基本上可以向不同的收件人发送多封电子邮件(报告)。但是,当收件人的电子邮件地址存在错误时,脚本会停止(例如,如果没有提供电子邮件地址或收件人的电子邮件地址无效)。
我需要帮助来放置一行代码,告诉脚本跳过有错误的代码并继续到下一个收件人。
如果能提供任何指导,我将不胜感激。
尊敬的卡里姆
这是下面的代码...
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 {}
}
之后
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;
}
要获得更复杂的方法,请查看 What's the best way to validate an email address in 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
相关
-
需要帮助... 我有一个脚本,基本上可以向不同的收件人发送多封电子邮件(报告)。但是,当收件人的电子邮件地址存在错误时,脚本会停止(例如,如果没有提供电子邮件地址或收件人的电子邮件地址无效)。
我需要帮助来放置一行代码,告诉脚本跳过有错误的代码并继续到下一个收件人。
如果能提供任何指导,我将不胜感激。 尊敬的卡里姆
这是下面的代码...
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 {}
}
之后
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;
}
要获得更复杂的方法,请查看 What's the best way to validate an email address in 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
相关
-