简体   繁体   中英

Script to merge PDF files with same name in G Suite set to weekly trigger

Looking for help compiling a code to merge PDF files with the same name in Google Documents. I have a script that saves a spreadsheet as a PDF with a name which is a formatted date. That said, there could be up to 7 with the same name. I need to merge these into 1 PDF but I need to do it automatically set up on a weekly trigger.

Anything helps. Thank you!

This combines the text of all PDF's in a folder

function combinePDFs() {
  var folder=DriveApp.getFolderById('1Zd_ty0O1WljjADzGQGrtUM57hvE5berT');
  var destFolder=DriveApp.getFolderById('1mHRFCwzqccJn83N7THnvZ8_Z-DCLeGOV');
  var files=folder.getFilesByType(MimeType.PDF);
  var text='';
  while(files.hasNext()) {
    var file=files.next();
    var blob=file.getBlob();
    var resource={title:blob.getName(),mimeType:blob.getContentType()}
    var f=Drive.Files.insert(resource, blob, {ocr: true,ocrLanguage: "en"});
    var doc=DocumentApp.openById(f.id);
    text+=doc.getBody().getText() + '\n------------------------------------------------------------\n';
    DriveApp.getFileById(f.id).setTrashed(true);//trash intermediate files
  }
  var tf=DocumentApp.create('combined.doc');
  tf.getBody().setText(text);
  tf.saveAndClose();//combined text file
  var docblob=DocumentApp.openById(tf.getId()).getAs('application/pdf');
  docblob.setName('combined.pdf');//
  destFolder.createFile(docblob);//combined PDF
  DriveApp.getFileById(tf.getId()).setTrashed(true);//trashed combined text file
}

Reference from Amit Agarwal

Drive.Files.insert

Setup a Time Based Trigger

Upload files with Google Apps Script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM