how to prevent app script from duplicating files

27 views
Skip to first unread message

Learmie James

unread,
May 24, 2024, 10:48:54 AMMay 24
to Google Apps Script Community
I was wondering how to prevent app scripts from duplicating a file once the document link has already been created. This is the script that I used.

function onOpen() {
   const ui = SpreadsheetApp.getUi();
   const menu = ui.createMenu('AutoFill Docs');
   menu.addItem('Create New Docs', 'createNewGoogleDocs');
   menu.addToUi();
   }


   function createNewGoogleDocs()  {

    const googleDocsTemplate = DriveApp.getFileById('1HKJxmSJDtvviFLJPsWox3UOuK0kd3C7K59K3M6yzakg');
    const destinationFolder = DriveApp.getFolderById('1BL9fp0PnC-65Rf-9OlcOm0hMaJPhqIUN');
    const sheet = SpreadsheetApp.getActiveSpreadsheet() . getSheetByName ('2024');
    const rows = sheet.getDataRange().getValues();

    rows.forEach(function(row, index) {
    if (index === 0) return;
    if (row[5]) return;

    const copy = googleDocsTemplate.makeCopy(`${row[1]}, ${row[2]} Management Details`, destinationFolder)
    const doc = DocumentApp . openById(copy .getId () )
    const body = doc .getBody();
    const friendlyDate = new Date(row[3]) . toLocaleDateString();

    body .replaceText('{{Date}}', row[0]);
    body .replaceText('{{Owner Name}}', row[1]);
    body .replaceText('{{Owner Phone Number}}', row[2]);
    body .replaceText('{{Owner Email}}', row[3]);
    body .replaceText('{{Occupied}}', row[4]);
    body .replaceText('{{Market Ready}}', row[5]);
    body .replaceText('{{Under Different Management}}', row[6]);
    body .replaceText('{{Company Name}}', row[7]);
    body .replaceText('{{End Date}}', row[8]);
    body .replaceText('{{Address to Manage}}', row[9]);
    body .replaceText('{{Bedrooms}}', row[10]);
    body .replaceText('{{Baths}}', row[11]);
    body .replaceText('{{Square Footage}}', row[12]);
    body .replaceText('{{Rent Amount}}', row[13]);
    body .replaceText('{{Pets Allowed}}', row[14]);
    body .replaceText('{{Notes}}', row[15]);
    body .replaceText('{{Initials}}', row[17]);
    body .replaceText('{{Source}}', row[16]);

    doc .saveAndClose();
    const url = doc.getUrl();
    sheet.getRange(index + 1,19) .setValue(url)
 
           

    })
   }

Reply all
Reply to author
Forward
0 new messages