![](/img/trans.png)
[英]How to skip a UrlFetchApp when the url is empty and still run the script
[英]Is there a way to skip empty, blank or Value! cells when using urlfetchapp and or in my datarange?? or some other solution to my problem?
下面的這段代碼是從 web 中逐部分復制粘貼的,它確實有效,在一些幫助下,我能夠在一定程度上實現我的這個項目的一個目標。 問題是這樣的:我有一個帶有數據的谷歌表,我正在使用 UrlFetchApp 將圖像附加到電子郵件中,但是當我的腳本運行為空、空白或值時。 細胞停止運行並破裂? 有什么辦法可以防止這種情況。 我是一個完全的新手,不知道。 我在網上搜索並確實找到了此信息... 如何使用 MailApp.SendEmail 跳過空白單元格? 我試圖將它應用於我的情況,但無法。
if (emailAddress.match('@') === null){
continue; // skip this iteration of the loop and go to the next one
};
我嘗試將其更改為我的項目情況...
if (image01.match('jpg') === null){
continue; // skip this iteration of the loop and go to the next one
};
但不幸的是,'image01.match' 不是 function(我知道你們所有的編碼人員現在都在笑)所以我又嘗試了一些東西,但也沒有用....
if ((row[3]).match('jpg') === null){
continue; // skip this iteration of the loop and go to the next one
};
所以這里是完整的代碼......非常感謝任何幫助或建議,謝謝!
function emailImage(){
var EMAIL_SENT = "EMAIL_SENT";
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow();
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 24)
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[0]; // First column
var subject = row[1]; // Second column
var message = row[2]; // Third column
var image01 = UrlFetchApp.fetch(row[3]).getBlob();
var image02 = UrlFetchApp.fetch(row[4]).getBlob();
var image03 = UrlFetchApp.fetch(row[5]).getBlob();
var emailSent = row[23]; //
if (emailSent != EMAIL_SENT) { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message, {attachments: [image01, image02, image03]});
sheet.getRange(startRow + i, 24).setValue(EMAIL_SENT);
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
嘗試這個:
function emailImage() {
const sh = SpreadsheetApp.getActiveSheet();
const sr = 2;
const rg = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 24);//your previous code was not calculating the number of rows properly so you were trying run the code in rows that probably did not have any data.
const data = rg.getValues();
for (let i = 0; i < data.length; ++i) {
let row = data[i];
let emailAddress = row[0]; // First column
let subject = row[1]; // Second column
let message = row[2]; // Third column
let image01 = UrlFetchApp.fetch(row[3]).getBlob();
let image02 = UrlFetchApp.fetch(row[4]).getBlob();
let image03 = UrlFetchApp.fetch(row[5]).getBlob();
let emailSent = row[23];
if (emailAddress && subject && message && image01 && image02 && image03 && emailSent != "EMAIL_SENT") { // Prevents sending duplicates and emails without all of the data being present
MailApp.sendEmail(emailAddress, subject, message, { attachments: [image01, image02, image03] });
sh.getRange(sr + i, 24).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
試試這種方式:
function emailImage() {
const sh = SpreadsheetApp.getActiveSheet();
const sr = 2;
const rg = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 24)
const data = rg.getValues();
for (let i = 0; i < data.length; ++i) {
let row = data[i];
let emailAddress = row[0]; // First column
let subject = row[1]; // Second column
let message = row[2]; // Third column
let options ={attachments:[]};
let image01 = UrlFetchApp.fetch(row[3]).getBlob();
if(image01)options.attachments.push(image01);
let image02 = UrlFetchApp.fetch(row[4]).getBlob();
if(image02)options.attachments.push(image02);
let image03 = UrlFetchApp.fetch(row[5]).getBlob();
if(image03)options.attachments.push(image03);
let emailSent = row[23];
if (emailAddress && subject && message && emailSent != "EMAIL_SENT") { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message, options);
sh.getRange(sr + i, 24).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
你也可以這樣嘗試:
function emailImage() {
const sh = SpreadsheetApp.getActiveSheet();
const sr = 2;
const rg = sh.getRange(sr, 1, sh.getLastRow() - sr + 1, 24)
const data = rg.getValues();
for (let i = 0; i < data.length; ++i) {
let row = data[i];
let emailAddress = row[0]; // First column
let subject = row[1]; // Second column
let message = row[2]; // Third column
let options ={attachments:[]};
let urls=[row[3],row[4],row[5]].filter(e => e != '');
前一行刪除了空白的 url,下一行只推送返回非 null 的圖像 blob。 因此,您可以使用盡可能多的圖像來保存列,或者您可以將所有 url 放入一個單元格中,並用說 control enter 將它們分開,在它們之間放置 '\n' 然后你可以使用。 類似於 row[3].toString().split('\n').filter(e=>e)...等
urls.forEach(u=>{let img = UrlFetchApp.fetch(u).getBlob();if(img)options.attachments.push(img);});
let emailSent = row[23];
if (emailAddress && subject && message && emailSent != "EMAIL_SENT") { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message, options);
sh.getRange(sr + i, 24).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
一般來說,我最好的代碼在我通過幾次迭代 go 之前不會向我展示。
blob
是 object 類型,您不能直接.match()
它。
相反,像這樣使用try-catch
:
try{
var image01 = UrlFetchApp.fetch(row[3].toString()).getBlob();
console.log(image01.getContentType()); //should be image/png.
console.log(image01); //should be {}
}
catch(Exception)
{
console.log("Link error!");
}
如果鏈接中有任何錯誤, catch
會處理它。 否則,您可以在try
部分發送郵件。
希望這會幫助你。 如果這不是你想要的,那么我道歉。
這似乎有效..多種努力的結合。 謝謝 MetaMan.. 我要先測試一下....
function emailImage() {
var EMAIL_SENT = "EMAIL_SENT";
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2;
var numRows = sheet.getLastRow();
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 24)
var data = dataRange.getValues();
for (let i = 0; i < data.length; ++i) {
let row = data[i];
let emailAddress = row[0]; // First column
let subject = row[1]; // Second column
let message = row[2]; // Third column
let options ={attachments:[]};
let urls=[row[3],row[4],row[5]].filter(e => e != '');
urls.forEach(u=>{let img = UrlFetchApp.fetch(u).getBlob();if(img)options.attachments.push(img);});
let emailSent = row[23];
if (emailAddress && subject && message && emailSent != "EMAIL_SENT") { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message, options);
sh.getRange(sr + i, 24).setValue(EMAIL_SENT);
SpreadsheetApp.flush();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.