Apps Script example · 12 min read

Update Clinical Trial Enrollment Funnels: Copy-Paste Apps Script Pattern

Working update clinical trial enrollment funnels example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

SpreadsheetAppTime-drivenClinical

Roll screening statuses into site-level enrolled vs target attainment for a trial. The workbook becomes the system of record; Apps Script owns the joins and business rules so analysts are not pasting monthly formulas.

Entry point `updateTrialEnrollment` reads typed columns with SpreadsheetApp batch APIs. SpreadsheetApp is the primary service; Time-driven describes how you usually invoke it after authorization.

Keep thresholds (grace days, bands, alert emails, API keys) in Config cells or Script Properties so the same .gs file promotes from sandbox to production without code edits.

This page is technical only: sheet layout, edge cases, runnable code, deploy steps, and FAQs for update clinical trial enrollment funnels. No consulting pitch—just the pattern you can paste and harden.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

TabColumnsNotes
ScreeningDate, Site, Subject, StatusFunnel
TargetsSite, TargetNGoals
EnrollmentAttainmentOutput

What this script does

updateTrialEnrollment() roll screening statuses into site-level enrolled vs target attainment for a trial.

  • Screen fail vs enrolled
  • Site targets
  • Attainment ratio

Prerequisites

Container-bound script on the workbook that contains the tabs in the setup table. Authorize SpreadsheetApp scopes on first run.

  • V8 runtime
  • Exact sheet names
  • Script timezone = ops timezone

Walkthrough

Count screened/fail/enrolled by site, join Targets, write attainment ratio.

Paste the code, set Config/Properties, run updateTrialEnrollment once, verify the output tab, then attach a Time-driven trigger.

Edge cases

Subjects counted once per Screening row; dedupe upstream if re-screens create duplicates.

Testing

Use a sandbox copy with 3–5 known rows. Compute expected outputs for update clinical trial enrollment funnels offline, run updateTrialEnrollment, and diff the output tab including one intentional bad row.

Hardening

Add LockService around multi-sheet writes if triggers can overlap. Log run timestamps. Keep API keys and alert emails in PropertiesService—not in shared cells.

Variations

Fork ideas: filter to one business unit, change grain (daily→weekly), or POST a summary payload after updateTrialEnrollment succeeds.

Operations notes

Assign an owner for the output tab, document regenerate steps, and treat `updateTrialEnrollment` as source of truth over ad-hoc cell formulas.

Full code: updateTrialEnrollment()

Run updateTrialEnrollment when source tabs are current. Edit sheet names and Config/Properties first.

function updateTrialEnrollment(){
  const ss=SpreadsheetApp.getActive();
  const rows=ss.getSheetByName('Screening').getDataRange().getValues().slice(1);
  const agg={};
  rows.forEach(r=>{
    const site=String(r[1]), status=String(r[3]).toUpperCase();
    if(!agg[site]) agg[site]={screened:0,failed:0,enrolled:0};
    agg[site].screened++;
    if(status==='SCREEN_FAIL') agg[site].failed++;
    if(status==='ENROLLED') agg[site].enrolled++;
  });
  const targets={};
  ss.getSheetByName('Targets').getDataRange().getValues().slice(1).forEach(r=>{ targets[String(r[0])]=Number(r[1])||0; });
  const out=Object.keys(agg).map(site=>{
    const a=agg[site], t=targets[site]||0;
    return [site,a.screened,a.failed,a.enrolled,t, t? a.enrolled/t : 0];
  });
  const sh=ss.getSheetByName('Enrollment');
  sh.clearContents();
  sh.appendRow(['Site','Screened','ScreenFail','Enrolled','Target','Attainment']);
  if(out.length) sh.getRange(2,1,out.length+1,6).setValues(out);
}
  1. Line 1: Entry point — bind triggers to updateTrialEnrollment.
  2. Line 3: Batch read; avoid per-cell getValue in loops.
  3. Line 13: Batch read; avoid per-cell getValue in loops.
  4. Line 21: Batch write output rows in one setValues call.
  5. Line 6: Rename sheet constants before production.

Deploy this example

  1. 01

    Open Apps Script

    In the bound spreadsheet: Extensions → Apps Script. For standalone projects, create one at script.google.com and link your Sheet by ID.

  2. 02

    Paste and save

    Add a .gs file, paste the code below, rename constants at the top (sheet names, column letters, API property keys), then save.

  3. 03

    Authorize once

    Run the main function from the editor. Accept OAuth scopes when prompted — triggers cannot run until authorization succeeds once.

  4. 04

    Add the trigger

    Triggers → Add trigger → choose the handler function and event (time-driven, on edit, or on form submit). Delete test triggers before production.

Before you run: update clinical trial enrollment funnels

  • 1Setup tabs exist with headers matching updateTrialEnrollment
  • 2Dry-run on a copy workbook
  • 3Timezone verified
  • 4SpreadsheetApp authorization completed
  • 5Output spot-checked against hand calc
  • 6Time-driven trigger added only after validation
  • 7Owners + alert recipients documented

Frequently asked questions

At minimum the tabs listed in the setup table for Update Clinical Trial Enrollment Funnels. Output tabs may be cleared each run—do not store source-of-truth data there.

Run from the Apps Script editor for dry runs. Production usually uses a Time-driven trigger after OAuth succeeds once.

Subjects counted once per Screening row; dedupe upstream if re-screens create duplicates.

Per-cell calls burn quota and wall time. One read + one write keeps this pattern under the 6-minute execution cap longer.

PropertiesService (Script Properties) for API keys and alert inboxes. Config sheet is fine for non-secret thresholds.

Simple ratios maybe; update clinical trial enrollment funnels needs joins, branching, or side effects (email/Docs/API) that Apps Script handles cleanly.

Keep the .gs in clasp/git. Avoid divergent copies of formulas on the output tab—regenerate from the script.

Related examples

Want this wired into your real workflow?

I adapt these patterns to your Sheet structure, APIs, and triggers — deployed in your Google account. Fixed-scope quotes from $500 · free 30-min consult · quote within 24 hours.