2

What I would like to do is be able to run the script and have it create four new sheets (within the current sheet I'm in) with the following formatting in each:

1st row frozen with column headings on Column A, B, C, D, and F.  
A formula in E2 (=D2*100) that would be copied all the way to E30

I want to also do some other formatting but if I see the script for the top two I can hopefully learn enough to do the rest on my own. Right now I have started to write a script that uses the insertSheet command to create the sheets but I am stuck at that point.

1 Answer 1

2

Here is the script that does what you described. The formulas are prepared in a double array formulas ahead of time. Then, each new sheet gets one frozen row, columns headings, and formulas.

function setup() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var formulas = [];
  for (var i = 2; i <= 30; i++) {
    formulas.push(['=D' + i + '*100']);
  }
  for (i = 1; i <= 5; i++) { 
    var sheet = ss.insertSheet('New Sheet '+ i);
    sheet.setFrozenRows(1);
    sheet.getRange("A1:F1").setValues([['Heading 1', 'Heading 2', 'Heading 3', 'Heading 4', '', 'Heading 5']]);
    sheet.getRange("E2:E30").setFormulas(formulas); 
  }
}

However it's easier to use arrayformula command that deals with all multiplications at once: =arrayformula(D2:D30*100). Then the script is shorter:

function setup() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  for (i = 1; i <= 5; i++) { 
    var sheet = ss.insertSheet('New Sheet '+ i);
    sheet.setFrozenRows(1);
    sheet.getRange("A1:F1").setValues([['Heading 1', 'Heading 2', 'Heading 3', 'Heading 4', '', 'Heading 5']]);
    sheet.getRange("E2").setFormula("=arrayformula(D2:D30*100)");
  }
}
3
  • Thank you so much for the help. I will tinker with this a little and see if I get stuck again. Much appreciated. Commented Mar 7, 2016 at 13:02
  • Sally, I am stuck again :) I tried using the arrayformula for Column F but its not working, maybe because the arrayformula will not work with what I am trying to do. I want Column F to be able to spit out a letter grade based on Column E, so here is the formula I know works: =if(E2 > 89.44, "A", if(E2 > 79.44, "B", if(E2 > 69.44, "C", if(E2 > 59.44, "D", "F")))))" So what I did was put "=arrayformula( in front of it, but I am getting an error so I am assuming arrayformula doesn't work. Commented Mar 7, 2016 at 13:25
  • Sally, here is the only thing I added to your script: sheet.getRange("F2").setFormula("=arrayformula(if(E2:E30 > 89.44, "A", if(E2:E30 > 79.44, "B", if(E2:E30 > 69.44, "C", if(E2:E30 > 59.44, "D", "F")))))"); I know the arrayformula is correct as I have put it into a cell manually and it worked perfectly. The error I am getting when placing it into the script is "Missing ) after argument list." I have counted all my open and close parenthesis and they match. I'm stumped. Commented Mar 7, 2016 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.