Delete old files with the same name as the generated output

This commit is contained in:
Adam Goldsmith 2020-03-18 15:27:17 -04:00
parent df130c5d03
commit 2b377bbfca

18
Code.ts
View File

@ -65,6 +65,12 @@ function spreadsheetRowToObject(
}, {});
}
function trashFiles(files: GoogleAppsScript.Drive.FileIterator) {
while (files.hasNext()) {
files.next().setTrashed(true);
}
}
function generateForCurrentRow() {
const spreadsheet = SpreadsheetApp.getActive();
const cell = spreadsheet.getCurrentCell();
@ -82,10 +88,14 @@ function generateForCurrentRow() {
'1ROyJXk-QANTHM6Jiw0ne3EQiR1f2UsLr'
);
const out_file = source_file.makeCopy(
row['Document Name'] + row['Version'],
out_folder
);
const out_name = row['Document Name'] + '_' + row['Version'];
// Delete old files with the same name
trashFiles(out_folder.getFilesByName(out_name));
trashFiles(out_folder.getFilesByName(out_name + '.pdf'));
// Duplicate source document and open
const out_file = source_file.makeCopy(out_name, out_folder);
const out_doc = DocumentApp.openById(out_file.getId());
// Copy header