title: Solving math equations with fractions on google scripts
body: <p>I am making a Google Sheets program to create a <a href="https://docs.google.com/spreadsheets/d/1s0tltmOLDwWmjROBZN_-ibpfc5jcnPk6aU-aQ-vLp80/edit?usp=sharing" rel="nofollow noreferrer">math worksheet</a> that randomly generates the numbers within certain parameters. </p>
  
  <p>I have succeeded in addition, subtraction, a mix of addition and subtraction, and multiplication.  I am now trying to add fractions to the mix, but have run into the formatting issue.
  While I have been able to generate random fractions and have them appear correctly, the issue comes in trying to have the correct answer generate on the Key sheet.</p>
  
  <p>I have tried finding a way to read the cells occupied by fractions as a string and parse out the individual numbers however that would only solve the issue with the numerator, and I still have the issue with the denominator (finding the common one or at least having the answer one that can be simplified by the student or teacher after solved).</p>
  
  <p>I included a link above, please let me know if you have any ideas to help with this issue.</p>
  
  <p>Also since I don't think I have it properly documented in the script, the max value of integers generated is located on the key page in the range "K2"</p>
  
  <pre><code>function myWSG() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var ws = ss.getSheetByName("Worksheet");
   var ks = ss.getSheetByName("Key");
  
   var m = ks.getRange('K2').getValue()+1;
  
   function getRandomInt(min, max) {
   min = Math.ceil(min);
   max = Math.floor(max);
   return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
  }
  for (var i = 8; i &lt;= 26; i = i + 2){
  var num = getRandomInt(0, m);
  var num1 = getRandomInt(0, m);
  var dn1 = getRandomInt(0, m);
  var dn2 = getRandomInt(0, m);
  var sym = getRandomInt(0, 2);
  ws.getRange("A1").copyFormatToRange(ws, 3, 3, i, i)
  ws.getRange("A1").copyFormatToRange(ws, 5, 5, i, i)
   switch(ws.getRange(6, 5).getValue()){
    case "Fractions":
      ks.getRange("A6").setValue("Fractions");
      if(dn1 == 0){
        ws.getRange(i, 3).setValue(num)
      }
  else if(dn1 == 1){
        ws.getRange(i, 3).setValue(num)
      }
      else{
        ws.getRange(i, 3).setValue(num + "/" + dn1);
      }
      if(dn2 == 0){
        ws.getRange(i, 5).setValue(num1)
      }
      else if(dn2 == 1){
        ws.getRange(i, 5).setValue(num1)
      }
      else{
        ws.getRange(i, 5).setValue(num1 + "/" + dn2);
      }
      break;
   case "Add&amp;Sub":
      ks.getRange("A6").clearContent();
      if(num &gt; num1){
        ws.getRange(i, 3).setValue(num);
        ws.getRange(i, 5).setValue(num1);
      }
      else{
        ws.getRange(i, 3).setValue(num1);
        ws.getRange(i, 5).setValue(num);
      }
      if(sym == 0){
        ws.getRange(i, 4).setValue('+')
       }
      else{
        ws.getRange(i, 4).setValue('-')
      }
      break;
    case "Addition":
      ks.getRange("A6").setValue("Fractions")
      ws.getRange(i, 4).setValue('+');
      if(num &gt; num1){
        ws.getRange(i, 3).setValue(num);
        ws.getRange(i, 5).setValue(num1);
      }
      else{
        ws.getRange(i, 3).setValue(num1);
        ws.getRange(i, 5).setValue(num);
      }
      break;
    case "Subtraction":
      ks.getRange("A6").setValue("Fractions")
      ws.getRange(i, 4).setValue('-');
              if(num &gt; num1){
        ws.getRange(i, 3).setValue(num);
        ws.getRange(i, 5).setValue(num1);
      }
      else{
        ws.getRange(i, 3).setValue(num1);
        ws.getRange(i, 5).setValue(num);
      }
      break;
    case "Multiplication":
      ks.getRange("A6").setValue("Fractions")
      ws.getRange(i, 4).setValue('x');
      ws.getRange(i, 3).setValue(num);
      ws.getRange(i, 5).setValue(num1);
      break;
   }
  }
  for (var v = 30; v &lt;= 46; v = v + 3){
  var vnum = getRandomInt(0, m); //random Integer 1
  var vnum1 = getRandomInt(1, m); //random Integer 2
  var vnum2 = getRandomInt(0, m); //random Integer 3
  var vnum3 = getRandomInt(1, m); //random Integer 4
  var vsym = getRandomInt(0, 2); //Symbol determinate
  var vsym1 = getRandomInt(0, 2); //Symbol determinate 2
  var vdn1 = getRandomInt(0, m); //random denominator 1
  var vdn2 = getRandomInt(0, m); //random denominator 2
  ws.getRange("A1").copyFormatToRange(ws, 3, 3, v, v); //Reset Format to 
  
  ws.getRange("A1").copyFormatToRange(ws, 5, 5, v, v); //Reset Format to 
  
  ws.getRange("N1").copyFormatToRange(ws, 3, 3, v+1, v+1); //Reset Format 
  
  ws.getRange("N1").copyFormatToRange(ws, 5, 5, v+1, v+1); //Reset Format 
  
  switch(ws.getRange(6, 5).getValue()){
    case "Fractions":
      if(vdn1 == 0){
        ws.getRange(v, 3).setValue(vnum);
      }
      else if(vdn1 == 1){
        ws.getRange(v, 3).setValue(vnum);
      }
      else{
        ws.getRange(v, 3).setValue(vnum + "/" + vdn1);
        ws.getRange(v, 5).setValue(vnum2 + "/" + vdn1);
      }
      if(vdn2 == 0){
        ws.getRange(v+1, 3).setValue(vnum1);
      }
      else if(vdn2 == 1){
        ws.getRange(v+1, 3).setValue(vnum1);
      }
      else{
        ws.getRange(v+1, 3).setValue(vnum1 + "/" + vdn2);
        ws.getRange(v+1, 5).setValue(vnum3 + "/" + vdn2);
      }
  break;
    case "Add&amp;Sub":
      if(vnum &gt; vnum1){
        ws.getRange(v, 3).setValue(vnum);
        ws.getRange(v+1, 3).setValue(vnum1);
      }
      else{
        ws.getRange(v, 3).setValue(vnum1);
        ws.getRange(v+1, 3).setValue(vnum);
      }
      if(vnum2 &gt; vnum3){
        ws.getRange(v, 5).setValue(vnum2);
        ws.getRange(v+1, 5).setValue(vnum3);
      }
      else{
        ws.getRange(v, 5).setValue(vnum3);
        ws.getRange(v+1, 5).setValue(vnum2);
      }
      if(vsym == 0){
        ws.getRange(v+1, 2).setValue('+');
      }
      else{
        ws.getRange(v+1, 2).setValue('-');
      }
      if(vsym1 == 0){
        ws.getRange(v+1, 4).setValue('-');
      }
      else{
        ws.getRange(v+1, 4).setValue('+');
      }
      break;
  case "Addition":
      ws.getRange(v+1, 2).setValue('+');
      ws.getRange(v+1, 4).setValue('+');
      ws.getRange(v, 3).setValue(vnum);
      ws.getRange(v+1, 3).setValue(vnum1);
      ws.getRange(v, 5).setValue(vnum2);
      ws.getRange(v+1, 5).setValue(vnum3);
      break;
    case "Subtraction":
      ws.getRange(v+1, 2).setValue('-');
      ws.getRange(v+1, 4).setValue('-');
      if(vnum &gt; vnum1){
        ws.getRange(v, 3).setValue(vnum);
        ws.getRange(v+1, 3).setValue(vnum1);
      }
      else{
        ws.getRange(v, 3).setValue(vnum1);
        ws.getRange(v+1, 3).setValue(vnum);
      }
      if(vnum2 &gt; vnum3){
        ws.getRange(v, 5).setValue(vnum2);
        ws.getRange(v+1, 5).setValue(vnum3);
      }
      else{
        ws.getRange(v, 5).setValue(vnum3);
        ws.getRange(v+1, 5).setValue(vnum2);
      }
      break;
    case "Multiplication":
      ws.getRange(v+1, 2).setValue('x');
      ws.getRange(v+1, 4).setValue('x');
      ws.getRange(v, 3).setValue(vnum);
      ws.getRange(v+1, 3).setValue(vnum1);
      ws.getRange(v, 5).setValue(vnum2);
      ws.getRange(v+1, 5).setValue(vnum3);
      break;
    }
  }
  for ( var s = 8; s &lt;= 26; s = s + 2){
    ks.getRange(s, 7).clearContent();
  }
   for ( var s2 = 32; s2 &lt;= 47; s2 = s2 + 3){
      ks.getRange(s2, 3).clearContent();
      ks.getRange(s2, 5).clearContent();
     }
  
  }
  function myKSG() {
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var ws = ss.getSheetByName("Worksheet");
   var ks = ss.getSheetByName("Key");
  
   for ( var s = 8; s &lt;= 26; s = s + 2){
     var nm = ks.getRange(s, 3).getValue();
     var nm1 = ks.getRange(s, 5).getValue();
  
     switch(ws.getRange(s, 4).getValue()){
      case "+":
        ws.getRange(s, 4).copyFormatToRange(ks, 7, 7, s, s);
        ks.getRange(s, 7).setValue(nm + nm1);
        break;
      case "-":
        ws.getRange(s, 4).copyFormatToRange(ks, 7, 7, s, s);
        ks.getRange(s, 7).setValue(nm - nm1);
      break;
      case "x":
        ws.getRange(s, 4).copyFormatToRange(ks, 7, 7, s, s);
        ks.getRange(s, 7).setValue(nm * nm1);
      break;
     }
    }
    for ( var s2 = 32; s2 &lt;= 47; s2 = s2 + 3){
      var vm = ks.getRange(s2 - 2, 3).getValue();
      var vm1 = ks.getRange(s2 - 1, 3).getValue();
      var vm2 = ks.getRange(s2 - 2, 5).getValue();
      var vm3 = ks.getRange(s2 - 1, 5).getValue();
      if( ks.getRange(s2-1, 2).getValue() == "+"){
        ks.getRange(s2, 3).setValue(vm + vm1);
     }
     else if(ks.getRange(s2-1, 2).getValue() == "-"){
        ks.getRange(s2, 3).setValue(vm - vm1);
      }
     else if(ks.getRange(s2-1, 2).getValue() == "x"){
       ks.getRange(s2, 3).setValue(vm * vm1);
     }
       if( ks.getRange(s2-1, 4).getValue() == "+"){
        ks.getRange(s2, 5).setValue(vm2 + vm3);
      }
      else if(ks.getRange(s2-1, 4).getValue() == "-"){
       ks.getRange(s2, 5).setValue(vm2 - vm3);
      }
       else if(ks.getRange(s2-1, 4).getValue() == "x"){
        ks.getRange(s2, 5).setValue(vm2 * vm3);
      }
     }
   }
  </code></pre>