0
votes

I have a Google Sheets spreadsheet that's been working for months, and has no scripts associated with it. Recently I decided I'd like to log the date of change to a row, and that the onEdit() function could do that for me.

From the spreadsheet display, I selected Tools / Script Editor, which popped up an empty script with a myFunction() function.

I modified myFunction() to:

function onEdit(e) {
Logger.log("Something was edited.");  
}

Then pressed Save and accepted the default progject name (Untitled project). I also tested this (Run) and it created a log entry.

Now when I edit any value in the sheet or add a new row, I would expect a log entry. But none appears.

Have I missed a step? What must one do in order to get a valid onEdit() to run?

3
did you authorized the script?player0

3 Answers

1
votes

Your logs will be available at View>Stackdriver logging. View>Logs will only show logs executed in the current session (by clicking "Run"). Using console class is preferred.

0
votes

Disclaimer: I don't often work with the Script Editor in Google Sheets, nor Google Sheets itself, too often. There may be a better way to accomplish this.


In my experience, I've found you also have to explicitly add a trigger to your Script Editor project to get the events to fire correctly.

  1. From your sheet, open your Apps Script project by selecting the Tools > Script Editor menu option.
  2. From the resulting Apps Script project screen, select the Edit > Current Project's Triggers menu option.
  3. From the resulting Triggers view, click the + Add Trigger button (appeared in the lower-right of the interface presented to me; your mileage may vary).
  4. In the resulting modal, select the following options:
    1. "Choose which function to run" - select the onEdit method to execute the method you've written.
    2. "Choose which deployment should run" - I've selected the "Head" option, but I'd imagine your mileage will vary wildly if you've elected to use the more advanced deployment features here.
    3. "Select event source" - set this to "From spreadsheet".
    4. "Select event type" - set this to "On edit".
    5. "Failure notification settings" - set this to whatever setting you'd like. Finally, click the "Save" button.

Once your trigger is set up, the spreadsheet should now be running your method whenever an edit to the spreadsheet is made.

0
votes

Answer:

Make sure you run the onEdit(e) function from the Apps Script UI using the play button (►) and authorise the script to run as you.

I hope this is helpful to you!