For loop and if statement for multiple conditions

前端 未结 1 1232
萌比男神i
萌比男神i 2020-12-12 07:04

I am having a head-scratching time with the if conditions. I will make it clear how the sheet is supposed to work.

When users select a value from the drop-down menus

相关标签:
1条回答
  • 2020-12-12 07:21

    This would be a good application for an Installable Edit Trigger function. See Google Sheets Events as well.

    Note that you must use an installable trigger which will run as the document owner, not the simple onEdit() trigger that runs as an anonymous user, because sending an email requires user authorization.

    Something like this (tested):

    function installableOnEdit( e ) {
      if (!e) throw new Error( "Need event parameter." ); // see http://stackoverflow.com/a/16089067
    
      var changedCell = e.range;
      var row = e.range.getRow();
      var col = e.range.getColumn();
      var sheet = e.range.getSheet();
    
      var headers = sheet.getDataRange().getValues()[0]; // Get header row 1
      var statusCol = headers.indexOf('Reminder Email Status')+1;
      // Build an array of "rows we care about". If the headers change, this needs to be updated
      var teamCols = [ headers.indexOf('Team 1 (PM)')+1,
                       headers.indexOf('Team 2 (Colombo)')+1,
                       headers.indexOf('Team 3 (VCS)')+1 ];
    
      if (teamCols.indexOf(col) == -1) return;  // Exit if cell outside of the team columns
    
      var status = e.value.toUpperCase().replace(/ /g,"_");   // Change "Request Sent" to "REQUEST_SENT"
      sheet.getRange(row,statusCol).setValue(status);
    
      //--------- Put your emailing code here, or call a function
    }
    
    // Read user's current cell, and feed to installableOnEdit() as an event.
    // see http://stackoverflow.com/a/16089067
    function test_installableOnEdit() {
      installableOnEdit({
        user : Session.getActiveUser().getEmail(),
        source : SpreadsheetApp.getActiveSpreadsheet(),
        range : SpreadsheetApp.getActiveSpreadsheet().getActiveCell(),
        value : SpreadsheetApp.getActiveSpreadsheet().getActiveCell().getValue(),
        authMode : "LIMITED"
      });
    }
    
    0 讨论(0)
提交回复
热议问题