paninaro95
paninaro95

Reputation: 3

My Google Apps Script doesn't work and I don't know why

I'm a newcomer to this site and I have a few questions:

I want to make a time recorder, for which someone will input any data in a Google Sheet ( a cell any place)

My code works successfully, but I can't see any results from my spreadsheets.

Here is my code:

    function endofstudy(e) {
        var app = SpreadsheetApp;
        var activeSheet = app.getActiveSpreadsheet().getActiveSheet();
        var currentCell = app.getActiveSpreadsheet().getRange("sheetname!K1255:K2000")
         if( currentCell.getRange < "101" ) { 
          var nextCell = currentCell.offset(0, -9);
          var newDate = Utilities.formatDate(new Date(),"GMT+09:00", "HH:mm");
          nextCell.setValue(newDate);
        }
      }

Upvotes: 0

Views: 307

Answers (1)

TheWizEd
TheWizEd

Reputation: 8616

The function you describe is typically called onEdit and is a simple trigger that doesn't need to be installed. Your function endofstudy need to be installed through the script editor.

I've shown an onEdit(e) that uses the event object to tell which cell is being edited and how to offset from there to place a time stamp.

I've edited my script to address the comments below.

Code.gs

function onEdit(e) {
  if( e.range.getSheet().getName() === "Sheet1" ) {  // limit this onEdit to only Sheet1
    if( e.range.getColumn() === 11 ) {  // Limit to only column K
      let row = e.range.getRow();
      if( ( row >= 1255 ) && ( row <= 2000 ) ) {  // Limit to rows 1255 to 2000
        let now = Utilities.formatDate(new Date(),"GMT+09:00", "HH:mm");
        e.range.offset(0,-7).setValue(now);  // offset -7 columns (K to D) from the cell being edited
      }
    }
  }
}

Reference

Upvotes: 1

Related Questions