I could really use some help with my onedit triggered scripts. Needing to simplify and condense into one script.

1,104 views
Skip to first unread message

Jon Beckner

unread,
Apr 12, 2022, 3:10:38 PM4/12/22
to Google Apps Script Community
I have two separate scripts in google sheets that are triggered by "TRUE" value checkboxes. The first script is set to uncheck all checkboxes from the sheet and the second is resetting a dropdown list. I'll post the code below for the two scripts. Ultimately I would like both conditions to run on one checkbox. If the code can be written in a more simplified manor that would not be a bad thing.

Best regards, Jon

Script 1:

function onEdit(e) {
  if (e.range.getSheet().getName() == 'Audits') {
    if (e.range.getA1Notation() == 'J27' && e.value == "TRUE") {
      e.range.setValue("FALSE");
      var ss = SpreadsheetApp.getActive();
      var sh = ss.getActiveSheet();
      var rg = sh.getDataRange();
      var vA = rg.getDataValidations();
      var cbA = [];
      for (var i = 0; i < vA.length; i++) {
        for (var j = 0; j < vA[i].length; j++) {
          var rule = vA[i][j];
          if (rule != null) {
            var criteria = rule.getCriteriaType();
            if (criteria == SpreadsheetApp.DataValidationCriteria.CHECKBOX) {
              sh.getRange(i + 1, j + 1).setValue(null)

            }
          }
        }
      }
    }
  }
}
Script 2

function onEdit(e) {
  if (e.range.getSheet().getName() == 'Audits') {
    if (e.range.getA1Notation() == 'E27' && e.value == "TRUE") {
      e.range.setValue("FALSE");
      var ss = SpreadsheetApp.getActive();
      var sh = ss.getActiveSheet();
      var rg = sh.getDataRange();
      var vA = rg.getDataValidations();
      var cbA = [];
      for (var i = 0; i < vA.length; i++) {
        for (var j = 0; j < vA[i].length; j++) {
          var rule = vA[i][j];
          if (rule != null) {
            var criteria = rule.getCriteriaType();
            if (criteria == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) {
              sh.getRange(i + 1, j + 1).setValue(null)

            }
          }
        }
      }
    }
  }
}

Jon Beckner

unread,
Apr 12, 2022, 3:17:31 PM4/12/22
to Google Apps Script Community
Also my scripts run whenever they feel like it.

CBMServices Web

unread,
Apr 12, 2022, 3:19:35 PM4/12/22
to google-apps-sc...@googlegroups.com
Hi Jon,

Can you share a sample spreadsheet so that we can understand the environment the script runs in?

This message (including any attachments) may contain confidential and privileged information belonging to the sender, for a specific individual and purpose, and is legally privileged. If you are not the intended recipient, you should delete this message and any disclosure, copying, forwarding or distribution of this message, or the taking of any action based on it, by you is strictly prohibited.

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d56089e8-7d7e-459d-b54c-66422b404e6an%40googlegroups.com.

Jon Beckner

unread,
Apr 12, 2022, 3:31:34 PM4/12/22
to Google Apps Script Community
I wish I could but company policy says I can't do that I can however share a few screenshots 


Screen Shot 2022-04-12 at 3.27.46 PM.png
Screen Shot 2022-04-12 at 3.28.53 PM.png

Jon Beckner

unread,
Apr 13, 2022, 9:17:43 AM4/13/22
to Google Apps Script Community
Someone on another forum answered my question...I'll post the new and improved script below

function onEdit(e) {

  var ss = SpreadsheetApp.getActive();
  var sh = ss.getActiveSheet();
  var rg = sh.getDataRange();
  var vA = rg.getDataValidations();
  if (e.range.getSheet().getName() == 'Audits') {
    if (e.range.getA1Notation() == 'E27' && e.value == "TRUE") {
      e.range.setValue("FALSE");
      for (var i = 0; i < vA.length; i++) {
        for (var j = 0; j < vA[i].length; j++) {
          var rule = vA[i][j];
          if (rule != null) {
            var criteria = rule.getCriteriaType();
            if (criteria == SpreadsheetApp.DataValidationCriteria.CHECKBOX || criteria == SpreadsheetApp.DataValidationCriteria.VALUE_IN_LIST) {
Reply all
Reply to author
Forward
0 new messages