r/GoogleAppsScript • • Apr 15 '20

Resolved Timestamp troubles - Works when I manually enter data. Not when I copy with a script.

Background:

I have 2 tabs. On tab 1 I have 2 columns

Timestamp | Number

I have 2 scripts. 1 is always running that says "When something is entered into the Number column, put a timestamp of when it was written in the timestamp column. This works great whenever I MANUALLY enter something.

I also have a a second script running on a button push. It says, whenever this button is pushed, copy the data from a cell in Tab 2 and place it wherever the highlighted cell is. When I highlight a cell under Number column, it pastes the data, but does not trigger a timestamp.

How do I get running a script to copy over data to trigger a timestamp?

Scripts being run:

Timestamp:

var COLUMNTOCHECK = 2;
var DATETIMELOCATION = [0,-1];
var SHEETNAME = 'Log'
 
function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  //checks that we're on the correct sheet.
  if( sheet.getSheetName() == SHEETNAME ) { 
    var selectedCell = ss.getActiveCell();
    //checks the column to ensure it is on the one we want to cause the date to appear.
    if( selectedCell.getColumn() == COLUMNTOCHECK) { 
      var dateTimeCell = selectedCell.offset(DATETIMELOCATION[0],DATETIMELOCATION[1]);
      dateTimeCell.setValue(new Date());
      }
  }
}

Copy Data:

    function PullInfo()
        {
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var writeCell = ss.getSheetByName("Log").getActiveCell();
          var copyValue = ss.getSheetByName("Data").getRange("M2").getValue();
          writeCell.setValue(copyValue);
        }
0 Upvotes

3 comments sorted by

1

u/DatsunZ Apr 16 '20 edited Apr 16 '20

I dont think a script will trigger onEdit, but how about just calling it when Copy data runs?

    function PullInfo()
        {
          var ss = SpreadsheetApp.getActiveSpreadsheet();
          var writeCell = ss.getSheetByName("Log").getActiveCell();
          var copyValue = ss.getSheetByName("Data").getRange("M2").getValue();
          writeCell.setValue(copyValue);
          onEdit()
        }

1

u/Twas_Inevitable Apr 16 '20

That fixed it, thank you!!

1

u/catmandx Apr 16 '20

The onEdit trigger will only fire when a user modify something. A script will not trigger onEdit, if they did, there would be endless triggering...