我有一个google脚本,可以将一系列值复制到新的电子表格中,然后将该新副本设置为pdf,然后通过电子邮件发送给它。一切正常,但在创建pdf后设置值。所以我收到一封空白pdf的电子邮件。我尝试使用" Utilities.sleep()"但那也不起作用。有关为什么在pdf之后复制值的线索?
如果您需要澄清代码,请告诉我。
function myFunction() {
//Template Info
var sheetTemplate = ""; //sheet id goes here
//Sheet with List of Suppliers
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var info = sheet.getRange("A1:B1").getValues();
//Grabs name and email
var name = "";
name = info[0][0];
var email = "";
email = info[0][1];
//Copies template and save the sheet's id
var copyId = DriveApp.getFileById(sheetTemplate).makeCopy("Copy").getId();
// Open the temporary spreadsheet
var copySs = SpreadsheetApp.openById(copyId);
// Get the sheet
var copySheet = copySs.getSheetByName("Sheet1");
var values = sheet.getRange("A9:F9");
var copyRange = copySheet.getRange("A9:F9");
copyRange.setValues(values);
//Convert Template File to PDF
emailMe(copyId, email, name);
}
function emailMe(copyId, email, name) {
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = "Test";
var body = "This is a test.";
MailApp.sendEmail(email, subject, body, {htmlBody: body, attachments: pdf});
}
答案 0 :(得分:0)
这次修改怎么样?
copyRange.setValues(values);
时会发生错误。因为values
是一个范围。它是var values = sheet.getRange("A9:F9");
。
A9:F9
。copyRange.setValues(values);
,请使用SpreadsheetApp.flush();
。var values = sheet.getRange("A9:F9");
var copyRange = copySheet.getRange("A9:F9");
copyRange.setValues(values);
//Convert Template File to PDF
emailMe(copyId, email, name);
至 :
var values = sheet.getRange("A9:F9").getValues(); // Modified
var copyRange = copySheet.getRange("A9:F9");
copyRange.setValues(values);
SpreadsheetApp.flush(); // Added
//Convert Template File to PDF
emailMe(copyId, email, name);
如果这不是你想要的,我很抱歉。