如何在我的代码中将 DocsList 更新为 DriveApp [英] How to update DocsList to DriveApp in my code

查看:15
本文介绍了如何在我的代码中将 DocsList 更新为 DriveApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于 DocsList 的折旧,我的脚本从 Google Drive 中的模板生成 pdf 文档,并根据电子表格中的列将其通过电子邮件发送给收件人,今天停止工作.请参阅此处:https://developers.google.com/google-apps/documents-list/

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/

我尝试按照本指南进行更新 https://developers.google.com/drive/web/migration 使用 DriveApp 更新 DocsList 的所有实例,但我无法让它工作.有人可以帮我更新这个脚本以使其正常工作吗?我在此脚本中有 6 个DocsList"实例,我能够正确更新其中一些实例,但其他一些(例如addFile")似乎需要不同的格式.即使在尝试简单地查找DocsList"并将其替换为DriveApp"后,我仍收到以下错误消息:

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:

类型错误:在对象 ProofOfCredit_CNZDTVR44N.pdf 中找不到函数 addFile.(第 45 行,文件ProofOfCreditCode")"

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

我希望得到任何建议和帮助,因为这种折旧破坏了我的 5 个脚本,这些脚本与这个脚本几乎相同.

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 />" 
   + "test@test.com";
   var cc = "test@test.com";
   MailApp.sendEmail(customer_email, subject, body, {htmlBody: body, attachments: pdf, cc: cc}); 
   DocsList.getFileById(copyId).setTrashed(true);
}

推荐答案

许多 DriveApp 方法与 DocsList 相同.因此,在许多情况下,您可以简单地将 DocsList 替换为 DriveApp.

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

旧的 getFolder() 方法出现问题.

Problems arise with the old getFolder() method.

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

在这种情况下,简单地将 DocsList 替换为 DriveApp 会给您带来问题.DriveApp 没有 getFolder() 方法.在 Google 云端硬盘中,您可以拥有多个名称相同(但 ID 不同)的文件和文件夹.旧的DocsListgetFolder() 方法没有考虑到这种情况.使用DriveApp,您仍然可以按名称搜索文件夹,但返回的是一个Folder Iterator.对于 DriveApp,按名称获取文件夹的方法名称略有不同;它是文件夹",末尾有一个s".getFoldersByName(name) 复数,而不是单数.因此,即使在 98% 的情况下,按名称获取文件夹只会产生一个文件夹,您仍然需要处理 Folder Iterator.您不需要遍历文件夹迭代器.您可以只获取第一个文件夹.只需使用 next() 方法,无需使用编程循环.

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.

文件夹迭代器

所以,我建议你 do 只用 DriveApp 替换 DocsList 除非按名称获取文件夹的情况.阅读文档,修复那行代码,然后运行它.如果您遇到错误,请查看执行转录本,它可能会告诉您哪行代码失败.如果您仍然需要帮助,请始终发布失败的代码行.

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.

此外,新的 DriveApp Folder 类没有 addToFolder() 方法.

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

文件夹类

您需要将其更改为:

folder.addFile(moveFile);

addFile() 方法

这篇关于如何在我的代码中将 DocsList 更新为 DriveApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