Home > Back-end >  Why is my onEdit script not copying cell to adjoining cell when edited?
Why is my onEdit script not copying cell to adjoining cell when edited?

Time:06-19

I am trying to copy the cell value in column B to the next adjoining cell when its value is edited. The idea is that the value in the adjoining cell is also copied to the next adjoining cell when the column B cell is again edited, thus creating a registry of the values this cell has had. This is what I have done but it doesn't seem to work:

function copyProd(e) {
  var range = e.range;
  var spreadSheet = e.source;
  var sheetName = spreadSheet.getActiveSpreadSheet().getActiveSheet();
  var column = range.getColumn();
  var row = range.getRow();
  var inputValue = e.value;
if ((sheetName == "Cart") && (e.range.getColumn() == 2)) {
var row = e.range.getRow();
var ss_ans_val = spreadSheet.getRange(row,3,1,22).getValues();
ss.getRange(row,4,1,22).setValues(ss_ans_val);
ss.getRange(row,3,1,1).setValue(e.oldValue);
}
}

This is supposed to be triggered by an onEdit installable trigger which fails. I'm a noob and would appreciate it if someone could tell me what I'm doing wrong.

https://docs.google.com/spreadsheets/d/1tBsZd3XcuZLwkvmDWM4obtQ46FA4RRoWVDVI5ae5uRE/edit?usp=sharing

CodePudding user response:

Try it this way:

function onEdit(e) {
  //e.source.toast("entry");
  const sh = e.range.getSheet();
  if ((sh.getName() == "Cart") && e.range.columnStart == 2) {
    //e.source.toast("Flag1")
    var ss_ans_val = sh.getRange(e.range.rowStart, 3, 1, 22).getValues();
    sh.getRange(e.range.rowStart, 4, 1, 22).setValues(ss_ans_val);
    sh.getRange(e.range.rowStart, 3).setValue(e.oldValue);
  }
}

If this is all you require then having an installable trigger is not required.

  • Related