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