Emir Yucedag
Emir Yucedag

Reputation: 1

How can I use Google Scripts to only fetch information from specific sheets?

I have a script with a webhook that retrieves details of edited cells in Google sheets, however, I'm trying to have specific codes per sheet in the spreadsheet (code 1 only reports edits to "jan" which is a seperate sheet from "feb," which would have its own individual code).

Using getSheetByName or getName doesn't seem to resolve this problem, could anyone take a look below?

var POST_URL = "webhook"

function onEdit(event){
  var sheet_name = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('feb');
  if (sheet_name != null)
  var rangeNotation = event.range.getA1Notation();
  var oldValue = event.oldValue;
  var value = event.value;
  var items = [];

  if (value == undefined && oldValue == undefined){
    if (rangeNotation.includes(':')){
      reason = "";
    }
    else{
      reason = "";
    }
  }
  else{
    if (oldValue == undefined){
      oldValue = "";
    }

    if (value == undefined){
        value = "";
    }
    reason = value
  }

Upvotes: 0

Views: 40

Answers (1)

doubleunary
doubleunary

Reputation: 18784

To make your function behave differently on different sheets, check the sheet name and use conditionals, like this:

function onEdit(e) {
  const sheetName = e.range.getSheet().getName();
  if (sheetName.match(/^(jan)$/i)) {
    // ...
  } else if (sheetName.match(/^(feb)$/i)) {
    // ...
  } else if (sheetName.match(/^(mar|apr|may)$/i)) {
    // ...
  }
)

Upvotes: 1

Related Questions