简体   繁体   中英

How to send whole row of data in my spreadsheet via email based on single cell edit (Google Script / GAS)

We have set up a workflow spreadsheet that has job information such as client name, job details etc. I have a nifty script that hides a whole row if you choose "Complete" from a drop down menu in the final column of that row.

What I am trying to do now is send an email with all the data in the row we are hiding.

UPDATED: Here is the code I have so far:

/** 
* TITLE:
*     Hide a row if a value is inputted then send an email 
*/

//**GLOBALS**
// Sheet the data is on.
var SHEET = "Live Jobs";
// The value that will cause the row to hide. 
var VALUE = "Complete";
// The column we will be using 
var COLUMN_NUMBER = 22

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();

  //Ensure on correct sheet.
  if(SHEET == activeSheet.getName()){
    var cell = ss.getActiveCell()
    var cellValue = cell.getValue();

    //Ensure we are looking at the correct column.
    if(cell.getColumn() == COLUMN_NUMBER){
      //If the cell matched the value we require,hide the row. 
      if(cellValue == VALUE){
        activeSheet.hideRow(cell)
        SendEmail(e);
      };
    };
  };
}



/**
 * Sends emails with data from the current spreadsheet.
 */
function SendEmail(e) {
  // Fetch the email address
var correctSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Live Jobs');
var emailRange = correctSheet.getRange('Z1');  
var emailAddress = emailRange.getValue();

  var sheet = SpreadsheetApp.getActiveSheet();

var cell = sheet.getActiveCell()
var rowValue = cell.getRow();  

  var startRow = rowValue; // First row of data to process
  var numRows = 2; // Number of rows to process
  // Fetch the range of cells A2:B3
  var dataRange = sheet.getRange(startRow, 1, 1, 5);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  for (var i in data) {
    var row = data[i];
    var message = row; // Second column
    var subject = 'Sending emails from a Spreadsheet';
    MailApp.sendEmail(emailAddress, subject, message);
  }
}

Now, the hide row works, and the email function works if I run it from the Google Script editor, but for some reason, the email does not fire when I select "Complete" but IT WAS WORKING YESTERDAY!!!

Any tips?

M

Try this:

You probably can't do this with a simple trigger since sending email requires authorization.

var SHEET = "Live Jobs";
var VALUE = "Complete";
var COLUMN_NUMBER = 22
function onEdit(e) {
  var sh=e.range.getSheet();
  if(sh.getName()==SHEET){
    if(e.range.columnStart==COLUMN_NUMBER && e.value==VALUE){
        sh.hideRow(e.range.rowStart);
        SendEmail(e);
      }
    }
  }
}


function SendEmail(e) {
  MailApp.sendEmail(e.range.getSheet().getRange('Z1').getValue(), 'Invoice Job Alert', e.range.getSheet(e.range.rowStart,1,1,e.range.getSheet().getLastColumn()).getValues()[0].join(','));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM