From 2b377bbfca8279a16b920c53dd356028114a4f3d Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Wed, 18 Mar 2020 15:27:17 -0400 Subject: [PATCH] Delete old files with the same name as the generated output --- Code.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/Code.ts b/Code.ts index ec5c893..bf7ed5b 100644 --- a/Code.ts +++ b/Code.ts @@ -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