我在脚本中遇到电子邮件附件问题。
代码:
//creating a new temporary spreadsheet
var tmpSpreadSheetId = SpreadsheetApp.create("attachment").getId();
var tmpSpreadSheet = SpreadsheetApp.openById(tmpSpreadSheetId);
var tmpSheet = tmpSpreadSheet.getActiveSheet();
//copying values from another spreadsheet with the original data
var values = otherSpreadSheet.getValues();
tmpSheet.getRange(1, 1, otherSpreadSheet.getLastRow(), otherSpreadSheet.getLastColumn()).setValues(values);
//formating some numbers in the temporary spreadsheet
var lastRow = tmpSpreadSheet.getLastRow();
tmpSheet.getRange(lastRow, 1, 1, 10).setBackground('#0000ff').setFontColor('white');
tmpSheet.getRange(1, 1, lastRow, 10).setBorder(null, true, null, true, null, true);
tmpSheet.getRange(1, 1, 1, 10).setBackground('#0000ff').setFontColor('white');
tmpSheet.getRange(2, 3, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 5, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 6, lastRow, 2).setNumberFormat("0.00");
tmpSheet.getRange(2, 8, lastRow, 1).setNumberFormat("00.00");
//logging the edited spreadsheet URL
Logger.log(tmpSpreadSheet.getUrl());
//sending an email with the spreadsheet as pdf
var file = DriveApp.getFileById(tmpSheet.getParent().getId());
MailApp.sendEmail('example@example.com', 'Reporting', 'bla bla bla', {attachments: [file.getAs(MimeType.PDF)]});
问题:临时电子表格看起来很好..彩色和正确的格式化数字等等, 但是在附件中,它只是普通的表格,其格式错误,没有颜色,就像数据一样,它们在开头插入了tempSheet。
希望你理解我的问题,我根本不知道,问题是什么,我试着修复它2-3个小时,但现在我需要帮助:)
先谢谢你,祝你有个愉快的一周 文森特
答案 0 :(得分:1)
在发送电子邮件之前,请考虑使用Utilities.sleep()添加延迟。也替换:
var file = DriveApp.getFileById(tmpSheet.getParent().getId());
使用:
var file = DriveApp.getFileById(tmpSpreadSheet);
答案 1 :(得分:0)
你的脚本几乎就在那里。您只需在发送电子邮件之前和完成格式化之后添加 SpreadsheetApp.flush(); (请参阅下面的代码)。 Utilities.sleep()是@Amit的一个很好的建议,但它不能用你的代码,因为它需要刷新才能更新,不幸的是,睡眠只会减慢代码,因为它不像setTimeout()那样工作(我试过了)。
无论如何这里是代码,我稍微编辑了它,但实质上唯一的区别是 SpreadsheetApp.flush()。如果您需要任何进一步的帮助,请告诉我:)
function myfunction() {
// otherSpreadSheet details
var otherSpreadSheet = SpreadsheetApp.openById("id").getSheets()[0];
//creating a new temporary spreadsheet
var tmpSpreadSheetId = SpreadsheetApp.create("attachment");
var tmpSheet = tmpSpreadSheetId.getSheets()[0];
//copying values from another spreadsheet with the original data
var values = otherSpreadSheet.getDataRange().getValues();
tmpSheet
.getRange(
1,
1,
otherSpreadSheet.getLastRow(),
otherSpreadSheet.getLastColumn()
)
.setValues(values);
//formating some numbers in the temporary spreadsheet
var lastRow = tmpSheet.getLastRow(); // **last row had an error in the script here
tmpSheet
.getRange(lastRow, 1, 1, 10)
.setBackground("#0000ff")
.setFontColor("white");
tmpSheet
.getRange(1, 1, lastRow, 10)
.setBorder(null, true, null, true, null, true);
tmpSheet.getRange(1, 1, 1, 10).setBackground("#0000ff").setFontColor("white");
tmpSheet.getRange(2, 3, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 5, lastRow, 1).setNumberFormat("00.00%");
tmpSheet.getRange(2, 6, lastRow, 2).setNumberFormat("0.00");
tmpSheet.getRange(2, 8, lastRow, 1).setNumberFormat("00.00");
// USE SpreadsheetApp.flush() to update the sheet before you send it as a PDF,
// this will allow you to apply the formatting and any other functions.
SpreadsheetApp.flush();
var fileID = tmpSheet.getParent().getId();
var file = DriveApp.getFileById(fileID);
MailApp.sendEmail("EMAIL HERE", "Reporting", "bla bla bla", {
attachments: [file.getAs(MimeType.PDF)]
});
}