7

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);
}
1
  • 1
    This is the original code without having replaced the string "DocsList" with "DriveApp", as this did not work (nor did I expect it to but I figured it was worth a try)
    – CalPal
    Apr 21, 2015 at 15:51

2 Answers 2

11

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

2
  • Thank you Sandy. I did as you suggested and replaced all strings of "DocsList" with "DriveApp". I had to change var folder = DocsList.getFolder('Proof of Credit'); to var folder = DriveApp.getFolderById('0B3nrCN8N5OBceFQ5WGVVLWVoNjQ'); but I am still getting the following error: 4/21/15 1:12 PM onFormSubmit TypeError: Cannot find function addToFolder in object ProofOfCredit_CNZDTVR44N.pdf. (line 45, file "ProofOfCreditCode")
    – CalPal
    Apr 21, 2015 at 17:14
  • Thank you! I think I was able to figure it out just as you replied. I replaced 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()); with var pdf = DriveApp.getFileById(copyId); var theblob = pdf.getBlob().getAs('application/pdf'); var folder = DriveApp.getFolderById('0B3nrCN8N5OBceFQ5WGVVLWVoNjQ'); var movefile = folder.createFile(theblob); and it seems to be working.
    – CalPal
    Apr 21, 2015 at 17:40
-1

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.

1
  • 3
    Did you intend to still have a deprecated DocsList method call in your answer?
    – Mogsdad
    Apr 21, 2015 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.