Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Friday, December 18, 2015

Creating a custom formula in Google Sheets to concatenate a range of values with a delimiter

In the past, I have written a little VBA to create an Excel function to do this.  I use it to take a range of cells containing email addresses and make one string containing those addresses delimited by semi-colons suitable for pasting into the TO: field of an email.

Now that I am using Google Sheets more, I was curious if I could do a similar thing.

With a little investigation, I was able to go to Tools | Script Editor and enter the following:

 function CCAT(range, delimiter) {  
  var returnString = '';  
  var rows = range.length;  
  for (r=0; r<rows;r++){  
   var cols = range[r].length;  
   for (c=0; c<cols; c++){  
    returnString += range[r][c]+delimiter;  
   }  
  }  
  return returnString;  
 }  

Once it is saved I can invoke it from my Sheet using something like:

 =CCAT(C2:C25,";")  

Notice that a range like C2:C25 is automatically converted by Sheets into a two dimensional array.

After successfully creating the function, I searched for a similar solution online and found that there are ways to do it that do not require a custom script - see
https://productforums.google.com/forum/?hl=en#!category-topic/docs/how-do-i/FQbzbVK4-i0 , however my script is illustrative if not elegant.

Thursday, March 11, 2010

Regular Expressions in Actionscript 2

Regular Expressions are a powerful method of working with text strings based on dynamic patterns. For example, you might want to extract all the numbers from a string like "Michael travels 550km in 6.2 hours using 40L of gasoline", or replace any occurrence of "INV" followed by an arbitrary number of alphanumerics followed by a space with "- reference removed -".

Regular Expressions are useful in parsing user input or creating grammars for strings.

Flash decided not to support a RegExp class in Actionscript 2, though it is implemented in Actionscript 3, Javascript, and JScript.

Pavils Jurjans
bemoaned this fact and created his own RegExp class for Flash 5, which was encapsulated by Joey Lott into a .as file for Actionscript 2. Pavils also created a very handy tester, which together with websina's page, helped me to get started with regular expressions.


The CLIPS programmers have started to use our version of the class, which has been documented with ZenDocs, and includes a trim, rtrim, ltrim and slightly modified replace method. A simple tester .fla is available to interact with the class.

I probably feel as excited learning this new skill as the fellow in this xkcd.com comic.