由于DocsList的贬值,我的脚本从Google云端硬盘中的模板生成pdf文档,然后根据电子表格中的列将其通过电子邮件发送给收件人,因此该脚本今天停止工作。看到这里:https://developers.google.com/google-apps/documents-list/
我已尝试按照本指南https://developers.google.com/drive/web/migration进行更新,以使用DriveApp更新DocsList的所有实例,但无法使其正常工作。有人可以帮我更新此脚本以使其正常工作吗?我在此脚本中有6个“DocsList”实例,并且能够正确更新其中的一些实例,但其他实例(例如“addFile”)似乎需要不同的格式。即使尝试简单地查找“DocsList”并将其替换为“DriveApp”,我仍然收到以下错误消息:
“TypeError:在对象ProofOfCredit_CNZDTVR44N.pdf中找不到函数addFile。(第45行,文件“ProofOfCreditCode”))”
我将不胜感激任何建议和帮助,因为这种折旧破坏了我的5个脚本,这些脚本与该脚本几乎相同。
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);
}
最佳答案
许多DriveApp
方法与DocsList
相同。因此,在许多情况下,您可以简单地将DocsList
替换为DriveApp
。
旧的getFolder()
方法会出现问题。
var folder = DocsList.getFolder('Name of your folder');
在这种情况下,仅将
DocsList
替换为DriveApp
会给您带来问题。 getFolder()
没有DriveApp
方法。在Google云端硬盘中,您可以具有名称相同(但ID不同)的多个文件和文件夹。旧的DocsList
,getFolder()
方法未考虑这种情况。使用DriveApp
,您仍然可以按名称搜索文件夹,但是返回的是Folder Iterator
。使用DriveApp
时,通过名称获取文件夹的方法名称略有不同;它是“文件夹”,末尾带有“s”。 getFoldersByName(name)
复数,不单数。因此,即使在98%的时间中,按名称获取文件夹将仅导致一个文件夹,但仍需要处理Folder Iterator
。您无需遍历文件夹迭代器。您可以只获取第一个文件夹。只需使用next()
方法,而无需使用编程循环。Folder Iterator
因此,我建议您仅将
DocsList
替换为DriveApp
,除非按名称获取文件夹。阅读文档,并修复该行代码,然后运行它。如果遇到错误,请查看 EXECUTION TRANSCRIPT ,它可能会告诉您哪一行代码失败。如果仍然需要帮助,请始终发布失败的代码行。另外,新的DriveApp Folder类没有
addToFolder()
方法。Folder Class
您将需要将其更改为:
folder.addFile(moveFile);
addFile() Method