Apps Script example · 9 min read

Calculate Fund NAV from Holdings: Copy-Paste Apps Script Pattern

Working calculate fund nav from holdings example in Apps Script—copy-paste code, common mistakes, and when to get it built professionally.

SpreadsheetAppTime-drivenFund ops

Value fund holdings, add cash, subtract accruals, and write NAV and NAV per share for an as-of date. The workbook becomes the system of record; Apps Script owns the joins and business rules so analysts are not pasting monthly formulas.

Entry point `calculateFundNav` 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 calculate fund nav from holdings. No consulting pitch—just the pattern you can paste and harden.

Need this built? Hire a Google Apps Script developer →

Sheet / project setup

TabColumnsNotes
HoldingsTicker, Qty, Price, StatusPositions
Cash/Accruals/ConfigBalances + sharesInputs
NavHistory appendOutput

What this script does

calculateFundNav() value fund holdings, add cash, subtract accruals, and write NAV and NAV per share for an as-of date.

  • Mark-to-market MV
  • Cash − fees
  • NAV per share

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

Sum qty×price for open holdings, add cash, subtract fee accruals, divide by shares for NAVPS, append Nav history.

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

Edge cases

Holdings marked SOLD are excluded; shares outstanding of 0 is guarded to 1 to avoid div/0.

Testing

Use a sandbox copy with 3–5 known rows. Compute expected outputs for calculate fund nav from holdings offline, run calculateFundNav, 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 calculateFundNav succeeds.

Operations notes

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

Full code: calculateFundNav()

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

function calculateFundNav(){
  const ss=SpreadsheetApp.getActive();
  const asOf=ss.getSheetByName('Config').getRange('B1').getValue();
  const holdings=ss.getSheetByName('Holdings').getDataRange().getValues().slice(1);
  let mv=0;
  holdings.forEach(r=>{
    if(String(r[4])==='SOLD') return;
    mv += (Number(r[2])||0)*(Number(r[3])||0); // qty * price
  });
  const cash=Number(ss.getSheetByName('Cash').getRange('B1').getValue())||0;
  const fees=Number(ss.getSheetByName('Accruals').getRange('B1').getValue())||0;
  const shares=Number(ss.getSheetByName('Config').getRange('B2').getValue())||1;
  const nav=mv+cash-fees;
  const navps=nav/shares;
  const sh=ss.getSheetByName('Nav');
  sh.appendRow([asOf,mv,cash,fees,nav,shares,navps,new Date()]);
}
  1. Line 1: Entry point — bind triggers to calculateFundNav.
  2. Line 4: Batch read; avoid per-cell getValue in loops.
  3. Line 4: Rename sheet constants before production.
  4. Line 5: Rename sheet constants before production.
  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: calculate fund nav from holdings

  • 1Setup tabs exist with headers matching calculateFundNav
  • 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 Calculate Fund NAV from Holdings. 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.

Holdings marked SOLD are excluded; shares outstanding of 0 is guarded to 1 to avoid div/0.

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; calculate fund nav from holdings 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.