2012-05-21 31 views
0

我正在使用下面我從現有sendEmail腳本修改的腳本。我正在收集Google表單中的信息,並希望通過每個「提交」發送電子郵件。我遇到的問題是腳本發送到電子表格的每一行,即使該信息已經發送。我需要腳本以某種方式標記發送的行,以便信息不會被重新發送。我的「新手」方式是在發送後將電子郵件地址更改爲我的電子郵件地址。我過濾了我的收件箱以自動刪除這些電子郵件。這是我粗略的解決方案,但我希望它能正常工作。有什麼建議嗎?需要一個腳本來發送電子郵件,而不會重新發送到以前記錄的發件人

var EMAIL_SENT = "EMAIL_SENT"; 

    function sendEmails() { 
     var ss = SpreadsheetApp.getActiveSpreadsheet(); 
     var dataSheet = ss.getSheets()[0]; 
     var dataRange = dataSheet.getRange(2, 1, dataSheet.getMaxRows() - 1, 17); 

     var templateSheet = ss.getSheets()[1]; 
     var emailTemplate = templateSheet.getRange("A1").getValue(); 

    // Create one JavaScript object per row of data. 
    objects = getRowsData(dataSheet, dataRange); 

    // For every row object, create a personalized email from a template and send 
    // it to the appropriate person. 
    for (var i = 0; i < objects.length; ++i) { 
    // Get a row object 
     var rowData = objects[i]; 

    // Generate a personalized email. 
    // Given a template string, replace markers (for instance ${"First Name"}) with 
    // the corresponding value in a row object (for instance rowData.firstName). 
     var emailText = fillInTemplateFromObject(emailTemplate, rowData); 
     var emailSent = rowData[4]; 
     if (emailSent != EMAIL_SENT) { 
      var emailSubject = "CCSS Walkthrough"; 
      MailApp.sendEmail(rowData.emailAddress, emailSubject, emailText); 
      var startRow = 2; 
      var sheet = SpreadsheetApp.getActiveSheet(); 
      sheet.getRange(startRow + i, 2).setValue("myemailaddress"); 
      SpreadsheetApp.flush(); 
    } 
    } 
} 


