From c2cb5dca206d9cb3edad2b5f28ab3d2cd63e0bc0 Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Mon, 22 Jun 2020 11:48:25 -0400 Subject: [PATCH] Add a function to dump all the files in a folder to the active sheet --- Code.ts | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Code.ts b/Code.ts index eb2e497..2717178 100644 --- a/Code.ts +++ b/Code.ts @@ -9,6 +9,7 @@ function onOpen() { ui.createMenu('CMS Document Generation') .addItem('Generate document for current row', 'generateForCurrentRow') .addItem('Generate document for all rows', 'generateForAllRows') + // .addItem('CAUTION! Write list of all files to current sheet', 'dumpFiles') .addToUi(); } @@ -17,6 +18,41 @@ type DocSection = | GoogleAppsScript.Document.HeaderSection | GoogleAppsScript.Document.FooterSection; +function findAllFiles(path: string, folder: GoogleAppsScript.Drive.Folder) { + const out: string[][] = []; + + const files = folder.getFiles(); + while (files.hasNext()) { + const file = files.next(); + if (!file.isTrashed()) { + out.push([ + path, + file.getId(), + file.getName(), + `=HYPERLINK("${file.getUrl()}", "${file.getName()}")`, + ]); + } + } + + const subfolders = folder.getFolders(); + while (subfolders.hasNext()) { + const subfolder = subfolders.next(); + out.push(...findAllFiles(path + '/' + subfolder.getName(), subfolder)); + } + + return out; +} + +function dumpFiles() { + const root = DriveApp.getFolderById('1mWOQDmQjQsi8the09VXpnKnvFUzj4wiv'); + + const out = findAllFiles('', root); + + const active_sheet = SpreadsheetApp.getActive(); + const range = active_sheet.getRange(`R1C1:R${out.length}C${out[0].length}`); + range.setValues(out); +} + function copyElement( source_element: GoogleAppsScript.Document.Element, dest: DocSection,