![](/img/trans.png)
[英]Send email from cell value google sheet, with other columns in email
[英]How to send an email with a formatted table from google sheet using gs?
這是將獲取電子郵件 ID 和主題以及表格的 GS 代碼。
var EMAIL_DRAFTED = "EMAIL DRAFTED";
function draftMyEmails() {
var sheet = SpreadsheetApp.getActiveSheet(); // Use data from the active sheet
var startRow = 2; // First row of data to process
var numRows = sheet.getLastRow() - 1; // Number of rows to process
var lastColumn = sheet.getLastColumn(); // Last column
var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
var data = dataRange.getValues(); // Fetch values for each row in the range
// Work through each row in the spreadsheet
for (var i = 0; i < data.length; ++i) {
var row = data[i];
// Assign each row a variable
var clientName = row[0]; // Col A: Client name
var clientEmail = row[1]; // Col B: Client email
var sub = row[2]; // Col C: subject
var body = row[3]; // Col D: emailbody
var emailStatus = row[lastColumn - 1]; // Col E: Email Status
var range = sheet.getRange(2, 6, 1, 5).getDataRegion(SpreadsheetApp.Dimension.ROWS);
var values = range.getValues();
// Prevent from drafing duplicates and from drafting emails without a recipient
if (emailStatus !== EMAIL_DRAFTED && clientEmail) {
// Build the email message
var emailBody = '<p>Hi ' + clientName + ',<p>';
//emailBody += '<p>We are pleased to match you with your vegetable: <strong>' + veg + '</strong><p>';
//emailBody += '<h2>About ' + veg + '</h2>';
emailBody += '<p>' + values + '</p>';
// emailBody += '<p>' + clientName + ', we hope that you and ' + veg + ' have a wonderful relationship.<p>';
// Create the email draft
GmailApp.createDraft(
clientEmail, // Recipient
sub, // Subject
'', // Body (plain text)
{
htmlBody: emailBody // Options: Body (HTML)
}
);
sheet.getRange(startRow + i, lastColumn).setValue(EMAIL_DRAFTED); // Update the last column with "EMAIL_DRAFTED"
SpreadsheetApp.flush(); // Make sure the last cell is updated right away
}
}
}
我得到的是使用此代碼:
身體
7 & 8,9 & 10,IOS,5 & 6,,,a,b,c,d,,1,2,3,4,,1,2,3,4,,1,2,3,4, ,1,2,3,4,,1,2,3,4,,1,2,3,4,,1,2,3,4,,1,2,3,4,
這就是谷歌表的樣子。
嘗試這個:
function draftMyEmails() {
var sh=SpreadsheetApp.getActiveSheet();
var startRow=2;
var lastCol=sh.getLastColumn();
var dataRange=sh.getRange(startRow,1,sh.getLastRow()-startRow+1,lastCol);
var data=dataRange.getValues();
for (var i=0;i<data.length;i++) {
var row=data[i];
var clientName=row[0];
var clientEmail=row[1];
var sub=row[2];
var body=row[3];
var emailStatus=row[lastCol-1];
var range=sh.getRange(2, 6, 1, 5).getDataRegion(SpreadsheetApp.Dimension.ROWS);
var values=range.getValues();
if (emailStatus!="EMAIL DRAFTED" && clientEmail) {
var emailBody= Utilities.formatString('<p>Hi %s </p>,<br />',clientName);
emailBody+='<style>td{border:1px solid black;)</style><table>';
values.forEach(function(r,j){
emailbody+='<tr>
r.foreach(function(c,k){
emailBody+=Utilities.formatString('<td>%s</td>',c);
});
emailBody+='</tr>';
});
emailBody+='</table>';
GmailApp.createDraft(clientEmail,sub,'',{htmlBody: emailBody});
sh.getRange(startRow + i, lastColumn).setValue("EMAIL_DRAFTED");
}
}
}
請向我提供您的電子表格的圖像,以便我查看您的表格的外觀,我將為您調試表格區域,或者您可以自己進行調試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.