Add a function to dump all the files in a folder to the active sheet

This commit is contained in:
Adam Goldsmith 2020-06-22 11:48:25 -04:00
parent b7a301ba78
commit c2cb5dca20

36
Code.ts
View File

@ -9,6 +9,7 @@ function onOpen() {
ui.createMenu('CMS Document Generation') ui.createMenu('CMS Document Generation')
.addItem('Generate document for current row', 'generateForCurrentRow') .addItem('Generate document for current row', 'generateForCurrentRow')
.addItem('Generate document for all rows', 'generateForAllRows') .addItem('Generate document for all rows', 'generateForAllRows')
// .addItem('CAUTION! Write list of all files to current sheet', 'dumpFiles')
.addToUi(); .addToUi();
} }
@ -17,6 +18,41 @@ type DocSection =
| GoogleAppsScript.Document.HeaderSection | GoogleAppsScript.Document.HeaderSection
| GoogleAppsScript.Document.FooterSection; | 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( function copyElement(
source_element: GoogleAppsScript.Document.Element, source_element: GoogleAppsScript.Document.Element,
dest: DocSection, dest: DocSection,