admin管理员组

文章数量:1431904

The code is pretty much done but I want to include today's date in the subject. What do I have to add to "const subject" in order to accomplish this? I've already defined today's date as "today"

the code is in the image, please see the highlighted part

See image code

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) {
      
    }
  
}

The code is pretty much done but I want to include today's date in the subject. What do I have to add to "const subject" in order to accomplish this? I've already defined today's date as "today"

the code is in the image, please see the highlighted part

See image code

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) {
      
    }
  
}
Share Improve this question edited Nov 19, 2024 at 13:37 Wicket 38.8k9 gold badges80 silver badges195 bronze badges asked Nov 19, 2024 at 6:34 Miguel Arturo MonclusMiguel Arturo Monclus 11 silver badge
Add a comment  | 

1 Answer 1

Reset to default 1

Append new Date to a string

Since your today variable is already a string because of Utilities.formatDate() (see image below) you can just easily append it to a string(your subject) using the following ways:

Using + symbol

const subject = "New machine "+today

or

Using String literals

const subject = `New Machine ${today}`

Data type of today variable

Sample Output

Reference: Utilities.formatDate

本文标签: