Ad

How To Have OnFormSubmit(e) And OnEdit(e) Together Nested

I want onFormSubmit(e) to be my main function trigger and within that I want onEdit(e) to be nested. Basically, no matter, the trigger will run onFormSubmit but it will do others within the onEdit if there is any edit, if there isn't then it will do something else.

I can't see to understand and make it work.

My script triggers shows onFormSubmit as the only function and onEdit is not in the dropdown.

function onFormSubmit(e){
    ScriptApp.newTrigger("onEdit").timeBased().after(60000).create();
  function onEdit(e){
    var ss = SpreadsheetApp.getActiveSpreadsheet().getRange('SpeedVSD');
    var sheet = ss.getSheetByName("Responses 1");
    var row = ss.range.getRow();
    var col = ss.range.getColumn();

    if (col >= ss.getColumn() && col <= ss.getLastColumn() && row >= ss.getRow() && row <= ss.getLastRow()){
      console.log("You edited a Cell within Range");

    }

  }

edit: Managed to get my lastRow value. However, I am still looking to get a command that can get the lastRow value for all the columns instead of manually doing it.

edit: Using a FOR Loop helps with collating the values.

//This is to get the Last Row on Column 2 value.
var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheetByName('FIRST');

  var row = sheets.getLastRow();
  for(var i = 1; i <= sheets.getLastColumn(); i++){
  var myID = sheets.getRange(row, i).getValue();
  }

  console.log("Row Number: "+row);
  console.log("Content of last Row: "+myID);```


Ad

Answer

function onFormSubmit(e){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheetByName('FIRST');
  var row = sheets.getLastRow();
  var myIDCol2 = 2;
  var myIDCol3 = 3;
  var myID2 = sheets.getRange(row, myIDCol2).getValue();
  var myID3 = sheets.getRange(row, myIDCol3).getValue();
  console.log("Speed Before Trigger Value: "+myID2);
  console.log("Voltage Before Trigger Value: "+myID3);
    ScriptApp.newTrigger("responsechange").timeBased().after(60000).create();

  }

function responsechange(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheets = ss.getSheetByName('FIRST');

  var row = sheets.getLastRow();
  var myIDCol2 = 2;
  var myIDCol3 = 3;
  /*for(var i = 1; i <= sheets.getLastColumn(); i++){

    console.log("Content of last Row: "+myID);
  }*/
   var myID2 = sheets.getRange(row, myIDCol2).getValue();
  var myID3 = sheets.getRange(row, myIDCol3).getValue();
  var template1 = HtmlService.createTemplateFromFile("speed1");
  var template2 = HtmlService.createTemplateFromFile("voltage");
  template1.speed1 = myID2;
  template2.voltage = myID3;
  console.log("Speed After Trigger Value: "+myID2);
  console.log("Voltage After Trigger Value: "+myID3);







  if((myID2 >=100) || (myID2 <= 50)){
    MailApp.sendEmail("[email protected]","Out of Range Notification Speed","",{htmlBody: template1.evaluate().getContent()});
  }

  if((myID3 >=100) || (myID3 <= 50)){
    MailApp.sendEmail("[email protected]","Out of Range Notification Voltage","",{htmlBody: template2.evaluate().getContent()});
  }

  }

With this, I managed make it work whereby on form submit, lets say the values are below 50 and above 100, it will trigger an email after the time-based trigger. I also tried within the time-based trigger, I edited the values to be within the range and it did not send an email. However, the only problems now is, if there are many triggers, it will stop the trigger by saying This script has too many triggers. Triggers must be deleted from the script before more can be added.

But on the bright side, I managed to get the last value submitted to have it checked if it was edited or not.

Ad
source: stackoverflow.com
Ad