N
N
Nik322020-02-18 18:59:56
Google Apps Script
Nik32, 2020-02-18 18:59:56

How to fire a trigger when editing a specific cell, specific sheet?

Hello, this issue has already been discussed on the forum. But I still don't understand how it works. I need the trigger to run when cell "M2" "Sheet1" is edited and fire this trigger:

function CopyList() {
  var sss = SpreadsheetApp.openById('ID-ТАБЛИЦЫ1'); 
  var ss = sss.getSheetByName('Sheet1'); 
  
  var from = ss;  
  var fromValues = from.getDataRange().getValues();  
  var fromData = fromValues

  var tss = SpreadsheetApp.openById('ID-ТАБЛИЦЫ2'); 
  var ts = tss.getSheetByName('Sheet2'); 
     
  ts.getRange(ts.getLastRow() + 1, 1, fromData.length, fromData[0].length)
    .setValues(fromData);
    }

But I don't understand how to do it

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2020-02-18
@Nik32

Probably this is how it should work

/**
 * @param {GoogleAppsScript.Events.SheetsOnEdit} e
 */
function onEdit(e) {
  if (
    e.range.getSheet().getName() === 'Sheet1' &&
    e.range.getA1Notation() === 'M2'
  )
    CopyList();
}

/**
 *
 */
function CopyList() {
  var sss = SpreadsheetApp.openById('ID-ТАБЛИЦЫ1');
  var ss = sss.getSheetByName('Sheet1');

  var from = ss;
  var fromValues = from.getDataRange().getValues();
  var fromData = fromValues;

  var tss = SpreadsheetApp.openById('ID-ТАБЛИЦЫ2');
  var ts = tss.getSheetByName('Sheet2');

  ts.getRange(
    ts.getLastRow() + 1,
    1,
    fromData.length,
    fromData[0].length
  ).setValues(fromData);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question