Share a Drive file when a row’s status flips to Approved

TL;DR — When a row goes to Approved, pull the file id from the link column, share it with the row’s email, and stamp SharedAt so reruns never re-share. Includes a dry-run flag. Plus a soft NitroGAS note for shareFileWithEmail.

The deliverable is done. Someone flips the row to Approved. Then they open Drive, find the file, click Share, paste the client’s email, pick Viewer, and hope they didn’t grab the wrong row’s link.

That last part is the bit a script should do. The row already has the file link and the email. All it needs is a status flip and a little care about not sharing twice.

TL;DR

  • A small job (time-driven every 10–15 minutes, or a menu item) looks for rows with Status = Approved and an empty SharedAt
  • File link → file id with extractDriveFileId; share with the row’s email via shareFileWithEmail
  • Stamp SharedAt (and who/what) right after sharing, so reruns skip the row
  • A dry-run flag logs what would be shared without touching Drive
  • Errors go on the row, not into a silent log

Using the Apps Script editor a lot? NitroGAS drops free themes & snippets right into script.google.com — optional Co-Pilot when you want a boost.

Why not onEdit?

It’s tempting to share the moment someone edits the Status cell. Two problems:

  • A simple onEdit can’t share files (no authorization), so you’d need an installable trigger anyway
  • Edits arrive in bursts (paste 20 statuses, fix a typo, undo), and you end up reacting to noise

A scan every few minutes is calmer. It treats Approved rows like a small queue: pick up the ones not yet shared, share, stamp. It’s the same idea as treating a sheet like a job queue, minus the claim step, because sharing is quick and safe to retry.

Sheet shape

A Client | B Email | C FileLink | D Status | E SharedAt | F ShareNote

Status is a dropdown (Pending / Approved / Rejected). SharedAt stays blank until the script shares.

What not to do

  • Don’t share based on Status alone; without SharedAt, every run tries again (and DriveApp sends a fresh email each time)
  • Don’t trust whatever is pasted in FileLink; extract and validate the id, and fail on garbage
  • Don’t share as Editor “to be safe”; give clients Viewer unless they really need to edit
  • Don’t run it for the first time on the live sheet without a dry run

The job

var SHARE_SHEET = 'Deliverables'; function shareApprovedFiles() { var props = PropertiesService.getScriptProperties(); var dryRun = props.getProperty('shareDryRun') === 'true'; // flip to false when happy var ss = SpreadsheetApp.getActive(); var sheet = ss.getSheetByName(SHARE_SHEET); var values = sheet.getDataRange().getValues(); var headers = values.shift(); var col = function (name) { var i = headers.indexOf(name); if (i === -1) throw new Error('Missing column: ' + name); return i; }; var iEmail = col('Email'), iLink = col('FileLink'), iStatus = col('Status'), iShared = col('SharedAt'), iNote = col('ShareNote'); var tz = ss.getSpreadsheetTimeZone(); var shared = 0; values.forEach(function (r, idx) { var rowNum = idx + 2; if (r[iStatus] !== 'Approved' || r[iShared]) return; // not ready, or already done try { var fileId = extractDriveFileId(r[iLink]); if (!fileId) throw new Error('No file id in FileLink'); var email = String(r[iEmail] || '').trim(); if (!email) throw new Error('No email on row'); if (dryRun) { sheet.getRange(rowNum, iNote + 1).setValue('DRY RUN: would share ' + fileId + ' with ' + email); return; } var res = shareFileWithEmail(fileId, email, 'viewer'); var stamp = Utilities.formatDate(new Date(), tz, 'yyyy-MM-dd HH:mm'); // Stamp immediately, so a crash on the next row can't cause a re-share of this one sheet.getRange(rowNum, iShared + 1, 1, 2).setValues([[ stamp, res.changed ? 'Shared (viewer)' : 'Already had access' ]]); shared++; } catch (err) { sheet.getRange(rowNum, iNote + 1).setValue('Error: ' + err.message); } }); console.log('%s row(s) shared%s', shared, dryRun ? ' (dry run)' : ''); }

The two helpers are in the catalog:

  • extractDriveFileId takes a pasted Docs/Drive URL (or a bare id) and returns the id
  • shareFileWithEmail grants Viewer/Editor and skips if the person already has that access or higher

Why both SharedAt and an idempotent share?

They catch different failures:

  • SharedAt stops the job from even trying again once a row is done. That’s cheap, and it’s visible to ops.
  • shareFileWithEmail skipping existing access covers the edge cases: someone shared by hand already, or the stamp didn’t get written because the run died right after sharing.

Belt and braces. A client getting the same “shared with you” email three times looks sloppy.

Notification emails

DriveApp always sends Google’s standard “shared with you” email, which is usually what you want for clients. To share quietly (say, internal reviewers), enable the Drive advanced service and call shareFileWithEmail(fileId, email, 'viewer', { notify: false }). The result’s method tells you which path ran.

Dry run first

1. Script Property shareDryRun = true 2. Run shareApprovedFiles from the editor 3. Read the ShareNote column: right files, right emails? 4. Set shareDryRun = false, clear the DRY RUN notes, run again 5. Install the trigger function installShareTrigger() { deleteTriggersByHandler('shareApprovedFiles'); ScriptApp.newTrigger('shareApprovedFiles').timeBased().everyMinutes(15).create(); }

Failure modes worth naming

  • Trigger owner doesn’t own or edit the file: you can only share what you can share. Keep deliverables in a folder the trigger owner controls.
  • External sharing blocked by Workspace policy: the share throws. The error lands in ShareNote and ops can escalate.
  • Wrong link pasted: the id extracts fine but it’s the wrong file. A dry run on new rows (or a “File name” column filled by the script) catches this.
  • Status flipped back to Pending: the job never unshares. If you need that, it’s a separate, deliberate step.
  • Group emails: sharing to a Google Group works, but the “already has access” check won’t see individual members.

Soft Co-Pilot note

NitroGAS keeps shareFileWithEmail and extractDriveFileId a search away in the editor. Paste your header row and Co-Pilot can draft the scan, the dry-run flag and the SharedAt stamp for your columns. You still run the dry run and read the notes. Free core; Co-Pilot is a pass you grab when you want the draft.

Closing checklist

  • Rows picked up only when Status = Approved and SharedAt is blank
  • File id extracted and validated; email required
  • SharedAt stamped right after each share
  • Dry run checked before going live
  • Errors written to the row

Happy Coding!