Skip to content
Gnaw script generator

Run your first custom function in a Sheet

The registration desk gets a booth code like West-007. Split it into a hall and a badge number while keeping all three digits.

Install the function in your spreadsheet

Open a blank Google Sheet. Choose Extensions → Apps Script. In the editor, add a script file called Booth, paste the code below and click Save. Return to the spreadsheet tab.

Booth.gs
Apps Script
/**
 * Splits a hall and its numeric booth ID into two columns.
 *
 * @param {string|string[][]} codes One code or a single-column range.
 * @return {string[][]} Hall and booth ID, one row per source cell.
 * @customfunction
 */
function BOOTHCODE(codes) {
  const rows = Array.isArray(codes) ? codes : [[codes]];
  return rows.map(function (row) {
    if (row.length !== 1) throw new Error('Select one column of booth codes.');
    const text = String(row[0]).trim();
    if (!text) return ['', ''];
    const match = /^(.+)-(\d+)$/.exec(text);
    return match ? [match[1], match[2]] : ['Check code', ''];
  });
}

Try a blocked result

  1. Enter West-007 in A2. Leave A3 empty. Enter Needs review in A4 and East-12 in A5.
  2. Type Keep aisle clear in C2. In B2 enter =BOOTHCODE(A2:A5). B2 shows #REF! because the second output column would overwrite C2. Your note stays in place.
  3. Clear the formula in B2. Enter the same formula in E2 with E2:F5 empty. The first row shows West and 007. The blank stays blank, the malformed code shows Check code, and the last row shows East and 12.
  4. Change A2 to West-009. F2 now reads 009 because the formula references the source range.

For your own function, use its name and arguments in the cell formula. Google’s custom function guide