From 967a06a1b45e7331511eb0db6d840105f3f45205 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Mon, 24 Jun 2024 07:21:55 -0500 Subject: [PATCH] move parsing to parsing.gs; write a simple parser for a demographics line --- Code.gs | 2 ++ parsing.gs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 parsing.gs diff --git a/Code.gs b/Code.gs index b7dac31..974ec16 100644 --- a/Code.gs +++ b/Code.gs @@ -1,3 +1,5 @@ + + function onOpen() { var ui = SpreadsheetApp.getUi(); diff --git a/parsing.gs b/parsing.gs new file mode 100644 index 0000000..8e00167 --- /dev/null +++ b/parsing.gs @@ -0,0 +1,53 @@ +/* The "Intern" age is an implementation detail. */ +ages = ["Adult", "Child", "Intern"] +/* These genders are not representative of the totality, but because I'm + * programming to a spec these are the choices. */ +genders = ["Male", "Female", "Unknown", "Other"] +/* This sounds wrong but it's the data we need to output. */ +ethnicities = ["Hispanic", "Not Hispanic"] +/* Sorry in advance. */ +races = [ + "American Indian/Alaskan Native", + "Asian", + "Black/African American", + "Native Hawaiian/Other Pacific Islander", + "White", + "More than one race", + "Unknown", +] + +/* These are for brevity -- they're as they appear on the form. + * The form should be at ./demographic-information-form.pdf. */ +age_shortcodes = ["A", "C", "I"] +gender_shortcodes = ["M", "F", "U", "O"] +ethnicity_shortcodes = ["H", "NH"] +race_shortcodes = ["AI/AN", "AS", "B/AA", "NH/OPI", "WH", "1+", "UNK"] + +function parseDemographicsLine(line) { + /* Take a row of the Demographics Data spreadsheet, then parse it into a dictionary. */ + site_name = line[0] + count = Number(line[1]) + age = ages[age_shortcodes.indexOf(line[2])] + gender = genders[gender_shortcodes.indexOf(line[3])] + ethnicity = ethnicities[ethnicity_shortcodes.indexOf(line[4])] + race = races[race_shortcodes.indexOf(line[5])] + + return Array(count).fill({ + site: site_name, + age: age, + gender: gender, + ethnicity: ethnicity, + race: race, + }) +} + +function parseDemographicsLineTest() { + console.log(parseDemographicsLine([ + "Testing site", + 5, + "A", + "M", + "NH", + "B/AA" + ])) +} \ No newline at end of file