DriveApp.getFileById()的怪异行为 [英] Weird Behaviour with DriveApp.getFileById()

查看:80
本文介绍了DriveApp.getFileById()的怪异行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码仍处于概念验证阶段-将尝试解释预期的结果以及我得到的结果.

Code is still in Proof of Concept phase - Will try to explain the expected result and what result I am getting.

此脚本将是常规邮件合并. 该脚本应执行以下操作:

This script will be a generic mail merge. The script is expected to do the following:

  • 提示用户输入要使用的模板文件的ID. (Google文档文档)
  • 制作模板副本,并更改​​模板副本正文中的值.
  • 获取模板的修改后的副本,并将其附加到电子邮件中.
  • 对电子表格中的每一行执行一次.

尽管上面的所有方法都能很好地工作,但在附加模板的编辑副本时却出现了怪异的行为.而不是获取模板的编辑版本,而是获取电子邮件附带的模板的未编辑版本

While everything above works really well the weird behaviour comes when attaching the edited copy of the template. Instead of getting an edited version of the template I am getting an unedited version of the template attached to the email

function myFunction() {


var ui = SpreadsheetApp.getUi();
var response = ui.prompt('Please enter the ID of your template file.', ui.ButtonSet.OK_CANCEL);

if(response.getSelectedButton() == ui.Button.OK){
var templateid = response.getResponseText()
}//End of IF Statement

var docid = DriveApp.getFileById(templateid).makeCopy().getId();
var doc = DocumentApp.openById(docid);
var body = doc.getBody();

var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lastColumn = sheet.getLastColumn();

sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).setNumberFormat("@");


var array = [];

for(var i = 1; i < lastColumn-1; i++) {
var headers = sheet.getRange(1, i, 1, 1).getValue();
array.push(headers)
}//END OF OUTER 1

for (var k = 2; k < sheet.getLastRow()+1; k++){
var row = sheet.getRange(k, 1, 1, lastColumn).getValues()

for (var j = 0; j <array.length; j++){
body.replaceText("<"+array[j]+">", row[0][j]);
}//END OF INNER


var recipient = sheet.getRange(k, lastColumn - 1, 1, 1).getValue();
var emailBody = "Good Day,\n\nPlease see attatched.\n\nRegards\nPerson's Name Here"
var subject = "See attatched";
var attatchment = DriveApp.getFileById(doc.getId());

Logger.log(doc.getId())


MailApp.sendEmail(recipient, subject, emailBody, {attachments: attatchment})

}//END OF OUTER 2
}//END OF FUNCTION

使用的电子表格:

推荐答案

从您的脚本中,我认为可能需要使用saveAndClose().因此,例如,在您的脚本中,请按如下所示将其添加到var attatchment = DriveApp.getFileById(doc.getId());之前.

From your script, I thought that it might be required to use saveAndClose(). So in your script, for example, please add it before var attatchment = DriveApp.getFileById(doc.getId()); as follows.

var subject = "See attatched";
doc.saveAndClose(); // Added
var attatchment = DriveApp.getFileById(doc.getId());

参考:

  • saveAndClose()
  • Reference:

    • saveAndClose()
    • 在DocumentApp,SpreadsheetApp和SlidesApp的类上,当通过每个类的方法修改Doc时,如果脚本正在运行,则通过使用save方法保存Doc来反映修改.另一方面,在Google APIs Sheets API和Slides API中,调用并运行每种方法时,即使脚本正在运行,也会反映出修改.在Google文档中,还没有Docs API.但是最近,发布了关于Docs API的早期访问的表格.当必须能够使用Docs API时,我认为API的方法也许可以在不使用saveAndClose()的情况下编辑和保存文档.

      At the classes of DocumentApp, SpreadsheetApp and SlidesApp, when the Doc is modified by the methods of each class, in the case that the script is running, the modifications are reflected by saving the Doc using the save method. On the other hand, at Google APIs of Sheets API and Slides API, when each method is called and run, the modifications are reflected, even if the script is running. At Google Document, there is no Docs API yet. But recently, the form about the early access of Docs API is released. When Docs API got to be able to be used, I think that the methods of API might be able to edit and save the document without using saveAndClose().

      这篇关于DriveApp.getFileById()的怪异行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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