// Replaces markers in a template string with values define in a JavaScript data object. 
// Arguments: 
// - template: string containing markers, for instance ${"Column name"} 
// - data: JavaScript object with values to that will replace markers. For instance 
//   data.columnName will replace marker ${"Column name"} 
// Returns a string without markers. If no data is found to replace a marker, it is 
// simply removed. 
    function fillInTemplateFromObject(template, data) { 
     var email = template; 
    // Search for all the variables to be replaced, for instance ${"Column name"} 
     var templateVars = template.match(/\${\"[^\"]+\"\}/g); 

    // Replace variables from the template with the actual values from the data object. 
    // If no value is available, replace with the empty string. 
     for (var i = 0; i < templateVars.length; ++i) { 
    // normalizeHeader ignores ${"} so we can call it directly here. 
     var variableData = data[normalizeHeader(templateVars[i])]; 
     email = email.replace(templateVars[i], variableData || ""); 
    } 

     return email; 
} 





////////////////////////////////////////////////////////////////////////////////////////// 
// 
// The code below is reused from the 'Reading Spreadsheet data using JavaScript Objects' 
// tutorial. 
// 
////////////////////////////////////////////////////////////////////////////////////////// 

// getRowsData iterates row by row in the input range and returns an array of objects. 
// Each object contains all the data for a given row, indexed by its normalized column name. 
// Arguments: 
// - sheet: the sheet object that contains the data to be processed 
// - range: the exact range of cells where the data is stored 
// - columnHeadersRowIndex: specifies the row number where the column names are stored. 
//  This argument is optional and it defaults to the row immediately above range; 
// Returns an Array of objects. 
    function getRowsData(sheet, range, columnHeadersRowIndex) { 
     columnHeadersRowIndex = columnHeadersRowIndex || range.getRowIndex() - 1; 
     var numColumns = range.getEndColumn() - range.getColumn() + 1; 
     var headersRange = sheet.getRange(columnHeadersRowIndex, range.getColumn(), 1,  numColumns); 
     var headers = headersRange.getValues()[0]; 
     return getObjects(range.getValues(), normalizeHeaders(headers)); 
} 

// For every row of data in data, generates an object that contains the data. Names of 
// object fields are defined in keys. 
// Arguments: 
// - data: JavaScript 2d array 
// - keys: Array of Strings that define the property names for the objects to create 
    function getObjects(data, keys) { 
     var objects = []; 
     for (var i = 0; i < data.length; ++i) { 
     var object = {}; 
     var hasData = false; 
     for (var j = 0; j < data[i].length; ++j) { 
      var cellData = data[i][j]; 
      if (isCellEmpty(cellData)) { 
      continue; 
     } 
     object[keys[j]] = cellData; 
     hasData = true; 
    } 
    if (hasData) { 
     objects.push(object); 
    } 
    } 
    return objects; 
} 

// Returns an Array of normalized Strings. 
// Arguments: 
// - headers: Array of Strings to normalize 
    function normalizeHeaders(headers) { 
     var keys = []; 
     for (var i = 0; i < headers.length; ++i) { 
     var key = normalizeHeader(headers[i]); 
     if (key.length > 0) { 
      keys.push(key); 
    } 
    } 
    return keys; 
} 

// Normalizes a string, by removing all alphanumeric characters and using mixed case 
// to separate words. The output will always start with a lower case letter. 
// This function is designed to produce JavaScript object property names. 
// Arguments: 
// - header: string to normalize 
// Examples: 
// "First Name" -> "firstName" 
// "Market Cap (millions) -> "marketCapMillions 
// "1 number at the beginning is ignored" -> "numberAtTheBeginningIsIgnored" 
    function normalizeHeader(header) { 
     var key = ""; 
     var upperCase = false; 
     for (var i = 0; i < header.length; ++i) { 
     var letter = header[i]; 
     if (letter == " " && key.length > 0) { 
      upperCase = true; 
      continue; 
    } 
     if (!isAlnum(letter)) { 
      continue; 
    } 
     if (key.length == 0 && isDigit(letter)) { 
      continue; // first character must be a letter 
    } 
     if (upperCase) { 
      upperCase = false; 
      key += letter.toUpperCase(); 
    }  else { 
      key += letter.toLowerCase(); 
    } 
    } 
    return key; 
} 

// Returns true if the cell where cellData was read from is empty. 
// Arguments: 
// - cellData: string 
    function isCellEmpty(cellData) { 
     return typeof(cellData) == "string" && cellData == ""; 
} 

// Returns true if the character char is alphabetical, false otherwise. 
    function isAlnum(char) { 
     return char >= 'A' && char <= 'Z' || 
     char >= 'a' && char <= 'z' || 
     isDigit(char); 
} 

// Returns true if the character char is a digit, false otherwise. 
    function isDigit(char) { 
     return char >= '0' && char <= '9'; 
} 

回答

1

最好的方法是添加一列並將其標記爲email_sent,然後在發送時將其過濾掉。

Section 2this tutorial完全正是你想要完成的。

此方法允許您保留提交表單的人員的電子郵件地址。

1

因爲你想要做的是:「我正在從Google表單收集信息並希望每個」提交「都發送電子郵件」你甚至不需要訪問電子表格來發送您想要發送的信息。電子表格實際上會存儲表單提交隨附的信息,但您不必返回電子表格即可在提交時將數據發送至電子郵件。您可以編寫一個在表單提交時觸發的函數,該函數將以表單提交作爲事件並從那裏獲取相關數據並將其發送到所需的電子郵件地址。我在這裏包含一個演示代碼,查看更多詳細信息的評論。

function onFormSubmit(event) { //On form submission send an email to the approver. 
var sheet = SpreadsheetApp.getActiveSheet(); 
    //Get values that have to be put on the email. 
    //event.values[index] is indexed in the same order that form values are ordered  on the spreadhseet 
var formVal1 = event.values[0]; 
var formVal2 = event.values[1]; 
. 
. 
. 
. 
. 
var formValN = event.values[N]; 
//Setting the message that goes on the email sent to the approver. 
var message = 'This message has the following information from the form submit: '+ formVal1+'blah blah' + formVal2+...........+formValN; 
//Title for the mail sent. 
var title = 'New Submission'; 
//Email address the email is sent to. 
var mailAdd = '[email protected]' //this could be an array for multiple recipients. 
//Sending Email. 
MailApp.sendEmail(mailAdd, title, message); 
} 

這個腳本編寫之後,你必須去資源>當前腳本的觸發器。在這種情況下,您可以使用表單提交中的電子表格觸發您的功能。