我有一个Google脚本,每天自动发送一封电子邮件。这是一个相当大的Google电子表格,具有多个“importrange”功能。我有一个完美的电子表格。但是,在大型电子表格中,当电子邮件发出时,数据仍在加载。我相信这是因为在下一个函数开始之前,数据没有时间加载。有没有人知道一个脚本可以在脚本移动之前延迟脚本中的下一个函数?在“SheetFlush”和“attachSendPDF”函数之间。脚本的副本如下:
function SheetFlush(worksheet) {
worksheet = worksheet || SpreadsheetApp.getActive();
var sheets = worksheet.getSheets();
SpreadsheetApp.flush();
}
function attachSendPDF() {
var ssID = SpreadsheetApp.getActiveSpreadsheet().getId();
var sheetName = SpreadsheetApp.getActiveSpreadsheet().getName();
var email = ("Email@gmail.com");
var subject = "Master Update";
var body = "Team, here is the Master Update.";
var todaysDate = new Date();
var subjectDate = subject+" "+todaysDate
//this is three level authorization
var oauthConfig = UrlFetchApp.addOAuthService("google");
oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://spreadsheets.google.com/feeds/");
oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");
//even better code
//oauthConfig.setConsumerKey(ScriptProperties.getProperty("consumerKey"));
//oauthConfig.setConsumerSecret(ScriptProperties.getProperty("consumerSecret"));
var requestData = {
"method": "GET",
"oAuthServiceName": "google",
"oAuthUseToken": "always"
};
//"&gid=0&fit to width=true" part is for pdf to b in portrait mode and gid part exports only first sheet . u could have all sheets, so dont put any gid or the number of sheet u wish to export
var url = "https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key="
+ ssID + "&exportFormat=pdf&gridlines=true&printtitle=0&size=letter&fzr=true&portrait=0&fitw=true";
var result = UrlFetchApp.fetch(url , requestData);
var contents = result.getContent();
MailApp.sendEmail(email, subjectDate , body, {attachments:[{fileName:sheetName+todaysDate+".pdf", content:contents, mimeType:"application//pdf"}]});
}
`
谢谢!
答案 0 :(得分:1)
尝试使用Utilities.sleep(毫秒)。请注意它会消耗您的每日时间配额。