appscript.dev
Automation Beginner Drive Docs

Generate a shareable file-index Doc

List every file in a Northwind folder with links — share one Doc instead of folder access.

Published Jul 12, 2025

When a Northwind client asks for “all the project files”, the quick option is to share the whole folder. But that hands them edit history, sub-folders they were never meant to see, and a live view that keeps changing under them. Often what they actually want is a tidy list of what exists and a link to each item.

This script reads one folder and builds a Google Doc that lists every file as a clickable link. You share the Doc, not the folder, so the recipient gets a clean snapshot with no extra access. Re-run it whenever the folder contents change and you have a fresh index in seconds.

What you’ll need

  • The ID of the folder you want to index — the string after /folders/ in its URL.
  • A title for the Doc, so each index is easy to recognise later.
  • Edit access to the folder. The script reads file names and URLs only; it never changes the files themselves.

The script

// The folder to index and the title for the generated Doc.
const INDEX_FOLDER_ID = '1abcFolderId';
const INDEX_DOC_TITLE = 'Northwind project files';

/**
 * Builds a Google Doc listing every file in a folder as a clickable link.
 * Returns the URL of the Doc so you can share it straight away.
 */
function buildFileIndex() {
  // 1. Resolve the folder. Bail out early if the ID is wrong.
  let folder;
  try {
    folder = DriveApp.getFolderById(INDEX_FOLDER_ID);
  } catch (err) {
    Logger.log('Could not open the folder — check INDEX_FOLDER_ID.');
    return;
  }

  // 2. Create the Doc and give it a title heading.
  const doc = DocumentApp.create(INDEX_DOC_TITLE);
  const body = doc.getBody();
  body.appendParagraph(INDEX_DOC_TITLE)
    .setHeading(DocumentApp.ParagraphHeading.TITLE);

  // 3. Walk the folder and append one linked line per file.
  const files = folder.getFiles();
  let count = 0;
  while (files.hasNext()) {
    const file = files.next();
    const para = body.appendParagraph('');
    para.appendText(file.getName()).setLinkUrl(file.getUrl());
    count++;
  }

  // 4. Note an empty folder so the Doc is never a confusing blank page.
  if (count === 0) {
    body.appendParagraph('(This folder contains no files.)');
  }

  // 5. Save and hand back the URL.
  doc.saveAndClose();
  Logger.log(`Indexed ${count} file(s): ${doc.getUrl()}`);
  return doc.getUrl();
}

How it works

  1. buildFileIndex resolves INDEX_FOLDER_ID inside a try/catch, so a wrong ID produces a clear log message instead of a stack trace.
  2. It creates a new Doc named after INDEX_DOC_TITLE and adds a matching title paragraph at the top.
  3. It calls getFiles() and loops through the result. For each file it appends an empty paragraph, then adds the file name as text and attaches the file’s URL as a link with setLinkUrl.
  4. It counts the files as it goes. If the folder turns out to be empty, it appends a short note so the shared Doc never looks broken.
  5. It saves the Doc and logs the file count alongside the URL, ready to share.

Example run

Suppose the folder contains three files:

File in Drive
2025 Statement of Work.pdf
Brand assets.zip
Kick-off notes.gdoc

After a run, the Doc reads:

Northwind project files

2025 Statement of Work.pdf
Brand assets.zip
Kick-off notes.gdoc

Each line is a live link to the file in Drive. The recipient clicks through to exactly the items you listed — nothing more.

Run it

This is an on-demand job, so run it whenever the folder contents change:

  1. In the Apps Script editor, select buildFileIndex and click Run.
  2. Approve the authorisation prompt the first time.
  3. Open the URL from the log, then share the Doc with whoever needs it.

Watch out for

  • The links point at the real files. A recipient can only open a link if the file itself is shared with them — the Doc grants no access on its own. Set the files’ sharing first, or this is just a list of names.
  • It indexes one folder, not its sub-folders. For a nested structure, see Visualize a deep folder hierarchy, or call this on each sub-folder you care about.
  • The Doc is a snapshot, not a live view. Rename or delete a file after generating the index and the Doc will be out of date — re-run it to refresh.
  • File order follows whatever Drive returns, which is roughly by creation date, not alphabetical. Sort the paragraphs afterwards if order matters.

Related