如何在 Apps 脚本中使用 Mailapp.sendemail 在电子邮件主题中显示字符串 + 今天的日期?

问题描述 投票:0回答:1

代码已基本完成,但我想在主题中包含今天的日期。为了实现这个目标,我必须在“const subject”中添加什么?我已经将今天的日期定义为“今天”

代码在图片中,请查看突出显示的部分

查看图片代码

function sendEmails() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  const dataRange = sheet.getRange("A17:dm");
  const data = dataRange.getValues();
  i=0; //i & j is the row number (up/down)
  j=3;

    const col = data[j];
    const row = data[i];
    const machinetype = row[84]; //the 84 is how far you are going to the right, add 4 to each when re-running it
    const machinemodel = col[84];
    const emailAddress = "[email protected]";
    const subject = "New Machine";


   const message = createEmailMessage(machinetype, machinemodel);
function createEmailMessage(machinetype, machinemodel) {
  const message = `Hi Dhariana,

A new machine has arrived at the shop. It is a ${machinemodel}... a ${machinetype} machine.

Kindly schedule the cleaning procedure.

Thank you. 

Best,
`;

  return message;
}

    try {
      MailApp.sendEmail(emailAddress, subject, message);
      console.log(` Email sent to ${emailAddress}`);
      console.log(`${machinetype}, ${machinemodel}`);
    } catch (error) {
      
    }
  
}
email google-sheets google-apps-script
1个回答
1
投票

将新日期附加到字符串

由于您的

today
变量已经是一个字符串,因为 Utilities.formatDate() (见下图),您可以使用以下方式轻松地将其附加到字符串(您的主题):

使用

+
符号

const subject = "New machine "+today

使用

String literals

const subject = `New Machine ${today}`

today
变量的数据类型

enter image description here

示例输出

output

参考:Utilities.formatDate

© www.soinside.com 2019 - 2024. All rights reserved.