Fletcher
Fletcher

Reputation: 37

GoogleScript Spreadsheet Custom Function Handling a range of cells and getting their values

I have a Goggle Spreadsheet with some data, and I want to write a custom function to use in the sheet, which accepts a range of cells and a delimiter character, takes each cell value, splits it by the delimiter, and counts the total.

For example
Column A has the following values in rows 1-3: {"Sheep","Sheep,Dog","Cat"}
My function would be called like this: =CountDelimitedValues(A1:A3;",");
It should return the value: 4 (1+2+1)

The problem I am having is in my custom script I get errors like

"TypeError: cannot get function GetValues from type Sheep"

This is my current script:

function CountArrayList(arrayList, delimiter) {
  var count = 0;
  //for (i=0; i<array.length; i++)
  //{
    //count += array[i].split(delimiter).length;
  //}

  var newArray = arrayList.GetValues();
  return newArray.ToString();

  //return count;
}

I understand that the parameter arraylist is receiving an array of objects from the spreadsheet, however I don't know how to get the value out of those objects, or perhaps cast them into strings.

Alternatively I might be going about this in the wrong way? I have another script which extracts the text from a cell between two characters which works fine for a single cell. What is it about a range of cells that is different?

Upvotes: 1

Views: 2185

Answers (3)

Jacob Jan
Jacob Jan

Reputation: 1197

That's something you can achieve without using script but plain old formula's:

=SUM(ARRAYFORMULA(LEN(A1:A3)-LEN(SUBSTITUTE(A1:A3; ","; "")) + 1))

Credit goes here: https://webapps.stackexchange.com/q/37744/29140

Upvotes: 3

Fletcher
Fletcher

Reputation: 37

Another solution I have was that I needed to implicitly cast the objects in the array being passed as a string.

For example this function accepts the array of cells, and outputs their contents as a string with del as the delimiter (similar to the String.Split() function). Note the TrimString function and that it is being passed an element of the array.

function ArrayToString(array,del) {
  var string = "";

  for (i=0; i < array.length; i++) {
    if (array[i] != null) {
      var trimmedString = TrimString(array[i]);
      if (trimmedString != "") {
        if (string.length > 0) {
          string += del;
        }

        string += trimmedString;
      }
    }
  }

  return string;
}

Below is the TrimString function.

function TrimString(string) {
  var value = "";
  if (string != "" && string != null) {
    var newString = "";
    newString += string;

    var frontStringTrimmed = newString.replace(/^\s*/,"");
    var backStringTrimmed = frontStringTrimmed.replace(/\s*$/,"");
    value = backStringTrimmed;
  }
  return value;
}

What I found is that this code threw a TypeError unless I included the declaration of the newString variable, and added the array element object to it, implicitly casting the array element object as a string. Otherwise the replace() functions could not be called.

Upvotes: 0

Serge insas
Serge insas

Reputation: 46792

something like this works :

function CountArrayList(arrayList) {
return arrayList.toString().split(',').length
}

wouldn't it be sufficient ?

edit Oooops, sorry I forgot the user defined delimiter, so like this

function CountArrayList(arrayList,del) {
return arrayList.toString().split(del).length
}

usage : =CountArrayList(A1:C1;",")

NOTE : in this example above it would be dangerous to use another delimiter than "," since the toString() joins the array elements with commas... if you really need to do so try using a regex to change the commas to what you use and apply the split on that.

try like this :

function CountArrayList(arrayList,del) {
return arrayList.toString().replace(/,/g,del).split(del).length
}

Upvotes: 1

Related Questions