Glib
Glib

Reputation: 262

Missing ; error in first attempt at script

I am writing an OnOpen script for a spreadsheet. I have a number in cell B1 and a date in C1 When the spreadsheet is opened, I want to check that the date on the sheet is today's and if not, it will change the sheet's date and also reset the number in B1 to 0

I am getting the error Missing ; before statement. (line 8, file "Code") (Which is the If Statement)

function OnOpen(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Create Ticket");
  var TicketCount = sheet.getRange("B1").getvalue();
  var TicketDate = sheet.getRange("C1").getvalue();
  var Today = date().setNumberFormat("dd/mm/yyyy");

  If not TicketDate = Today {
    sheet.getRange("C1").setValue(new Date()).setNumberFormat("dd/mm/yyyy");
    sheet.getRange("B1").setvalue(0);
  }
}

This is all new to me so my apologies if this is basic. The only coding I ever did was simple VBA, so I don't even know what == means.

Upvotes: 0

Views: 57

Answers (1)

St3ph
St3ph

Reputation: 2290

You can try this function

function onOpen(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Create Ticket");
  var TicketCount = sheet.getRange('B1').getValue();
  var TicketDate = Utilities.formatDate(new Date(sheet.getRange("C1").getValue()),Session.getScriptTimeZone(),"dd/MM/yyyy");
  var Today = Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"dd/MM/yyyy");
  if(TicketDate != Today) {
    sheet.getRange("C1").setValue(Utilities.formatDate(new Date(), Session.getScriptTimeZone(),"dd/MM/yyyy"));
    sheet.getRange("B1").setValue(0);
  }
}

Be carefull because in your code there is some typo error. setvalue => setValue getvalue => getValue

This will generate error.

To format date you can use Utilities.formatDate()

Stéphane

Upvotes: 1

Related Questions