Show Google docs document path

Last modified 2 years ago
Danger icon
The last modifications of this post were around 2 years ago, some information may be outdated!

Summary

Google docs doesn't show the folder path by default. This script will show it at the top.

Usage

Script needs to be installed via Tools->Script Editor and works one document at a time (for now).

Code

function onOpen() {
// Add a menu with some items, some separators, and a sub-menu.
DocumentApp.getUi().createMenu('Fresh Tools')
.addItem('Show Document Path', 'listParentFolders')
.addToUi();
}


function listParentFolders() {

var theDocument = DocumentApp.getActiveDocument();

var docID = theDocument.getId();

var theFile = DriveApp.getFileById(docID);

var parents = theFile.getParents();

// No folders
if ( parents == null ) return;

var folder = parents.next();

var folderName = folder.getName();

var folderURL = folder.getUrl();

var folders = [[folderName,folderURL]];

while (folderName != "Root"){

parents = folder.getParents();

if (parents.hasNext())
{
folder = parents.next();

folderName = folder.getName();

folderURL = folder.getUrl();

folders.unshift([folderName,folderURL]);
}
else
{
break;
}

}

var html = HtmlService.createHtmlOutput()
.setTitle('Document Path')
.setWidth(300);

var indentNum = 0, link;

var pathToDocument = '';

for (var fldCntr = 0; fldCntr < folders.length; fldCntr++){

folderName = folders[fldCntr][0];

folderURL = folders[fldCntr][1];

pathToDocument += '<h3>';

if(indentNum > 0)
{
pathToDocument += ' / '
}

pathToDocument += '<a href="' + folderURL + '">' + folderName + "</a>";

pathToDocument += '</h3>';

indentNum += 1;
}

html.append(pathToDocument);

DocumentApp.getUi()
.showSidebar(html);
}