2017-03-31 8 views
0

Googleフォームを使用してブックに情報を収集し、その情報を使用してカスタマイズされたメールを送信者に送り返しました。私はコーディングの騒ぎですが、私はこの仕事のために十分に一緒に手を貸しています。私の問題は、それが空の電子メールセルになると、それは停止するということです。私はそれをスキップさせるためにループに入れようとしましたが、成功しませんでした。どんな助けもありがとう。これはループなしで使用しているプログラムです。MailApp.SendEmailを使用して空のセルをスキップするにはどうすればよいですか?

// This constant is written in column C for rows for which an email 
// has been sent successfully. 
var EMAIL_SENT = "EMAIL_SENT"; 

function sendEmails2() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var startRow = 2; // First row of data to process 
    var numRows = 2000; // Number of rows to process 
    // Fetch the range of cells A2:B2001 
    var dataRange = sheet.getRange(startRow, 1, numRows, 2001) 
    // Fetch values for each row in the Range. 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 
    var emailAddress = row[0]; // First column 
    var message = row[1];  // Second column 
    var emailSent = row[2];  // Third column 
    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates 
     var subject = "Bounce Reply"; 
     MailApp.sendEmail(emailAddress, subject, message); 
     sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); 
     // Make sure the cell is updated right away in case the script is interrupted 
     SpreadsheetApp.flush(); 
     } 
    } 
    } 

答えて

0

string.match()関数を使用して、セルが空であるかどうかを判断できます。電子メールアドレスを探しているので、セルに「@」記号があるかどうかを確認することもできます。何かが空であるかであるかどうかを確認するには

var EMAIL_SENT = "EMAIL_SENT"; 
function sendEmails2() { 
    var sheet = SpreadsheetApp.getActiveSheet(); 
    var startRow = 2; // First row of data to process 
    var numRows = 2000; // Number of rows to process 
    // Fetch the range of cells A2:B2001 
    var dataRange = sheet.getRange(startRow, 1, numRows, 2001) 
    // Fetch values for each row in the Range. 
    var data = dataRange.getValues(); 
    for (var i = 0; i < data.length; ++i) { 
    var row = data[i]; 
    var emailAddress = row[0] // First column 
    if (emailAddress.match('@') === null){ 
     continue; // skip this iteration of the loop and go to the next one 
    }; 
    var message = row[1];  // Second column 
    var emailSent = row[2];  // Third column 
    if (emailSent != EMAIL_SENT) { // Prevents sending duplicates 
     var subject = "Bounce Reply"; 
     MailApp.sendEmail(emailAddress, subject, message); 
     sheet.getRange(startRow + i, 3).setValue(EMAIL_SENT); 
     // Make sure the cell is updated right away in case the script is interrupted 
     SpreadsheetApp.flush(); 
     } 
    } 
    } 

編集:いない場合は、細胞を除外し、そのような次のセルに進み:

if (emailAddress.match('@') === null){ 
continue; // skip this iteration of the loop and go to the next one 
} 

あなたの最終的なコードは次のようになりますこのようなことを試すことはできません。

if (message.match(/[a-zA-Z0-9_]/) === null) { //" /[a-zA-Z0-9_]/ " is regex to match any and every alphanumeric letter including underscore 
continue; // skip this iteration of the loop and go to the next one 
} 

あなたは機能助けmatch and regex here.

希望に関する詳細を見つけることができます!

+0

ありがとうございました!それはまさに私が必要としたものでした。それはあなたがそれを指摘した後、そのような明らかな修正のように思えます。 – Cyborgninja

+0

ありがとう!それはまさに私が必要としたものでした。それはあなたがそれを指摘した後、そのような明らかな修正のように思えます。電子メールが空白の場合、またはメッセージが空白の場合は、これをスキップするためにこれを使用する方法はありますか? '||を使うのと同じですmessage.match()=== null 'です。私はそれが私に未定義の条件と一致させることができないことを知っているので、どうすればいいのですか? – Cyborgninja

+0

未定義の状態ですか?それはどういう意味ですか? –

関連する問題