电子表格测试计划于 1899 年恢复

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

有人可以告诉我为什么我的应用脚本和我的日程安排 一直告诉我我已经预订了 1899 年的机票,什么时候 2024 年

// Function to send email with image and booking details
function sendEmailWithImage(Username, Timeslot, Day) {
  var imageObject = {};
  var successImageLoading = true;
  var sheet = SpreadsheetApp.getActive().getSheetByName('Schedule');
  var emailAddress = "[email protected]"; // Your email
  var subject = "Presenter Booked";

  // Use try-catch to handle errors while loading the image
  try {
    imageObject['myImage1'] = DriveApp.getFileById('1oin8reV7pvZZ9kewuYYw-z4lAFf233YI').getAs('image/png');
  } catch (error) {
    successImageLoading = false;
  }

  // Ensure Day is a Date object
  if (!(Day instanceof Date)) {
    Logger.log("Day is not a valid Date object: " + Day);
    return; // Exit the function if Day is invalid
  }

  // Convert the Day and Timeslot into a human-readable format
  var dayFormatted = Utilities.formatDate(Day, Session.getScriptTimeZone(), "mm dd yy");  // e.g., "Saturday, Oct 07 2023"
  var timeFormatted = Timeslot; // Assuming Timeslot is already in a readable format; adjust as needed

  // Create HTML content for the email
  var htmlStartString = "<html><head><style type='text/css'> table {border-collapse: collapse; display: block;} th {border: 1px solid black; background-color:blue; color: white;} td {border: 1px solid black;} #body a {color: inherit !important; text-decoration: none !important; font-size: inherit !important; font-family: inherit !important; font-weight: inherit !important; line-height: inherit !important;}</style></head><body id='body'>";
  var htmlEndString = "</body></html>";

  // Message content with Username, Timeslot, and Day
  var message = `Slot Booked! Thank you ${Username} for booking. Your slot is scheduled for ${dayFormatted}, ${timeFormatted}.`;

  var emailBody = `${htmlStartString}<p>${message}</p>`;

  // Include image in the email body if image loading is successful
  if (successImageLoading) {
    emailBody += `<p><img src='cid:myImage1' style='width:400px; height:auto;' ></p>`;
  }

  emailBody += htmlEndString;

  // Debugging log
  Logger.log(emailBody);  // This will show the email body in the Logs for debugging

  // Send email
  MailApp.sendEmail({
    to: emailAddress,
    subject: subject,
    htmlBody: emailBody,
    inlineImages: (successImageLoading ? imageObject : null)
  });
}

// Trigger function for On Change event
function onChange(e) {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Schedule');
  var range = e.range;
  var row = range.getRow();
  var col = range.getColumn();

  if (col >= 3 && col <= 9) { // Check if the change is within the schedule columns (C to H)
    var Username = sheet.getRange(row, col).getValue(); // Get the presenter's name
    var Timeslot = sheet.getRange(row, 2).getValue(); // Get the time from column B
    var Day = sheet.getRange(4, col).getValue(); // Get the day from row 4 (header)

    // Convert Day if it's not already a Date object
    if (typeof Day === "string") {
      Day = new Date(Day); // Convert string to Date
    }

    if (Username) {
      // Call the sendEmailWithImage function with the Username, Timeslot, and Day
      sendEmailWithImage(Username, Timeslot, Day);
    }
  }
}

日程

我尝试在小写的日期中添加不同的代码行 mm, dd, yy 再次测试电子表格时间表,而不是大写字母,仍然告诉我 我订的是1899

结果已通过电子邮件发送,预订了 1899 年的席位

google-sheets appscript
1个回答
0
投票

您可能使用时间或持续时间而不是日期的

Day
来调用该函数。当日期时间中缺少日期部分时,它默认为 Google 表格纪元,即
1899-12-31

使

Day
指向包含修复日期的单元格。

请参阅在 Google 表格中使用日期和时间值在 Apps 脚本中使用日期和时间

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