How to update DocsList to DriveApp in my code

My script that generates a pdf document from a template within Google Drive and emails it to a recipient based on columns in a spreadsheet stopped working today due to the depreciation of DocsList. See here: https://developers.google.com/google-apps/documents-list/

I have tried updating following this guide https://developers.google.com/drive/web/migration to update all instances of DocsList with DriveApp, but I cannot get it to work. Could somebody please help me update this script to work properly? I have 6 instances of "DocsList" in this script, and I was able to update some of them properly but others such as "addFile" seem to require a different format. Even after trying to simply find and replace "DocsList" with "DriveApp" I get the following Error Message:

" TypeError: Cannot find function addFile in object ProofOfCredit_CNZDTVR44N.pdf. (line 45, file "ProofOfCreditCode")"

I would appreciate any advice and help, as this depreciation broke 5 of my scripts which are pretty much identical to this one.

var docTemplate = "1JAPmsrPRrRwXCVAli229C5J7Kr4xaOnfO2rmGqvYyhU"; 
var docName     = "ProofOfCredit";

function onFormSubmit(e) {
   var first_name = e.values[1];
   var last_name = e.values[2];
   var customer_email = e.values[3];
   var order_number = e.values[4];
   var brand = e.values[5];
   var amount = e.values[6];
   var date_of_credit = e.values[7];
   var auth_code = e.values[8];
   var last_4 = e.values[9];
   var request_id = e.values[10];
   var rep_name = e.values[11];
   var copyId = DocsList.getFileById(docTemplate)
                .makeCopy(docName+'_'+order_number)
                .getId();   

   var copyDoc = DocumentApp.openById(copyId);
   var copyBody = copyDoc.getActiveSection();

   copyBody.replaceText('keyFirst', first_name);
   copyBody.replaceText('keyLast', last_name);
   copyBody.replaceText('keyBrand', brand);
   copyBody.replaceText('keyAmount', amount);
   copyBody.replaceText('keyCreditdate', date_of_credit);
   copyBody.replaceText('keyAuth', auth_code);
   copyBody.replaceText('keyRep', rep_name);
   copyBody.replaceText('keyOrder', order_number);
   copyBody.replaceText('keyCClast4', last_4);
   copyBody.replaceText('keyRequestID', request_id);
   var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy"); 
   copyBody.replaceText('keyTodaysDate', todaysDate);

   copyDoc.saveAndClose();

   var pdf = DocsList.getFileById(copyId).getAs("application/pdf"); 
   var folder = DocsList.getFolder('Proof of Credit');
   var movefile = DocsList.createFile(pdf);
   movefile.addToFolder(folder);
   movefile.removeFromFolder(DocsList.getRootFolder());
   var subject = "Proof of Credit regarding Order Number: " + order_number;
   var body    = "Hello " + first_name + " " + last_name + "," + "<br /><br />" 
   + "Thank you for calling " + brand + " Support. The attached document contains information " 
   + "for you to reference related to the credits we have issued back to your original form of payment." + "<br /><br />" 
   + "If you have any further questions or require additional assistance please let us know." + "<br /><br />" 
   + "Regards," + "<br /><br />" 
   + rep_name + ", Payments Department" + "<br />" 
   + "[email protected]";
   var cc = "[email protected]";
   MailApp.sendEmail(customer_email, subject, body, {htmlBody: body, attachments: pdf, cc: cc}); 
   DocsList.getFileById(copyId).setTrashed(true);
}

Answers:

Answer

Many of the DriveApp methods are identical to DocsList. So, in many cases, you can simply replace DocsList with DriveApp.

Problems arise with the old getFolder() method.

var folder = DocsList.getFolder('Name of your folder');

In this case, simply replacing DocsList with DriveApp will give you a problem. There is no getFolder() method for DriveApp. In Google Drive, you can have multiple files and folders with identical names (But different ID's). The old DocsList, getFolder() method did not take this situation into account. With DriveApp, you can still search for a folder by name, but the return is a Folder Iterator. With DriveApp the method name for getting a folder by name is very slightly different; it's "folders", with an "s" on the end. getFoldersByName(name) Plural, not singular. So, even though 98% of the time, getting folders by name will result in only one folder, you still need process the Folder Iterator. You don't need to loop through the folder iterator. You can just get the first folder. Just use the next() method without using a programming loop.

Folder Iterator

So, I suggest that you do just replace DocsList with DriveApp except in the case of getting a folder by name. Read the documentation, and fix that line of code, then run it. If you get an error, VIEW the EXECUTION TRANSCRIPT, and it will probably tell you what line of code failed. If you still need help, always post what line of code failed.

Also, there is no addToFolder() method of the new DriveApp Folder class.

Folder Class

You will need to change that to:

folder.addFile(moveFile);

addFile() Method

Answer

There is no method for "adding to folder" for a File object in DriveApp. Instead, you need to get the folder (which it looks like you store in var folder), and then use the Folder object to add a file. It would look something like this:

var folder = DriveApp.getFolderById(string id);
var movefile = DocsList.createFile(pdf);
folder.addFile(moveFile);

You can use Folder objects to add a file to the Folder, but DriveApp has no method for File objects to add themselves to a folder.

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.