Home > Software engineering >  OnEdit Script to Print Executed Formula
OnEdit Script to Print Executed Formula

Time:06-23

So I have a google sheets script that will do on edit of Column 1 to "Print" something in Column 14, And this works. But I'm wanting the print to be a little more dynamic and print what the value of this formula is at the time of the OnEdit execution (=C1 1) as cell C1 changes weekly. So how do i get this script to "Print" the Results of that formula?

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Current Loads 2020" ) { //checks that we're on Sheet1 or not
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, 14);
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
nextCell.setValue("Pinted Value");
}
}
}

CodePudding user response:

In case anyone else looks this up in the future, here's the Answer to my own question.

At first it was executing the script multiple times, but I added the Getrange higher up in the script and that seems to have fixed that issue, and so now it's actually printing the value of the formula =C1 1.

function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "Current Loads 2020" ) { //checks that we're on Sheet1 or not
var v = s.getRange(1,3).getValue()
var r = s.getActiveCell();
if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
var nextCell = r.offset(0, 13);
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
nextCell.setValue(v 1);
}
}
}

CodePudding user response:

Try this:

function onEdit(e) {
  var sh = e.range.getSheet();
  if (sh.getName() == "Current Loads 2020") { 
    if (e.range.columnStart == 1) { 
      var nextCell = e.range.offset(0, 14);
      if (nextCell.getValue() === '') 
        nextCell.setValue(sh.getRange("C1").getValue()   1;
    }
  }
}

Note: this function require the event object to run now

  • Related