0

Maybe you guys have an idea, thanks in advance community!

I'm current getting the Error:

Text result of JOIN is longer than the limit of 50000 characters

within Google Sheets when using this formula

=TRANSPOSE(SPLIT(JOIN(",";B1:B7);","))

I thought I could create a workaround in Google AppScript but my code is not working properly on the front end.

Current code:


function masterJoin(value) {
 
  //var delim = ","
  //var value = [["employee1","employee2"],["employee3"]]
  
  //delim = delim || '';
  
  var result = [];
  
  for (var i = 0; i < value.length; i++){
    
    //result.push(value[i]);
    //Logger.log(result);
    
    var temp = value[i]
    
    for (var t = 0; t < temp.length; t++){
    
      //Logger.log(temp[t])
      result.push(temp[t])
    
    }
     
  }
  
  return result
  
}

enter image description here

TheMaster
  • 45,448
  • 6
  • 62
  • 85
mrichman
  • 41
  • 1
  • 8

1 Answers1

2

Issue:

  • The values look like [["employee1,employee2"],["employee3"]] and not like [["employee1","employee2"],["employee3"]]. In the former case, they're strings. In the latter case, they're arrays.(They're not!)

Solution:

Sample script:

/**
 * Simulates TRANSPOSE(SPLIT(JOIN(",",array),","))
 * @param {Object[][]} array2d The two dimensional array passsed from Google sheet
 * @returns {String[]} 1D array - 1D is enough for custom function
 * @customfunction
 */
const masterJoinFixed = array2d => recursiveFunc_(array2d).flat(3);

/**
 * Recursive helper function to split all elements
 *   of a multi dimensional array by comma `,`
 * @param {Object[]|String} arrOrString
 * @returns {Object[]}
 * @private
 */
const recursiveFunc_ = arrOrString =>
  Array.isArray(arrOrString)
    ? arrOrString.map(recursiveFunc_)
    : String(arrOrString).split(',');

/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const masterJoinFixed = array2d => recursiveFunc_(array2d).flat(3);

const recursiveFunc_ = arrOrString =>
  Array.isArray(arrOrString)
    ? arrOrString.map(recursiveFunc_)
    : String(arrOrString).split(',');
console.info(masterJoinFixed([['1,2,3'],[4],[5]]))
<!-- https://meta.stackoverflow.com/a/375985/ -->    <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Thanks for the suggestion. The underlying code was just for testing purposes. Sorry but I'm very new to this :( . How would you convert your suggestion into a GoogleAppscript code? function masterJoin(value) {...} ``` //var value = [["employee1","employee2"],["employee3"]] ``` – mrichman Oct 08 '20 at 12:15
  • 1
    @mrichman Just copy paste my code. It should work out of the box – TheMaster Oct 08 '20 at 12:18
  • I don't know what I'm doing wrong. :( I've added the code and tried to do =masterJoin(B2:B8) in the sheet but I'm not getting any result :( ``` function masterJoin(value) { const masterJoinFixed = array2d => recursiveFunc_(array2d).flat(3); const recursiveFunc_ = arrOrString => Array.isArray(arrOrString) ? arrOrString.map(recursiveFunc_) : String(arrOrString).split(','); } ``` – mrichman Oct 08 '20 at 13:15
  • 1
    @mrichman Copy only what I added. Don't add `function masterJoin(value){}` on the outside. Can you guess the function name you should use on the sheet? – TheMaster Oct 08 '20 at 13:23
  • 1
    @mrichman Make sure to read all the links and practice. – TheMaster Oct 08 '20 at 15:00