'Unable to insert signature / Google Doc table into script to draft emails
I'm using a template that allows me to automatically draft emails to send. I'm using a Google Sheet and Google Doc to make this happen. The script is working fine, but I'm unable to insert my signature. I've tried various workarounds and have been unsuccessful. I have even tried to manually add the signature to the drafted email, but it will not allow it. I have created a table on my Google doc in an attempt to make this happen.
Is there any way to 1- add my signature to the drafted email? Or 2- insert the table from the Google doc to the draft?
Here is the code I'm using.
var googleDocId = "15mR-cuM9JVRwzXByd7-X8n_QgKODybdjXZJhyNYxW4c";
var emailField = 'Email';0
var emailSubject = 'Question About Your Business';
var emailStatus = 'Date Drafted';
var sheet = SpreadsheetApp.getActiveSheet();
function draftMyEmails() {
var emailTemplate = DocumentApp.openById(googleDocId).getText();
var data = getCols(2, sheet.getLastRow() - 1);
var myVars = getCols(1, 1)[0];
var draftedRow = myVars.indexOf(emailStatus) + 1;
data.forEach(function(row, index){
var config = createConfig(myVars, row);
// Prevent from drafing duplicates and from drafting emails without a recipient
if (config[emailStatus] === '' && config[emailField]) {
var emailBody = replaceTemplateVars(emailTemplate, config);
var emailSubjectUpdated = replaceTemplateVars(emailSubject, config);
GmailApp.createDraft(
config[emailField], // Recipient
emailSubjectUpdated, // Subject
emailBody // Body
);
sheet.getRange(2 + index, draftedRow).setValue(new Date()); // Update the last column
SpreadsheetApp.flush(); // Make sure the last cell is updated right away
}
});
}
function replaceTemplateVars(string, config) {
return string.replace(/{[^{}]+}/g, function(key){
return config[key.replace(/[{}]+/g, "")] || "";
});
}
function createConfig(myVars, row) {
return myVars.reduce(function(obj, myVar, index) {
obj[myVar] = row[index];
return obj;
}, {});
}
function getCols(startRow, numRows) {
var lastColumn = sheet.getLastColumn(); // Last column
var dataRange = sheet.getRange(startRow, 1, numRows, lastColumn) // Fetch the data range of the active sheet
return dataRange.getValues(); // Fetch values for each row in the range
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
