Home > database >  unexpected extra copy of google doc in google drive root
unexpected extra copy of google doc in google drive root

Time:10-11

I have an invoice template which is a google doc saved in GOOGLE DRIVE\SYSTEM\ named (invoice template.gdoc) My goal is to make a copy of this template to GOOGLE DRIVE\SYSTEM\INVOICE PDF\INVOICE TEMP FOLDER, use "replaceText" to create individual invoice and save the created invoice as a pdf in the GOOGLE DRIVE\SYSTEM\INVOICE PDF. And the new pdf is downloadable with the http script so that user can immediately open and print.

My folder structure are as follow:

GOOGLE DRIVE\SYSTEM\INVOICE PDF\INVOICE TEMP FOLDER (ID: TEMPFOLDER, temp invoice google doc saved here and will be deleted immediately at the end of the function)

GOOGLE DRIVE\SYSTEM\INVOICE PDF\ (ID: PDF FOLDER, newly created pdf saved here)

GOOGLE DRIVE\SYSTEM\ (invoice template saved here)

GOOGLE DRIVE\ (Google drive root)

I achieved my goal. However, there is an unexpected copy of google doc in the GOOGLE DRIVE ROOT named (Copy of invoice template.gdoc) appears. Which part went wrong?

//function to create an invoice pdf
function createInvoiceAndPrint(grabConfirmEntryValue, entryNumber, invoiceNumber, invoiceTime) {


  const templateInvoice = DriveApp.getFileById("template file id");
  const invoiceTempFolder = DriveApp.getFolderById("TEMPFOLDER");
  const invoiceFolder = DriveApp.getFolderById("PDF FOLDER");
  const tempInvoiceTemplate = templateInvoice.makeCopy(invoiceTempFolder);
  const tempInvoiceDoc = DocumentApp.openById(tempInvoiceTemplate.getId());
  const body = tempInvoiceDoc.getBody();

  body.replaceText("{EntryNo.}", Utilities.formatString('d', invoiceNumber ));
  //and a bunch of rewrite content code here...

  tempInvoiceDoc.saveAndClose();
  const invoiceContentBlob = tempInvoiceTemplate.getAs(MimeType.PDF);
  var newlyCreatedPdf = invoiceFolder.createFile(invoiceContentBlob).setName(invoiceNumber   ".pdf");

  var downloadLink = HtmlService
    .createHtmlOutput('<p>Entry ID: '   entryNumber   '<br>Invoice No.: '   invoiceNumber   '</p> <p>Print Invoice: <a href="'   newlyCreatedPdf.getUrl()   '" target="_blank">here</a>.</p>')
    .setWidth(300)
    .setHeight(200);

  SpreadsheetApp.getUi().showModalDialog(downloadLink, "Sales Created");


  invoiceTempFolder.removeFile(tempInvoiceTemplate);

}

CodePudding user response:

I think that the reason for your issue of However, there is an unexpected copy of google doc in the GOOGLE DRIVE ROOT named (Copy of invoice template.gdoc) appears. is due to invoiceTempFolder.removeFile(tempInvoiceTemplate);. So, please modify it as follows.

From:

invoiceTempFolder.removeFile(tempInvoiceTemplate);

To:

// invoiceTempFolder.removeFile(tempInvoiceTemplate); // Remove this line.
  • And also, removeFolder(child) has already been deprecated. Please be careful about this. Ref

Added:

From your following reply,

I need only the pdf

If you want to remove the copied template document, please modify your showing script as follows.

From:

invoiceTempFolder.removeFile(tempInvoiceTemplate);

To:

tempInvoiceTemplate.setTrashed(true);
  • By this, the copied template document is moved to the trash box.
  • Related