OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

This code works just fine, but I don't know why

  • Thread starter Thread starter Mr G
  • Start date Start date
M

Mr G

Guest
I have some onEdit() code that writes the result in another tab just fine, I don't know how! How does createAbsenceAlert() know what cells have been edited and their value? I can't see how I'm passing it any variables.

Code:
const workBook = SpreadsheetApp.getActiveSpreadsheet(); 

function onEdit(e) {
  var eventValue = e.value
  if (eventValue === "Absent" && e.range.getColumn() === 3) {
    var emailAbsenceDetails = createAbsenceAlert(); 
  }
  return;
};

function createAbsenceAlert() {
  var absentPerson = workBook.getSheetName();
  var vmsSender = workBook.getName()          // extract name of sender from file name
    .split(/\s+/)
    .slice(0, -2)  
    .join(" ");

  const editRecord = workBook.getSheetByName("editRecord");
  let trg = editRecord.getRange(3, 3).getDataRegion().getLastRow()

  // format date.
  let date = new Date().toLocaleDateString('en-GB', { hour: "numeric", minute: "numeric" })
  var absenceDetails = date + ": " + absentPerson + " has just been marked absent from " + vmsSender + "'s appointment";
     
  return absenceDetails
};

I should add, there are multiple tabs in the Google sheet, so it's not just looking at a single sheet.

There's other parts of the code that rely on my understanding what's going on here. Thanks all.

<p>I have some onEdit() code that writes the result in another tab just fine, I don't know how! How does createAbsenceAlert() know what cells have been edited and their value? I can't see how I'm passing it any variables.</p>
<pre><code>const workBook = SpreadsheetApp.getActiveSpreadsheet();

function onEdit(e) {
var eventValue = e.value
if (eventValue === "Absent" && e.range.getColumn() === 3) {
var emailAbsenceDetails = createAbsenceAlert();
}
return;
};

function createAbsenceAlert() {
var absentPerson = workBook.getSheetName();
var vmsSender = workBook.getName() // extract name of sender from file name
.split(/\s+/)
.slice(0, -2)
.join(" ");

const editRecord = workBook.getSheetByName("editRecord");
let trg = editRecord.getRange(3, 3).getDataRegion().getLastRow()

// format date.
let date = new Date().toLocaleDateString('en-GB', { hour: "numeric", minute: "numeric" })
var absenceDetails = date + ": " + absentPerson + " has just been marked absent from " + vmsSender + "'s appointment";

return absenceDetails
};
</code></pre>
<p>I should add, there are multiple tabs in the Google sheet, so it's not just looking at a single sheet.</p>
<p>There's other parts of the code that rely on my understanding what's going on here. Thanks all.</p>
 

Latest posts

Top