如何解决邮件合并问题

时间:2017-08-07 07:05:28

标签: google-apps-script

enter image description here我正在尝试使用邮件合并邮件中的批准按钮创建批准流程。当用户提交HTML表单时,可能会生成邮件。 下面的脚本无法正常工作我已将脚本附加到样本表中。

我无法确定具体问题,请帮我修复或识别

Smaple Sheet

提前谢谢

var REQUESTS_STATE_COLUMN = 32;
var STATE_APPROVERS_EMAILED = "EMAILED_Response Pending";
var STATE_APPROVED = "Approved";
var STATE_DENIED = "Rejected";


function sendEmail(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var dataRange = sheet.getRange("A2:AH2");
  var data = dataRange.getValues();
  for (i in data) {
    var rowData = data[i];
    var email = e.namedValues["Email Address"];
    var emailAddress = rowData[30];
    var submiterEmail = rowData[1];
    var ProjectName = rowData[2];
    var SrDesign = "Sr. Design Architect Services";
    var PerHourRate = rowData[6];    
    var NumberOfHours = rowData[7];
    var TotalRate = rowData[8];
    var Contact  = "Contact Centre Expert";
    var PerHourRate2 = rowData[10];
    var NumberOfHours2 = rowData[11];
    var NTotalRate2 = rowData[12];
    var NJrDesign = "Jr. Design Architect";
    var PerHourRate3 = rowData[14];
    var NumberOfHours3 = rowData[15];
    var TotalRate3 = rowData[16];
    var Outsourced = "Outsourced Technical Support Resource";
    var PerHourRate4 = rowData[18];
    var NumberOfHours4 = rowData[19];
    var TotalRate4 = rowData[20];
    var Supplier = "Supplier Management";
    var PerHourRate5 = rowData[22];
    var NumberOfHours5 = rowData[23];
    var TotalRate5 = rowData[24];
    var Project = "Project Management & Execution";
    var PerHourRate6 = rowData[26];
    var NumberOfHours6 = rowData[27];
    var TotalRate6 = rowData[28];
    var UserAccount = rowData[28];
    var GrandTotalRate = rowData[29];
    var seq = e.values[1];
    var url = ScriptApp.getService().getUrl();
    var sheet = SpreadsheetApp.openById('1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc').getSheetByName("Sheet1");
    var cellval = range.getCell(row,1).getValue();


  var options = '?approval=%APPROVE%&reply=%EMAIL%'
           .replace("%EMAIL%",e.namedValues["User Email Address"])         
  var approve = url+options.replace("%APPROVE%","Approved")+'&row='+data+'&cellval='+cellval; 
  var reject = url+options.replace("%APPROVE%","Rejected")+'&row='+data+'&cellval='+cellval;

 var subject = 'Technology Charges for '+ProjectName+''; 
var html = '<p style="font-family:Verdana; fontsize=9px">Hello Recipient,<br/><br/><br/><br/>Referance No: '+cellval+'<br/><a href='+ approve +'> <b>approve </b></a><a href='+ reject +'> <b>Reject</b></a><br/>'
MailApp.sendEmail({
    to: emailAddress,
    subject: subject,
    htmlBody: html,
  });

   //Add a state in the form spreadsheet with APPROVERS_EMAILED state  
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = sheet.getLastRow();
  sheet.getRange(lastRow, REQUESTS_STATE_COLUMN+1).setValue(STATE_APPROVERS_EMAILED);

}
}

function doGet(e){
  var answer = (e.parameter.approval === 'Approved') ? 'Yes -Approved!' : 'No-Rejected '; 
  var cellval = e.parameter.cellval;
  var email = e.parameter.reply;

  var sheet = SpreadsheetApp.openById("1i5x0oqpCDgOZO2Nbly4SpNkWpLbTXJhT6pPOfQ7NuEc").getSheetByName("Sheet1");
  var data = sheet.getDataRange().getValues();
  var headers = data[0];
  var approvalCol = headers.indexOf("Approval") + 1;
  if (0 === approvalCol) throw new Error ("Must add Approval column.");

  // Record approval or rejection in spreadsheet
  var row = ArrayLib.indexOf(data, 0, cellval);
  if (row < 0) throw new Error ("Request not available.");  // Throw error if request was not found
  sheet.getRange(row+1, approvalCol).setValue(e.parameter.approval);

  // Display response to approver
  var app = UiApp.createApplication();
  app.add(app.createHTML('<h2>Thank you..! Your response has been recorded'+' saying: '+ answer + '</h2>'))
  return app
}

1 个答案:

答案 0 :(得分:0)

getCell需要行和列的整数值,而是传递整行( rowData )。您需要将第45行替换为以下行。

var cellval = dataRange.getCell(i+1, 1).getValue()