по электронной почте с листа Google с предварительным просмотром - PullRequest
0 голосов
/ 18 марта 2019

Этот скрипт создает черновик электронной почты в моей учетной записи Gmail.Я бы хотел, чтобы черновик автоматически открывался для предварительного просмотра, с возможностью вносить любые исправления и отправлять, не покидая листа.

Как мне этого добиться?

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 clientEmail = "ron@xxxxxxx.com"
        var ClientFirstName = row[2];               // Col A: Client name
        var ClientLastName = row[3];           // Col B: Client email
        var dob = new Date(row[6]);
        var ClientsDOB = Utilities.formatDate(dob, 
            SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "MM/dd/yyyy");                      
        // Col C: Vegetable name
        var Gender = row[7];                     // Col D: Vegetable description
        var ClientAddress = row[13];
        var PrimaryPolicyHolderName = row[14];
        var policyDOB = (row[15]);
        if(policyDOB != '') { 
            var policyDOBDate = new Date(policyDOB); 
            var PrimaryPolicyHolderDOB = Utilities.formatDate(policyDOBDate, 
    SpreadsheetApp.getActive().getSpreadsheetTimeZone(), 'MM/dd/yyyy');
        } else { 
            var PrimaryPolicyHolderDOB = '';
        }
        var InsuranceCompanyName = row[16];
        var MemberID = row[17];
        var GroupNumber = row[18];
        var InsuranceCompanyPhone  = row[19];
        var Howwasthepolicyobtained = row[20]
        var emailStatus = row[lastColumn - 1];  // Col E: Email Status

        // Prevent from drafing duplicates and from drafting emails without a recipient
        if (emailStatus !== EMAIL_DRAFTED && clientEmail) {    
            // Build the email message
            var emailBody =  '<p> ' + "Client First Name - " + ClientFirstName + '<p>';
            if(ClientLastName != '') { 
                emailBody += '<p> ' + "Client Last Name - " + ClientLastName + '<p>'; }
            if(ClientsDOB != '') { 
                emailBody += '<p> ' + "Clients DOB - " + ClientsDOB + '<p>'; }
            if(Gender != '') { 
                emailBody += '<p> ' + "Gender - " + Gender + '<p>'; }
            if(ClientAddress != '') { 
                emailBody += '<p> ' + "Client Address - " + ClientAddress + '<p>'; }                
            if(PrimaryPolicyHolderName != '') { 
                emailBody += '<p> ' + "Primary Policy Holder Name - " + 
                            PrimaryPolicyHolderName + '<p>'; }
            if(PrimaryPolicyHolderDOB != '') {  
                emailBody += '<p> ' + "Primary Policy Holder DOB - " + 
                            PrimaryPolicyHolderDOB + '<p>'; }
            if(InsuranceCompanyName != '') { 
                emailBody += '<p> ' + "Insurance Company Name - " + InsuranceCompanyName  + '<p>'; }
            if(MemberID != '') { 
                emailBody += '<p> ' + "Member ID - " + MemberID  + '<p>'; }
            if(GroupNumber != '') { 
                emailBody += '<p> ' + "Group Number - " + GroupNumber  + '<p>'; }
            if(InsuranceCompanyPhone) { 
                emailBody += '<p> ' + "Insurance Company Phone Number - " + 
                            InsuranceCompanyPhone  + '<p>'; }
            if(Howwasthepolicyobtained) {  
                emailBody += '<p> ' + "How Was The Policy Obtained - " + Howwasthepolicyobtained  + '<p>'; }
            var mailSig = '<h3>Ron</h3><h5>AmHealth</h5><h4>844-601-4848 Toll Free</h4><a href="mailto:ron@amhealth.com">ron@amhealth.com</a>';
            emailBody += mailSig; 
            var confidential = "<p><h5><strong>CONFIDENTIALITY NOTICE:</strong></h5> This [electronic transmission], and any documents, files, or previous [electronic] messages attached to it, may contain privileged and confidential and/or protected health information (PHI), as defined under Federal and State law. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, you are hereby notified that any disclosure, copying, distribution, or use of any of the information contained in or attached to this message is STRICTLY PROHIBITED. If you have received this [electronic message] in error, please immediately notify us by a separate email or by telephone at (424) 330-2774, and destroy the original transmission and its attachments without reading or downloading them.</p>";
            emailBody += confidential;
            // Create the email draft
            GmailApp.createDraft(
                clientEmail,            // Recipient
                'AMHEALTH INS VER - ' + ClientFirstName,  // 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
        }
    }
}
...