title: Adding A Timestamp When New Row Inserted or Row Edited in Google Sheets
body: <p><strong>I have:</strong></p>
  
  <ul>
  <li>A google sheet which is automatically receiving new shopify orders
  through zapier</li>
  <li>The orders are inserted as new rows, at least have been so far, but
  I do not know whether they may update be edited in the future
  by zapier</li>
  <li>Customer data from the orders is in columns A through JO</li>
  <li>The below code, which is a container bound simple trigger, that is accessed by going to <strong>Tools > Script Editor</strong> in the google sheet</li>
  </ul>
  
  <p><strong>I want:</strong></p>
  
  <ul>
  <li>To add a time stamp in column JP for new rows or edited rows</li>
  </ul>
  
  <p>I pieced the below code together from this post <a href="https://stackoverflow.com/questions/47109699/want-to-create-timestamp-for-time-added-and-create-unique-incremental-user-id-a">"Want to create timestamp for time added and create unique incremental user ID. And also include a last changed column"</a>. However I have an issue -- when I try to run this I am getting the following error message:</p>
  
  <p>"TypeError: Cannot read property 'source' of undefined (line 6, file "Code")"</p>
  
  <pre><code>function onChange(e) {
  
  var lastChangedTime = 276 //column JP
  
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = e.source.getActiveSheet
  
  var now = new Date();
  
  if (e.changeType == "INSERT_ROW") {
  
  
  //set lastChangedTime when the row is created 
  ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), lastChangedTime, 1, 1).setValue(now);
     } 
  
    if (e.changeType == "EDIT") {
  
  //set date in same row as edit happens, at fixed column  
  ss.getActiveSheet().getRange(ss.getActiveRange().getLastRow(), lastChangedTime, 1, 1).setValue(new Date());
  
     }  
  }
  </code></pre>
  
  <p>Can anyone tell what the error is by looking at this code? Any help would be very much appreciated!</p>
  
  <p>EDIT 1:</p>
  
  <p>I wanted to follow-up and tell everyone how this issue resolved for me. I thought I had one problem, but I actually had two.</p>
  
  <ol>
  <li><p>The first problem was that I was trying to "Run" the OnEdit(e) function from the script editor to test it, which was throwing out the above error code. As Chemi Adel correctly notes, the code needs to be altered to enable this type of test. </p></li>
  <li><p>The second problem, as correctly identified by Mateo Randwolf in the comments, was that I had not correctly installed the trigger. To install the trigger, start by opening the google sheet containing the script. Then go to <strong>Tools > Script Editor</strong> to open the script editor. The script editor should open in a separate tab in your web browser. Then, within the script editor tab go to <strong>Edit > Current Project's Triggers</strong> which will open a new tab showing the triggers for the current project. Then press the "Add Trigger" button in the lower right, and set the parameters of the function as <strong>Function: OnChange</strong>, <strong>Deployment: Head</strong> (I don't know what this means but is only option for me), <strong>Select Event Source: From Spreadsheet</strong>, and <strong>Select Event Type: On Edit</strong>. I was able to find how to install the trigger in the documentation that Mateo Randwolf linked to.</p></li>
  </ol>