0

I can't figure out what I'm doing wrong. I'm still new at this, so there's plenty of places for mistakes. I'm trying to get two versions of the same script to run. When a box is checked, if it's in column F I want function 7R to run, but if the checked box is in column K, I want function 8R to run. I used threads with similar questions to try and combine the two scripts but keep getting an error in line 2.

There error I was getting is Missing ; before statement: Line 2

Here is what I have:

function onEdit(e) {
8R();
7R();
}

function 8R() {
//Get the sheet you want to work with. 
var editrange = {
  top : 2,
  bottom : 260,
  left : 11,
  right : 11};
//getRow() and not getrow()
var thisrow = e.range.getRow();
if (thisrow < editrange.top || thisrow > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn = e.range.getColumn();
if (thiscolumn < editrange.left || thiscolumn > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: 
rangevalues
var range8 = sheet.getRange("K3:K90");
var range28 = sheet.getRange("M3:M90");
var range2values8 = range28.getValues();
var rangevalues8 = range8.getValues();
//Loops through range results
for (var i in rangevalues8) {
  Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
  //Set the rules logic
  if (rangevalues8[i][0] == true) { //Modified
  //Set the cell 
  range2values8[i][0] += 1; //Directly add 1 to range2values
  Logger.log(range2values8);//Added
  }
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell8 = destination.getRange("I3:I90");
destCell8.setValues(range2values8);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
range8.setValues(cleaningcell8);
}

function 7R() {
//Get the sheet you want to work with. 
var editrange = {
  top : 2,
  bottom : 260,
  left : 6,
  right : 6};
//getRow() and not getrow()
var thisrow7 = e.range.getRow();
if (thisrow7 < editrange.top || thisrow7 > editrange.bottom) return;
//getColumn() and not getcolumn()
var thiscolumn7 = e.range.getColumn();
if (thiscolumn7 < editrange.left || thiscolumn7 > editrange.right) return;
//Line that replaces the erroneous 'var ss = e.range.getSheet()';
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Responsible");
//Grab the entire Range, and grab whatever values you need from it. EX: 
rangevalues
var range7 = sheet.getRange("F3:F90");
var range27 = sheet.getRange("H3:H90");
var range2values7 = range27.getValues();
var rangevalues7 = range7.getValues();
//Loops through range results
for (var i in rangevalues7) {
  Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
  //Set the rules logic
  if (rangevalues7[i][0] == true) { //Modified
    //Set the cell 
    range2values7[i][0] += 1; //Directly add 1 to range2values
    Logger.log(range2values7);//Added
  }
}
//copy new information
var destination = ss.getSheetByName('Compiled Data');//whatever page
var destCell7 = destination.getRange("I93:I180");
destCell7.setValues(range2values7);
//clear checkboxes
var cleaning = ss.getSheetByName('Asset Bank');
var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
range7.setValues(cleaningcell7);
}

Here is a link to a replica sheet of what I'm working on with all of the relevant information: https://docs.google.com/spreadsheets/d/1PWaWm7AryljOMd5Aq1O2RSADyRFNVZbUDqKO__SzW2w/edit?usp=sharing

Michael Damp
  • 19
  • 1
  • 5
  • A function you call only receives the arguments you give it (you don't give your functions any parameters, nor do they bind any arguments they might be passed). Visit your favorite Javascript reference and review function scope namespace. Consider also that if your functions are mutually exclusive, you can perform range checking to determine which to *call*, rather than if execution should be *skipped*. – tehhowch Sep 07 '18 at 12:49
  • Thanks for the help. I'll keep cracking at it. This is what we get for not having money to hire someone that knows what they're doing or to buy a pre-made program. We get someone self-teaching/learning on the fly. I appreciate the advice, especially about range checking. – Michael Damp Sep 07 '18 at 16:41
  • I continued to get the same error using Henrique's answer. I updated the post to include the error. Missing ; before statement: Line 2. – Michael Damp Sep 07 '18 at 18:21
  • Please also ensure that the code in the question reflects the code that is giving you this `Missing ; before statement: Line 2` error. If you renamed in accordance with Henrique's answer, and still get that same error, please show that code. – tehhowch Sep 07 '18 at 20:53
  • My original code in the question was the code giving the error. When I renamed, I was able to save it, but was continuing to get other errors. But I got them sorted out using the script I posted below. – Michael Damp Sep 11 '18 at 17:27

2 Answers2

1

Javascript variables and functions names cannot start with numbers. Replace 8R and 7R with a valid identifier, for example func8R and func7R.

Apart from that, you 8R and 7R functions try to access right away a e.range variable, but that is not defined anywhere. I'm assuming you're trying to read the onEdit event parameter, but you have to pass that down onto your functions and also define the parameter their, e.g.

function onEdit(e) {
  f8R(e);
  f7R(e);
}

function f8R(e) {
  //... continue (remember to also declare on f7R)

If you want to manually test this function you have to fill in this e parameter, as the environment would do when you actually change something on the spreadsheet. I like to do that by writing another "caller" function, like this:

function testOnEdit() {
  onEdit({range: SpreadsheetApp.getActiveSheet().getRange('F2')});
}
Henrique G. Abreu
  • 17,406
  • 3
  • 56
  • 65
  • I did not know that. Thanks! – Michael Damp Sep 07 '18 at 16:39
  • I re-named them as you suggested, but the problem persists. – Michael Damp Sep 07 '18 at 18:21
  • For me, it was not allowing even saving the file with these wrong names. Complaining about syntax error on line 2, now it allowed save. What is the exact error message you're getting? Is it when saving or running? Are you triggering onEdit manually? – Henrique G. Abreu Sep 08 '18 at 00:19
  • I could get it to save, but not run when I changed the names. I tried to trigger it every way I could. But it's sorted out now. See my updated answer below. – Michael Damp Sep 11 '18 at 17:26
0

Finally was able to get the script working. First, I changed the names as Henrique suggested. Then got the onEdit block sorted out. Thanks everyone for the help and advice. I'd still be angry at my machine without you all.

function onEdit(e) {
  //don't need entire range, just the column that was modified to figure out which function to call
  var editColumn = e.range.getColumn();

  //confirm edit was a box being checked before running the code (as this seems to run on ANY sheet edit)
  if (e.oldValue === "false" && e.value === "TRUE") {
    if (editColumn === 6)
      func7R(e);
    else if (editColumn === 11)
      func8R(e);
  }
}

function func8R(e) {
  //Get the sheet you want to work with. 
  var editRange = {
    top : 2,
    bottom : 260,
    left : 11,
    right : 11};
  //getRow() and not getrow()
  var thisRow = e.range.getRow();
  if (thisRow < editRange.top || thisRow > editRange.bottom) return;
  //getColumn() and not getcolumn()
  var thisColumn = e.range.getColumn();
  if (thisColumn < editRange.left || thisColumn > editRange.right) return;
  //Line that replaces the erroneous 'var ss = e.range.getSheet()';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Responsible");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range8 = sheet.getRange("K3:K90");
  var range28 = sheet.getRange("M3:M90");
  var range2values8 = range28.getValues();
  var rangevalues8 = range8.getValues();
  //Loops through range results
  for (var i in rangevalues8) {
    Logger.log("rangevalues8["+i+"]["+0+"] is:"+rangevalues8[i][0]);//Added
    //Set the rules logic
    if (rangevalues8[i][0] == true) { //Modified
      //Set the cell 
      range2values8[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values8);//Added
    }
  }
  //copy new information
  var destination = ss.getSheetByName('Compiled Data');//whatever page
  var destCell8 = destination.getRange("I3:I90");
  destCell8.setValues(range2values8);
  //clear checkboxes
  var cleaning = ss.getSheetByName('Asset Bank');
  var cleaningcell8 = cleaning.getRange("A3:A90").getValues();
  range8.setValues(cleaningcell8);
}

function func7R(e) {
  //Get the sheet you want to work with. 
  var editRange = {
    top : 2,
    bottom : 260,
    left : 6,
    right : 6};
  //getRow() and not getrow()
  var thisrow7 = e.range.getRow();
  if (thisrow7 < editRange.top || thisrow7 > editRange.bottom) return;
  //getColumn() and not getcolumn()
  var thiscolumn7 = e.range.getColumn();
  if (thiscolumn7 < editRange.left || thiscolumn7 > editRange.right) return;
  //Line that replaces the erroneous 'var ss = e.range.getSheet()';
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Responsible");
  //Grab the entire Range, and grab whatever values you need from it. EX: rangevalues
  var range7 = sheet.getRange("F3:F90");
  var range27 = sheet.getRange("H3:H90");
  var range2values7 = range27.getValues();
  var rangevalues7 = range7.getValues();
  //Loops through range results
  for (var i in rangevalues7) {
    Logger.log("rangevalues7["+i+"]["+0+"] is:"+rangevalues7[i][0]);//Added
    //Set the rules logic
    if (rangevalues7[i][0] == true) { //Modified
      //Set the cell 
      range2values7[i][0] += 1; //Directly add 1 to range2values
      Logger.log(range2values7);//Added
    }
  }
  //copy new information
  var destination = ss.getSheetByName('Compiled Data');//whatever page
  var destCell7 = destination.getRange("I93:I180");
  destCell7.setValues(range2values7);
  //clear checkboxes
  var cleaning = ss.getSheetByName('Asset Bank');
  var cleaningcell7 = cleaning.getRange("A3:A90").getValues();
  range7.setValues(cleaningcell7);
}
tehhowch
  • 9,645
  • 4
  • 24
  • 42
Michael Damp
  • 19
  • 1
  • 5