S
S
Snowyyy2020-02-15 17:07:48
Google Apps Script
Snowyyy, 2020-02-15 17:07:48

How to fire onEdit from editor or programmatically?

Am I correct in understanding that the code below will fire a trigger on a specific cell change, and not any one? When run, it gives the error "TypeError: Cannot read property 'range' of undefined (line 3, file se)".
Here is the code:

function onEdit(e) {
  // Get cell edited.  If it B6, then do something
  var cellAddress = e.range.getA1Notation();
  if (cellAddress === 'B26') {
    //To Do.  Code here if cell edited was B26
    Logger.log('the check worked!');
  };
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-02-16
@Snowyyy

As far as I understand, you are trying to call a function onEditfrom another function or from the editor.
Error Parsing
Error

TypeError: Cannot read property 'range' of undefined (line 3, file se)

Means that in some file sein 3й строкеthere is some variable with value undefined, whose property rangecannot be read. Everything is obvious here - it undefinedhas no properties.
If you look at the code, it becomes clear that the name of this variable is e. And we get it into a system function onEdit . This means that the system itself passes the context to this function.
OK. So, to test this function, you need to pass the parameter yourself .
For example,
/**
 * Тестирование триггера для события EDIT
 */
function runOnEdit() {
  var source = SpreadsheetApp.getActive();
  var range = source.getRangeByName('Sheet!!B26');
  /**
   * @type {GoogleAppsScript.Events.SheetsOnEdit}
   */
  var e = {
    authMode: ScriptApp.AuthMode.LIMITED,
    oldValue: undefined, // ну или что хотите
    range: range,
    value: range.getValue(),
    source: source,
    triggerUid: 0,
    user: Session.getActiveUser(),
  };

  onEdit(e);
}

/**
 *
 * @param {GoogleAppsScript.Events.SheetsOnEdit} e
 */
function onEdit(e) {
  // Работает простой триггер
}

Check - this code will work exactly the same as if the user made changes in the Table.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question