Google App Mail 合并 - 发送的子文件未修改字段



我正在构建一个简单的Google App Script,它每个月都会发送一封带有文件的电子邮件。

该文件更新为 : -当月的第一天 -当月的最后一天 - 月份名称

所有文档创建和字段修改都运行良好(我在脚本启动几秒钟后看到带有修改字段的 gdoc(。

问题是我收到一封电子邮件,附件中没有修改的字段。

有谁知道为什么子文档修改得很好,但附加到电子邮件的 PDF 版本没有修改其字段?

谢谢!

function myFunction() {

//DEALS WITH DATES
//Create a date
var today = new Date();

//Create a variable that will take month's values
var Mois = ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Decembre"];   

//Get the current month
var MoisNum = today.getMonth()+1;
var TextMois = Mois[MoisNum-1];

//Get the number of first day of the month, and last day of the month
var Jour = today.getDate();
var NombreJourMois = ["31", "28", "31","30","31","30","31","31","30","31","30","31"];
var DernierJourMois = NombreJourMois[MoisNum-1];

//Get the current year
var Annee = today.getYear();

//Sets two variable with the date of the first day of the month and last day of the month
var DateDebutMois = 01+'/'+MoisNum+'/'+Annee;
var DateFinMois = DernierJourMois+'/'+MoisNum+'/'+Annee;



//CREATE A COPY OF THE PARENT FILE AND PUT IT IN THE RIGHT FOLDER  
//Dossier de destination et fichier template
var DossierSource = DriveApp.getFolderById('0B0mCpRQd5BQidmQ2MGl4em9MRzA');
var DossierCible = DriveApp.getFolderById("13pril4REhDrITljX523z7LdQgVpOcazU");
var FichierSource = DriveApp.getFileById('109_Kuoxjh-C56MYeFhyyP-PhYJirCD5OVhuce0v7osI');

//Create a copy of the document and put it in the above folder
var Enfant = FichierSource.makeCopy();
DossierCible.addFile(Enfant);
DossierSource.removeFile(Enfant);


//MODIFY CHILD DOCUMENT WITH THE PROPER DATES AND RENAME IT  
//Get the body of the child document
var BodyEnfant = DocumentApp.openById(Enfant.getId()).getBody();

//Replace date fields of the body by the right dates, and changes the name of the child document
BodyEnfant.replaceText('{1erDuMois}', DateDebutMois);
BodyEnfant.replaceText('{DernierDuMois}',DateFinMois);
Enfant.setName('Quittance de loyer - 218 rue de Grenelle - '+DateDebutMois);  

//SEND AND EMAIL WITH THE DOCUMENT ATTACHED
var recipient = 'jean-marie.laly@magicmakers.fr';
var subject = 'Quittance de loyer - '+TextMois+' - '+Annee;
var body = 'Bonjour, Veuillez trouver ci-joint la quittance de loyer du mois de '+TextMois;
var pdf = Enfant.getAs("application/pdf");
GmailApp.sendEmail(recipient, subject, body, {attachments: [pdf],name: ''});
}

我认为您的问题的原因是因为修改后的值未保存。因此,请在保存文档后转换为PDF。为了将修改后的值保存到文档中,如何对脚本进行此修改?

在这里,只显示了脚本中的修改部分。

从:

//Get the body of the child document
var BodyEnfant = DocumentApp.openById(Enfant.getId()).getBody();
//Replace date fields of the body by the right dates, and changes the name of the child document
BodyEnfant.replaceText('{1erDuMois}', DateDebutMois);
BodyEnfant.replaceText('{DernierDuMois}',DateFinMois);
Enfant.setName('Quittance de loyer - 218 rue de Grenelle - '+DateDebutMois); 

自:

//Get the body of the child document
var doc = DocumentApp.openById(Enfant.getId()); // Modified
var BodyEnfant = doc.getBody(); // Modified
//Replace date fields of the body by the right dates, and changes the name of the child document
BodyEnfant.replaceText('{1erDuMois}', DateDebutMois);
BodyEnfant.replaceText('{DernierDuMois}',DateFinMois);
Enfant.setName('Quittance de loyer - 218 rue de Grenelle - '+DateDebutMois); 
doc.saveAndClose(); // Added

参考:

  • 保存和关闭((

如果这不是你想要的,请告诉我。我想修改它。

最新更新