Created
September 5, 2019 22:01
-
-
Save Dillie-O/68975ec988c11d0aab7ec00a8c32c984 to your computer and use it in GitHub Desktop.
[Get Document Path] Get Google Docs document path #google #gsuite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Note: Needs to be installed via Tools->Script Editor and works one document at a time (for now) | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment