-1

I'm trying to accomplish the below input and output in Google Sheets.

Valid XHTML

I was actually able to find a partial solution on Stack Exchange.

This is the script I pulled, but it only functions on one column, whereas I need to function on two at the same time:

function result(range) {
  delimiter = ", "
  targetColumn = 1

  var output2 = [];
  for(var i=0, iLen=range.length; i<iLen; i++) {
    var s = range[i][targetColumn].split(delimiter);    
    for(var j=0, jLen=s.length; j<jLen; j++) {
      var output1 = []; 
      for(var k=0, kLen=range[0].length; k<kLen; k++) {
        if(k == targetColumn) {
          output1.push(s[j]);
        } else {
          output1.push(range[i][k]);
        }
      }
      output2.push(output1);
    }    
  }
  return output2;
}
  • Well, that wasn't adapting his code, the 'targetColumn' just allows you to select which column you want to split, which in my case is B (or 1) and C (or 2), but the code only allows me to set one. – Kyle May 12 '17 at 18:15
1

I believe this will work for you:

function myFunction(range) {
    delimiter = ", ";
    targetColumn = 1;
    targetColumn2 = 2;

    var output2 = [];
    for(var i=0, iLen=range.length; i<iLen; i++) {
        var s = range[i][targetColumn].split(delimiter);    
        var s2 = range[i][targetColumn2].split(delimiter);
        for(var j=0, jLen=s.length; j<jLen; j++) {
           var output1 = []; 
           for(var k=0, kLen=range[0].length; k<kLen; k++) {
           if(k == targetColumn) {
              output1.push(s[j]);
           } 
           else if (k == targetColumn2) {
               output1.push(s2[j]);
           } else {
                output1.push(range[i][k]);
           }
        }
        output2.push(output1);
      } 
     }
    return output2;
  }

The other option would be to more or less run your own function twice. Create another copy of it, this time setting targetColumn = 2. Run the first function on the cells you have, and then run the second function on what the first function returns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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