将发送(从邮件)发送的日期添加到电子表格中的行

时间:2019-05-15 10:10:26

标签: google-apps-script google-sheets

我有一个脚本,可以从电子表格生成pdf,然后将电子邮件发送到该电子表格中的电子邮件地址。发送pdf时,脚本会自动将一行添加到另一个电子表格中,并带有与创建和发送的pdf相关的名称,电子邮件地址和URL。

到目前为止一切正常。

我唯一不知道的是如何将发送的日期(从电子邮件中)添加到具有与创建和发送的pdf相关的名称,电子邮件地址和URL的电子表格中。

我不知道如何找到发送电子邮件的日期。 我试图尽可能地解释该脚本。

function createPDF() {
  var sheet1 = SpreadsheetApp.getActiveSpreadsheet();
  var ui = SpreadsheetApp.getUi();
  var result = ui.alert( // pop up to ask if the spreadsheet should be saved as pdf
      "Opslaan als PDF?",
      "Sla " +sheet1.getName()+ "_" + sheet1.getRange("A12").getValue() +".pdf op als PDF", // shows the name of the pdf that will be created
      ui.ButtonSet.OK_CANCEL);
  if (result == ui.Button.OK) {
   var docblob =         
SpreadsheetApp.getActiveSpreadsheet().getAs("application/pdf"); 
    docblob.setName(sheet1.getName() + "_" + sheet1.getRange("A12").getValue() + ".pdf"); // combines name of spreadsheet and student to create name for pdf
    var file = DriveApp.createFile(docblob);
    ui.alert(sheet1.getName() + "_" + sheet1.getRange("A12").getValue() + " is beschikbaar via deze link: " + file.getUrl()); // url to created pdf
  } else {
    ui.alert("Opslaan als .pdf is niet gelukt");
  }

  // the following code add just created pdf and another (already excisting) pdf to email
  var OOKnaam = file.getName(); // pdf just created
  var bijlage1 = DriveApp.getFilesByName(OOKnaam); 
  var bijlage2 =     
DriveApp.getFilesByName("Algemene_Voorwaarden.pdf"); // add the other pdf

  var defMessage = "message to student";
  var subject = "Je OOK & POK";
  var emailAddress = sheet1.getRange("A13").getValue(); // gets the emailaddress added in spreadsheet to send mail


  var ui = SpreadsheetApp.getUi(); // pop up with confirmation

  var result2 = ui.alert(
      "Versturen naar "+emailAddress+ "?",
      ui.ButtonSet.OK_CANCEL);
  if (result2 == ui.Button.OK) {
     MailApp.sendEmail(emailAddress, subject, defMessage, {
         replyTo: "email@email.nl",
         name: "name of sender",
         attachments: [bijlage1.next(), bijlage2.next()]

       })
   ui.alert("De email is verstuurd naar "+emailAddress);
   } else {
   ui.alert("Email versturen is mislukt");
   }

// following code adds email, file name and url to another spreadsheet
  var ontvangenSheet = SpreadsheetApp.openById(ID);
  var eMailVoorReminder = sheet1.getRange("A13").getValue();

    data = [ 
      eMailVoorReminder,
      file.getName(),
      file.getUrl(),
    ];

    ontvangenSheet.appendRow(data);
}

我不是立即寻找答案。我想知道从哪里开始或寻找什么,以便自己解决。

编辑: 我在脚本中添加了时间戳:

    var formattedDate = Utilities.formatDate(new Date(), "GMT+2", "dd-MM-yyyy' om 'HH:mm"); 

    data = [ 
      eMailVoorReminder,
      file.getName(),
      file.getUrl(),
      formattedDate,
   ];

ontvangenSheet.appendRow(data);

它确实向添加的行添加了时间戳,但是甚至在未发送电子邮件(取消)时也添加了时间戳。所以到目前为止,对这种解决方案还不完全满意。

0 个答案:

没有答案