Home > Software engineering >  Apps Script function for Google Sheets: Long formula(?) causing error: `SyntaxError: missing ) after
Apps Script function for Google Sheets: Long formula(?) causing error: `SyntaxError: missing ) after

Time:01-19

The Apps Script function in question:

function insertNewRow() {
  var ss = SpreadsheetApp.openById("redactedforprivacy");
  var sheet = ss.getSheetByName("Main");
  sheet.insertRowBefore(2);
  
  var date = new Date();
  var month = date.getMonth()   1;
  var day = date.getDate();
  var year = date.getFullYear().toString().substr(-2);
  var formattedDate = month   "/"   day   "/"   year;
  
  sheet.getRange("A2").setValue(formattedDate);
  sheet.getRange("C2").setFormula("=ADD(C3,B2)");
  sheet.getRange("G2").setFormula("=IF(E2 F2>59,INT((E2 F2)/60)&IF(INT((E2 F2)/60)=1," hour"," hours")&" and "&MOD(E2 F2,60)&IF(MOD(E2 F2,60)=1," minute"," minutes"),MOD(E2 F2,60)&IF(MOD(E2 F2,60)=1," minute"," minutes");

}

The function makes a new row and adds some useful values and formulas in that row at various cells.

Like I said in the title, the error is as follows:

SyntaxError: missing ) after argument list line: 14 file: new line.gs

We're focusing on line 14, where it sets the formula for the cell G2. The formula is probably pretty big and messy by someone else's standards, but it works as expected on the spreadsheet. The error only occurs when line 14 is present, but I'm too stubborn to give up on the formula.

What I've tried:

(Disclaimer: I am not a programmer in any way, shape, or form.)

  1. I tried changing the formula around a few times, shortening it, changing it, trying to make it more efficient, but I couldn't make any modifications that still worked on the spreadsheet; probably my own skill issue though.

  2. I looked through line 14 a numerous amount of times to try to find where it might be missing a faithful ), but I still can't find it.

Edit: More Information

Tanaike's suggestion did make the function run without giving an error, but the formula ended up throwing a parsing error.

I expanded on what the formula in question is supposed to do:

This formula is used to add the values of two cells in minutes, and then format the output as a string that displays the total time in hours and minutes. The formula uses the IF function to check whether the total minutes is greater than 59. If it is, the formula uses the INT function to divide the total minutes by 60 and get the number of hours. It then uses the IF function again to check whether the number of hours is equal to 1, and adds " hour" or " hours" to the output string accordingly. It then uses the MOD function to get the remaining minutes (after subtracting the hours) and check whether it is equal to 1, and adds " minute" or " minutes" to the output string accordingly. If the total minutes is less than 60, the formula just uses the MOD function to get the minutes and check whether it is equal to 1, and adds " minute" or " minutes" to the output string accordingly.

CodePudding user response:

function insertNewRow() {
  var ss = SpreadsheetApp.openById("redactedforprivacy");
  var sheet = ss.getSheetByName("Main");
  sheet.insertRowBefore(2);
  
  var date = new Date();
  var month = date.getMonth()   1;
  var day = date.getDate();
  var year = date.getFullYear().toString().substr(-2);
  var formattedDate = month   "/"   day   "/"   year;
  
  sheet.getRange("A2").setValue(formattedDate);
  sheet.getRange("C2").setFormula("=ADD(C3,B2)");
  sheet.getRange("G2").setFormula('=IF(E2 F2>59,INT((E2 F2)/60)&IF(INT((E2 F2)/60)=1," hour"," hours")&" and "&MOD(E2 F2,60)&IF(MOD(E2 F2,60)=1," minute"," minutes"),MOD(E2 F2,60)&IF(MOD(E2 F2,60)=1," minute"," minutes"))');
}
  • Related