Can’t get the data from Spreadsheet for Sending Email Notifications by Google App Script

I have a spreadsheet data containing Name, Start Contract, End Contract. I want to send email notifications to gmail if the End Contract ends within 1 month. I got the email but i can’t get the value of ValueA, ValueB, ValueC, and ValueH in email body.

function onChange(e) {
  try {
    if (!e || !e.source) {
      throw new Error("Event object or source is undefined.");
    }

    var sheet = e.source.getActiveSheet();
    var editedRange = e.range;
    var editedRow = editedRange.getRow();

    console.log("Sheet Name:", sheet.getName());
    console.log("Edited Row:", editedRow);

    if (sheet.getName() === '2023') {
      var valueInColumnI = sheet.getRange(editedRow, 9).getValue();
      console.log("Value in Column I:", valueInColumnI);

      if (valueInColumnI === 'OVERDUE') {
        var valueInColumnC = sheet.getRange(editedRow, 3).getValue();
        var valueInColumnH = sheet.getRange(editedRow, 8).getValue();
        var valueInColumnA = sheet.getRange(editedRow, 1).getValue();
        var valueInColumnB = sheet.getRange(editedRow, 2).getValue();

        console.log("Raw Value in Column C:", valueInColumnC, "Type:", typeof valueInColumnC);
        console.log("Raw Value in Column H:", valueInColumnH, "Type:", typeof valueInColumnH);
        console.log("Raw Value in Column A:", valueInColumnA, "Type:", typeof valueInColumnA);
        console.log("Raw Value in Column B:", valueInColumnB, "Type:", typeof valueInColumnB);

        var stringValueC = convertToString(valueInColumnC);
        var stringValueH = convertToString(valueInColumnH);
        var stringValueA = convertToString(valueInColumnA);
        var stringValueB = convertToString(valueInColumnB);

        console.log("Converted String Value in Column C:", stringValueC);
        console.log("Converted String Value in Column H:", stringValueH);
        console.log("Converted String Value in Column A:", stringValueA);
        console.log("Converted String Value in Column B:", stringValueB);

        sendEmailNotification(stringValueA, stringValueB, stringValueC, stringValueH);
      }
    }
  } catch (error) {
    console.error("Error in onChange:", error.message);
  }
}

function convertToString(value) {
  if (value === null || value === undefined) {
    return 'N/A';
  }
  if (Object.prototype.toString.call(value) === '[object Date]') {
    return Utilities.formatDate(value, Session.getScriptTimeZone(), 'MM/dd/yyyy');
  }
  if (typeof value === 'object') {
    return JSON.stringify(value); // Convert objects to JSON strings for better debugging
  }
  return String(value);
}

function sendEmailNotification(valueA, valueB, valueC, valueH) {
  var email = "myemail"; 
  var subject = "Contract Extend";

  var message = `Hello,

Please be informed of the following details:

- Contract: ${valueC}
- Due Date: ${valueH}
- Additional Info A: ${valueA}
- Additional Info B: ${valueB}

Please ensure to extend the contract '${valueC}' before '${valueH}'.

Best regards,
Your Team`;

  console.log("Email Message:", message);

  MailApp.sendEmail(email, subject, message);
}

The output shows undefined as seen below. What should I do to fix this script?

Hello,

Please be informed of the following details:

  • Contract: undefined
  • Due Date: undefined
  • Additional Info A: [object Object]
  • Additional Info B: undefined

Please ensure to extend the contract ‘undefined’ before ‘undefined’.

Best regards,
Your Team

0

function onChange(e) {
    // ...
    var editedRange = e.range;

There is no range field in the “on change” event object.

Delete the “on change” trigger and run the function on an installable “on edit” installable trigger instead.

You can reduce complexity in the code by using Range.getDisplayValues(), like this:

function installableOnEdit(e) {
  if (!e || !e.range) throw new Error('Event object or range is undefined.');
  try {
    const sheet = e.range.getSheet();
    if (sheet.getName() !== '2023') return;
    const values = sheet.getRange(e.range.rowStart, 1, 1, 9).getDisplayValues().flat();
    if (values[8] !== 'OVERDUE') return; // column I
    sendEmailNotification(values[0], values[1], values[2], values[7]);
  } catch (error) {
    e.source.toast(error.message);
    throw error;
  }
}

See Range.getDisplayValues().

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật