r/GoogleAppsScript • • Jan 14 '21

Resolved First time user: Apps Script won't let me save anything, even default/blank function

Hi all, please forgive my lack of knowledge as I'm sure I'm just making a stupid mistake. I'm trying to write an onEdit function for a spreadsheet, but when I try to save I keep getting the error,

Syntax error: SyntaxError: Invalid or unexpected token line: 1 file: Code.gs

I have no clue what I am doing wrong. My code looks exactly like every example/video/etc. I can find... Here's the function:

​function onEdit(evt) {

 ​ var ss = SpreadsheetApp.getActiveSheet();
​  var r = evt.range;
​  if (r.getColumn() > 2 && ss.getName()=='Sheet1') {
 ​   var datecelladdress = 'A'+ r.getRowIndex();
 ​   var timecelladdress = 'B'+ r.getRowIndex();
 ​   ss.getRange(datecelladdress).setValue(new Date()).setNumberFormat("MM/dd/yyyy");
  ​  ss.getRange(timecelladdress).setValue(new Date()).setNumberFormat("hh:mm");
​  }
​}

Even if I create a new blank script, with nothing except "function myFunction() {}", it gives me the same syntax error.

8 Upvotes

8 comments sorted by

5

u/Gryphacus Jan 14 '21

Well, I suppose I'm just an idiot? Apparently, there was an invisible character before my function declaration. Pressing backspace in front of the function was enough to solve the issue.

4

u/TobofCob Jan 14 '21

The epitome of bug hunting. It wasn’t even in the function you wrote! Nice job finding the problem

2

u/Gryphacus Jan 15 '21

Went from ripping hair out to incredulous in the blink of an eye.

1

u/Wishyouamerry May 28 '21

I'm so glad you answered this post yourself and left it up. The same thing just happened to me. I backspaced in front of function, and BAM! Problem solved!

Thank you!