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

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

问题描述

由于DocsList的折旧,我的脚本根据电子表格中的列生成PDF文档,并根据电子表格中的列将电子邮件发送给收件人。请参阅此处: https://developers.google.com/google-apps/documents-list /



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



TypeError:在对象ProofOfCredit_CNZDTVR44N.pdf中找不到函数addFile。 (第45行,文件ProofOfCreditCode)

我会很感激任何建议和帮助,因为这种折旧打破了我的脚本中5个与这个脚本非常相似的脚本。

  var docTemplate =1JAPmsrPRrRwXCVAli229C5J7Kr4xaOnfO2rmGqvYyhU; 
var docName =ProofOfCredit;

函数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',品牌);
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('Credit of Credit');
var movefile = DocsList.createFile(pdf);
movefile.addToFolder(folder);
movefile.removeFromFolder(DocsList.getRootFolder());
var subject =关于订单号的信用证明:+ order_number;
var body =Hello+ first_name ++ last_name +,+< br />< br />
+感谢您致电+品牌+支持,附加文件中包含信息
+,供您参考与我们已发行的信用相关的信用回原始付款方式。 +< br />< br />
+如果您有任何其他问题或需要其他帮助,请告诉我们。 +< 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



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

  var folder = DocsList.getFolder('您的文件夹的名称'); 

在这种情况下,只需将 DocsList 替换为 DriveApp 会给你一个问题。对于 DriveApp 没有 getFolder()方法。在Google云端硬盘中,您可以拥有多个具有相同名称的文件和文件夹(但ID不同)。旧的 DocsList getFolder()方法没有考虑到这种情况。使用 DriveApp ,您仍然可以按名称搜索文件夹,但返回值是文件夹迭代器。使用 DriveApp ,按名称获取文件夹的方法名称稍有不同;它是文件夹,最后是s。 getFoldersByName(name)复数,不是单数。因此,即使98%的时间,通过名称获取文件夹只会导致一个文件夹,但仍然需要处理 Folder Iterator 。您不需要 来遍历文件夹迭代器。你可以得到第一个文件夹。只需使用 next()方法,而无需使用编程循环。 $ b

文件夹迭代器



所以,我建议您只需将 DocsList 替换为 DriveApp ,除非按名称获取文件夹。阅读文档并修复该行代码,然后运行它。如果您收到错误 VIEW EXECUTION TRANSCRIPT ,它可能会告诉您哪行代码失败。如果您仍然需要帮助,请始终发布代码行失败。



此外,没有 addToFolder()新的DriveApp文件夹类的方法。



文件夹类别 a>



您需要将其更改为:

 文件夹.addFile(moveFile); 

addFile()方法


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 />" 
   + "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);
}

解决方案

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

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

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