Search code examples
emailgoogle-apps-scriptgoogle-sheets

Getting values from cells in Google scripts


I am trying to make working sheets for my work. In Google scripts, I've created "Custom Menu" for my sheet which is sending email correctly. But now I want to get value from the specific cell and check if it is below, for example, 2, send an email with that value.

For now, I have this:

function onOpen() { 
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Custom Menu')
      .addItem('First item', 'menuItem1')
      .addSeparator()
      .addToUi();
}
function menuItem1() {
  SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
     .alert('You clicked the first menu item!');
  if( 'A1' > 3){
  MailApp.sendEmail('[email protected]', 'subject', 'message');
  }
}

I don't know how to get this value from this cell. This 'If" is just an example of what I am trying to do, I know it is not working. Thank you in advance for any kind of help.


Solution

  • First, You need to find the sheet:

    var sheet = SpreadsheetApp.getActiveSheet();
    

    Then, you need to specify a cell range and get the value(s):

    var value = sheet.getRange("A1").getValue();
    

    You can browse the API for more functions here: https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app